Is it possible to render once, save to disk and then stream the file to the user?

121 views
Skip to first unread message

notuo

unread,
May 28, 2014, 3:32:27 PM5/28/14
to dom...@googlegroups.com
I have this process and I can save the pdf in order to send it via email:

  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $pdfcontent = $dompdf->output();
  $rslt = file_put_contents("../PDFs/" . $row['filename'] . ".pdf", $pdfcontent);

I have another option to just render the pdf and stream to the user screen for him to save it.

  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $dompdf->stream($pdf_name);

Is there a way to have both together in one render to save time?  ie something like this (is not working for me)

  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->render();
  $pdfcontent = $dompdf->output();
  $rslt = file_put_contents("../PDFs/" . $row['filename'] . ".pdf", $pdfcontent);
  $dompdf->stream($pdf_name);

The stream part loose some fonts in the last case

Thanks for any tips,

BrianS

unread,
Jun 3, 2014, 5:44:37 PM6/3/14
to dom...@googlegroups.com
I believe it should be possible, though I haven't tried it in a while. It's possible that there's a conflict when calling output then stream (which would be the correct order), I'd have to refresh myself with the relevant code.

That being said, once you output the PDF you have everything you need to stream. If the saved PDF is web accessible you could just redirect the user to that PDF. If it's not you could send the captured output to the browser, you would just have to supply the necessary headers yourself (look at the CPDF stream method to see what we use).
Reply all
Reply to author
Forward
0 new messages