0
if (file_exists("folder/$unique_id.pdf")) {
    header('Content-type: application/pdf');
    ob_clean(); // Added after reading similar question's answer
    flush(); // Added after reading similar question's answer
    readfile("folder/$unique_id.pdf");
} else {
    $html   = "PLACING_MY_HTML_CODE_HERE";
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->set_paper("A4");
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents("folder/$unique_id.pdf", $output);

    header('Content-type: application/pdf');
    ob_clean(); // Added after reading similar question's answer
    flush(); // Added after reading similar question's answer
    readfile("folder/$unique_id.pdf");
}

If the PDF file does not exist, the PDF files is generated using DOMPDF. The files is generated without any error and stored inside the folder. But sometimes, instead of reading the generated file, it downloads a file without extension and mostly with name 'download'. I tried opening the file in text editor, and I see symbols with some headers such as application, content-type etc.

Later, searched in SO and saw this question, and added ob_clean, flush in my code. But still the issue persists.

Note: I checked opening the PDF file stored in the folder using Adobe reader and Google Chrome. The files were completely rendered and saved properly.

EDIT:

Instead of readfile I tried header redirect directly to the file. And there are no issues with this.

Any suggestions?

Community
  • 1
  • 1
Sarvap Praharanayuthan
  • 4,212
  • 7
  • 47
  • 72
  • try moving `header('Content-type: application/pdf');` to before the PDF generation – Ali Gangji Apr 06 '14 at 06:26
  • about how much time does it spend generating the PDF before it downloads? – Ali Gangji Apr 06 '14 at 06:27
  • Are you sure if this code works because the function used in the first line in the if-conditional should be **file_exists** – Vagabond Apr 06 '14 at 06:28
  • @Adarsh: It was typo and I corrected it now. Thank you. – Sarvap Praharanayuthan Apr 06 '14 at 06:32
  • @AliGangji: Probably between 2 - 4 seconds. Then the download starts. – Sarvap Praharanayuthan Apr 06 '14 at 06:33
  • It would help to see the actual content in the download when it's wrong. Is there something going on above this code that may be relevant? What about after this code? If you have buffering on then the file will go into the buffer, then PHP execution continue. If you have a buffer limit and if the file is large enough it will trigger the buffer to flush, otherwise it will wait until the PHP is done executing or the buffer is manually flushed. – BrianS Apr 08 '14 at 04:00

2 Answers2

1

I suggest you change your code to this, and see what it does:

header('Content-type: application/pdf');
header('Content-disposition: attachment; filename="'. $unique_id .'.pdf"');
if (!file_exists("folder/$unique_id.pdf")) {
    $html   = "PLACING_MY_HTML_CODE_HERE";
    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->set_paper("A4");
    $dompdf->render();
    $output = $dompdf->output();
    file_put_contents("folder/$unique_id.pdf", $output);
}
header('Content-Length: ' . filesize("folder/$unique_id.pdf"));
readfile("folder/$unique_id.pdf");

First of all, you had a lot of double function calls in there (like header() in both if{} and else{}, readfile in both, etc.) so it is sane to put it outside of the condition since they get executed anyway.

Second, you were missing the file name and the file size parameters in your download.

Also, you did not specify if you wanted to force download of PDF or open it in browser? If you want to force download then change application/pdf to application/octet-stream.

And finally, code this way is easier to debug. If you see that certaing PDF generation is giving you incorrect pdf, all you have to do is comment out all header() calls and see what it gives you in browser. If dompdf spits out some kind of notice, it can render the pdf corrupt and unreadable. When you comment out the header() calls you will see those notices in browser.

dkasipovic
  • 5,930
  • 1
  • 19
  • 25
  • @Kasipovic: Thanks for suggestions. 1. I do not want to force download the file. I just want to open the PDF file in the browser. 2. The PDF file is generated fine and saved in the location properly. I tried opening the file manually using Adobe Reader and Google Chrome. I did not see any errors there. – Sarvap Praharanayuthan Apr 06 '14 at 13:10
  • Have you tried my code with filename and file size? – dkasipovic Apr 06 '14 at 13:11
  • I am altering the code. Let me do that and say you the result. – Sarvap Praharanayuthan Apr 06 '14 at 13:12
  • `header('Content-disposition: attachment; filename="'. $unique_id .'.pdf"');` Adding this header downloads the file. – Sarvap Praharanayuthan Apr 06 '14 at 13:17
  • Commenting out the header does not show any messages in the browser. And I tried header redirect to the PDF file directly, `header("location: folder/$unique_id.pdf")` and this works well. – Sarvap Praharanayuthan Apr 06 '14 at 13:24
  • Well commenting out the headers should have shown at least "garbage" (PDF file). So the problem is probably with `readfile()` then because if you do not see the garbage, it is probably not being output. – dkasipovic Apr 06 '14 at 14:52
0

Change the code inside the else {} to this:

else {

$dompdf = new DOMPDF();

ob_start();    
$temp_text   = "PLACING_MY_HTML_CODE_HERE";

echo $temp_text;

$html = ob_get_contents();
ob_end_clean();

$dompdf->load_html($html);
$dompdf->set_paper("A4");
$dompdf->render();
$output = $dompdf->output();
file_put_contents("folder/$unique_id.pdf", $output);

header('Content-type: application/pdf');
ob_clean(); // Added after reading similar question's answer
flush(); // Added after reading similar question's answer
readfile("folder/$unique_id.pdf");

}

I used this with mpdf and it worked. I hope this will help you too. Let me know the output.

Vagabond
  • 877
  • 1
  • 10
  • 23