Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and download PDF

Tags:

php

dompdf

I need to do 2 things together & at the same time with DOMPDF.

I need to do the following together - is this possible?

//print the pdf file to the screen for saving
$dompdf->stream("pdf_filename_".rand(10,1000).".pdf", array("Attachment" => false));
//save the pdf file on the server
file_put_contents('/home/stsnew/public_html/pdf/file.pdf', $dompdf->output()); 

The above works fine if $dompdf->stream and $dompdf->output() are done separately/individually , but when I try to run them together as shown above, it just crashes.

Any help/advice would be really appreciated.

like image 441
user991830 Avatar asked Oct 12 '11 16:10

user991830


2 Answers

Why dont you swap them round create the pdf first as a file and then stream the created file back ?

$file_to_save = '/home/stsnew/public_html/pdf/file.pdf';
//save the pdf file on the server
file_put_contents($file_to_save, $dompdf->output()); 
//print the pdf file to the screen for saving
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="file.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file_to_save));
header('Accept-Ranges: bytes');
readfile($file_to_save);
like image 134
Manse Avatar answered Sep 26 '22 23:09

Manse


Answering late but might be will help onward.

on set attachment true or 1

$dompdf->stream("pdf_filename_".rand(10,1000).".pdf", array("Attachment" => true));

No need of file_put_contents() if you are just looking for download

like image 30
Farhan Avatar answered Sep 22 '22 23:09

Farhan