user2809321 user2809321 - 1 month ago 15
PHP Question

DOMPDF, able to create pdf doc but the pdf itself failed to load

Hi I'm trying to use php to covert html to pdf. The code is able to run and I able to run it and download a pdf file but when I open the pdf file it gives me this error

enter image description here

This is the code I have to run the php

<?
require_once("dompdf/dompdf_config.inc.php");

$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");



?>


I have a feeling that I'm loading the html wrong so the contents of the pdf is actually messing up, any ideas?

Answer
   <?
^^^---- these spaces will corrupt the PDF
require_once("dompdf/dompdf_config.inc.php");

If you really do have those 3 spaces before your PHP opening tag, they will become part of the PDF file and corrupt it. Load up your "bad" pdf, do a file->save, and opening the file in a text/hex editor (NOT a pdf reader) and see what you're getting. Any PHP errors/warnings or other output before the actual PDF data will cause the PDF readers to see it as corrupted.