Dompdf not working in Symfony and displays characters

1.5k Views Asked by At

I already installed the dompdf in composer but I cannot render a pdf file and it shows some characters on the browser. Is there something i missed out on the code?

%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R >> >> /MediaBox [0.000 0.000 595.280 841.890] >> endobj 4 0 obj [/PDF /Text ] endobj 5 0 obj << /Producer (��dompdf + CPDF) /CreationDate (D:20191015175221+02'00') /ModDate (D:20191015175221+02'00') >> endobj 6 0 obj << /Type /Page /MediaBox [0.000 0.000 595.280 841.890] /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Filter /FlateDecode /Length 67 >> stream x��2�300P@&�ҹ�B�M���-L�L�,BR����B��5R�5cB�\C�'3 endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Roman /Encoding /WinAnsiEncoding >> endobj xref 0 9 0000000000 65535 f 0000000009 00000 n 0000000074 00000 n 0000000120 00000 n 0000000274 00000 n 0000000303 00000 n 0000000462 00000 n 0000000565 00000 n 0000000703 00000 n trailer << /Size 9 /Root 1 0 R /Info 5 0 R /ID[<728657938b76cb1e658d7f5ccfa3c466><728657938b76cb1e658d7f5ccfa3c466>] >> startxref 812 %%EOF

use Dompdf\Dompdf; use Dompdf\Options;

/**
* @Route("/add")
*/

 public function pdf()
{
    $pdfOptions = new Options();
    $pdfOptions->set('defaultFont', 'Arial');

    $dompdf = new Dompdf($pdfOptions);
    $dompdf->loadHtml('Hello world');
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    $dompdf->stream("mypdf.pdf", [
        "Attachment" => false
    ]);
}

2

There are 2 best solutions below

0
On

Same problem here !

Find my way around by adding ob_get_clean(); just before $dompdf->stream('name.pdf');

$html = 'Test';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4');
$dompdf->render();
ob_get_clean();
$dompdf->stream('name.pdf');
0
On

I had the same problem. I fixed this by adding exit(0); after $dompdf->stream();