Reputation: 4360
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?
Upvotes: 0
Views: 4801
Reputation: 6120
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.
Upvotes: 1
Reputation: 897
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.
Upvotes: 0