Bootstrap CSS issues in DOMPDF

Asked

Viewed 1,078 times

-2

I’m using the DOMPDF to print a CV that picks up the data via $_GET of the URL.

When I "print" the file on the screen, just the HTML, I can get exactly what I need, but when I try to turn the DOMPDFwith CSS in another location, it does not generate, only generates a blank screen...

How can I do the DOMPDF understand this style sheet?

I also tried to add inline CSS, but it doesn’t recognize things like float for example...

Can anyone give me a hand with using external CSS with DOMPDF? It is mandatory that I use it, I can not use something else like html2pdf for example.

Just follow my code:


<?php
error_reporting(E_ALL);
$var1 = "";
$var2 = "";
$var3 = "";
$var5 = "";
$var6 = "";
$var7 = "";
require_once 'dompdf/autoload.inc.php';

include('../conexao.php');

$cpf = $_GET['cpf'];

$result_nome = mysqli_query($conexao, "SELECT * FROM profissionais WHERE cpf='$cpf'");
    while($dados = mysqli_fetch_object($result_nome)) {
        global $nome;
        $nome = $dados->nome;
        $var1 .= '
    <html>
    <head>
        <link rel="stylesheet" href="../css/bootstrap.css" />
    </head>
    <body>
        <div class="row">
            <div class="card_CV col-md-10 col-xs-10 col-ls-10 offset-md-1">
                <div class="row justify-content-sm-center">
                    <div class="col-lg-3 col-md-6 col-sm-11 text-center image-print"float:left; >
                        <div class="col text-center"><img class="imgCV mt-2" src="../imagem/perfil/'.$dados->foto.'">
                        </div>
                    </div>
                    <div class="col-lg-6 col-md-6 col-sm-6 text-center name-print">
                        <div><h3 class="mt-5">'.$dados->nome.'</h3>
                        <h5 class="text-info mt-2">'.$dados->profissao.'</h5></div>
                    </div>
                    <div class="col-lg-3 col-md-6 col-sm-6 text-center dados-print">
                        <div><label class="mt-4"><i class="fas fa-map-marker-alt mr-2"></i>'.$dados->cidade.' - '.$dados->estado.' </label></div>
                        <div><label class=""><i class="fas fa-phone-volume mr-2"></i>'.$dados->telefone.'</label></div>
                        <div><label class=""><i class="fas fa-envelope mr-2"></i>'.$dados->email.'</label></div>
                    </div>
                </div>
                <div class="row mt-5 ml-2">
                    <div class="col-11"><h4><i class="fas fa-bookmark mr-3"></i>Apresentação</h4></div>
                    <div class="col-12"><p>'.$dados->apresentacao.'</p></div>
                </div>
            </div>
        ';
    }
    $var1 .= '<div class="card_CV col-md-10 col-xs-10 col-ls-10 mt-3 offset-md-1">
        <div class="row col mt-2 justify-content-end">
            <h4 class="mr-auto ml-4">Formação Academica</h4>
        </div>';
         $var1;
    $result_formacao = mysqli_query($conexao, "SELECT * FROM formacaoacademica WHERE cpf='$cpf'");
    while($formacao = mysqli_fetch_object($result_formacao)) {
        $var2 .= '
            <div class=" col-12">
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-6"><h5 class="card-title">'.$formacao->escola.'</h5>
                            </div>
                        <div class="row col-12">
                            <h6 class="card-subtitle font-weight-normal mb-2 text-muted">'.$formacao->curso.'</h6>
                        </div>
                        <div class="row col-12">
                            <h6 class="card-subtitle font-italic mb-2 text-info">'.$formacao->anoinicio.' - '.$formacao->anotermino.'</h6>
                        </div>
                        </div>
                    </div>
                </div>
            </div>
        ';
    }
     $var2;
     $var3 .= '</div>
    <div class="card_CV col-md-10 col-xs-10 col-ls-10 mt-3 offset-md-1">
        <div class="row col mt-2 justify-content-end">
            <h4 class="mr-auto ml-4">Licenças e certificados</h4>
        </div>';
    $result_certificados = mysqli_query($conexao, "SELECT * FROM certificadosprofi WHERE cpf='$cpf'");
    while($certificado = mysqli_fetch_object($result_certificados)) {
        $var5 .= '
            <div class=" col-12">
                <div class="card mb-3">
                    <div class="card-body">
                        <div class="row">
                            <div class="col-6"><h5 class="card-title">'.$certificado->curso.'</h5>
                            </div>
                        <div class="row col-12">
                            <h6 class="card-subtitle font-weight-normal mb-2 text-muted">'.$certificado->escola.'</h6>
                        </div>
                        <div class="row col-12">
                            <h6 class="card-subtitle font-italic mb-2 text-info">'.implode('/', array_reverse(explode('-', $certificado->data))).'</h6>
                        </div>
                        </div>
                    </div>
                </div>
            </div>
        ';
    }
     $var5;
     $var6 .= '</div>
    <div class="card_CV col-md-10 col-xs-10 col-ls-10 mt-3 offset-md-1" style="margin-bottom: 3%; padding-bottom: 2%">
        <div class="row col mt-2 justify-content-end">
            <h4 class="mr-auto ml-4">Experiência Profissional</h4>
        </div>';
    $result_experiencia = mysqli_query($conexao, "SELECT * FROM experienciaprofissional WHERE cpf='$cpf'");
    while($experiencia = mysqli_fetch_object($result_experiencia)) {
        $var7 .= '
            <div class="card-body" style="background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.125)">
                <div class="row">
                    <div class="col-6">
                        <h5 class="card-title">'.$experiencia->empresa.'</h5>
                    </div>
                </div>
                <div class="row col-12">
                    <h6 class="card-subtitle font-weight-normal mb-2 text-muted">'.$experiencia->funcao.'</h6>
                </div>
                <div class="row col-12">
                    <h6 class="card-subtitle font-italic mb-2 text-info">'.implode('/', array_reverse(explode('-', $experiencia->admissao))).' - '.implode('/', array_reverse(explode('-', $experiencia->demissao))).'</h6>
                </div>
                <div class="row col-12">
                    <h6 class="card-subtitle font-weight-normal mb-3 mt-2 text-muted">'.$experiencia->localidade.'</h6>
                </div>
                <div class="row col-12">
                    <p class="mb-2">'.$experiencia->responsabilidade.'</p>
                </div>
            </div>
    </body>
    </html>
        ';
    }
     $var7;

$encoding = mb_internal_encoding();
//$encoding = mb_internal_encoding();

$content = $var1 . $var2 . $var3 . $var5 . $var6 . $var7;

use Dompdf\Dompdf;

$dompdf = new DOMPDF();

//$dompdf->set_base_path("public_html/css/");

// Carrega seu HTML
$dompdf->load_html($content);

//Renderizar o html
$dompdf->render();

//Exibir a página
$dompdf->stream(
    $nome."cv.pdf",
    array(
        "Attachment" => false //Para realizar o download somente alterar para true
    )
);

?>

  • This is a symptom of an error in the code. Have you debugged this in the log? Is there anything you’re not letting the library render PHP output.

2 answers

1


@Jaksonfischer

Bro think you need to inform the css path to Dompdf

type if boostrap is externally, like bootstrap CND, you need to activate in domPdf

$options->set('isRemoteEnabled', TRUE);

if you are in place, you need to indicate where, with set_base_path():

$dompdf->set_base_path("caminho/pasta-com-bootstrap");

type with this automatic example:

$dompdf->set_base_path(realpath(dirname(__FILE__).'pasta-com-bootstrap/'));

Try it on and tell us. :)

0

Hi, Jackson!

Why not use an absolute URL, such as the Bootstrap repository itself? My first guess is that maybe DOMPDF isn’t locating the relative path ../css/bootstrap.css.

Another important thing is that you involve all your content that is in body in a div with the class container or container-fluid, as requested by Bootstrap.

Make sure all the requirements of DOMPDF are being serviced on your server (local/production). And activate all PHP error messages/warnings to see if DOMPDF is doing any output.

Anything, comments here! ;)

  • Hello Mac, All requirements are met, when I have "printd" the content, it can assemble everything correctly, but when I have it generate the PDF, it is not able to generate. It generates only one blank page.

  • Did you get any information from Apache logs? You might find something there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.