user2809321
user2809321

Reputation: 204

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?

Upvotes: 1

Views: 20623

Answers (9)

Md Afsar Uddin
Md Afsar Uddin

Reputation: 33

Use ob_end_clean(); before $dompdf -> stream("", array("Attachment" => false)); Hope, it will work.

Upvotes: -1

the best solution i found so far is to open the pdf with some text editor , then you can see the error causing the file not being able to open, and you can fix it then, and one of the main raisons that may cause the error is the path to the html template

Upvotes: -2

It has to do with your php and xampp version. changed to xampp version 3.2.1 (php version 5.2) and it worked.

Upvotes: -2

Kaloy
Kaloy

Reputation: 141

Use ob_end_clean(); before calling $dompdf->stream();.

Source: https://stackoverflow.com/a/44318219/11272475

Upvotes: 10

Akula
Akula

Reputation: 3

This can happen as well when we have php warnings when generating the pdf document.

Setting error_reporting(0) on specific php file generating the pdf file is a remedy. Fixing the warning or having conditions to handle it is an even better solution

Upvotes: -2

Devendra Sharma
Devendra Sharma

Reputation: 163

I was facing same issue "pdf document is damaged".

Finally I find the solution.

First open pdf in text editor like(gedit) then I seen some additional style(css).After that i find that and comment it.now again download pdf and its work fine.

<style>
#certificatrediv > div{margin:0 auto !important;}
</style>
<h2>Test PDF</h2>

Upvotes: -2

David de Tena
David de Tena

Reputation: 69

I had the same problem and the browser was not able to display the pdf file (image). After researching a lot over here and Google and a lot of test I've been able to make it work with this line:

$dompdf->loadHtml(html_entity_decode($html));

Hope it works for you, too.

Upvotes: -1

user4258584
user4258584

Reputation:

DOMPDF requires you to set a default_timezone for your php app. it cannot rely on system's timezone settings

Upvotes: -2

Marc B
Marc B

Reputation: 360872

   <?
^^^---- 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.

Upvotes: 10

Related Questions