Problem with accentuation when generating PDF with dompdf

Asked

Viewed 2,348 times

5

I am generating a PDF report using the dompdf API, and the accentuation words are not being displayed correctly.

I put the charset='utf-8' tag inside the head, but it does not solve the problem

<meta charset='utf-8'/>

In php elements if I do:

<?php echo utf8_decode($variavel); ?>

Content is displayed correctly with accentuation...

*But HTML elements still have accentuation problem...

Example

inserir a descrição da imagem aqui

Code

<?php
ob_start('geraPDF'); 
?>

<html>
    <head>
        <meta charset='utf-8'>

    <style>
        p{
            border:1px solid black;
            box-shadow:5px 5px 5px #ccc;
            width:90%;
            padding:5px;
        }
    </style>

     </head>

    <body>

        <b>Empresa: </b><?php echo utf8_decode($empresas); ?><br>

        <b>Data: </b><?php echo date('d/m/Y',strtotime($dt)); ?><br>

        <b>Nº da OS: </b><?php echo $os; ?><br>

        <b>Nº Orçamento: </b><?php $norcamento; ?><br><br>

    </body>
</html>

<?php
// Importa arquivo de config da classe DOMPDF
require_once 'dompdf/dompdf_config.inc.php';

/**
 *  Função ob_get_clean obtém conteúdo que está no buffer
 *  e exclui o buffer de saída atual.
 *  http://br1.php.net/manual/en/function.ob-get-clean.php 
 */
$html = ob_get_clean(); 
$pdf = new DOMPDF();
$pdf->load_html($html);
$pdf->render();
$pdf->stream(date('d/m/Y').'_orcamento.pdf', array('Attachment'=>0));
?>

The result does not come out as expected in the pdf...which may be happening?

  • 1

    Already tried to set the header? header('Content-type: text/html; charset=UTF-8').

  • 1

    I believe you will find what need here.

  • That’s it, problem solved. Thank you! @Bruno Wego

1 answer

3


First try adding the header to PHP:

header('Content-type: text/html; charset=UTF-8')

If this solution doesn’t work, edit the file dompdf_config.inc.php:

mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);

The above answer is based on a response in stackoverflow international.

Browser other questions tagged

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