Search code examples
phpsymfonypdfhtml2pdf

html2pdf: how to show the pdf (to allow the user to save it) and save it on the server


How your doing? Good! Today I'm looking to use html2pdf. It works pretty well and I can show/save the pdf but I can't do both! That's a problem and I don't know what should I do. I'm using the library with symfony and this code works:

ob_start();
include(dirname(__FILE__).'/feuille.php');
$content = ob_get_clean();
$html2pdf = new \Html2Pdf_Html2Pdf('P','A4','fr');
$html2pdf->pdf->SetDisplayMode('real');
$html2pdf->WriteHTML($content);

// $result = $html2pdf->Output($_POST["fournisseur"].".pdf", 'F'); //save on server
$result = $html2pdf->Output($_POST["fournisseur"].".pdf"); //show the page on browser
exit();

The commented code works only if I comment the following one... So, do you have any solution to allow both of those line to work together?

Thank you for your help!

PS: it also only works if I add the exit() function at the end, so if anyone knows something about it, may he speaks! :)


Solution

  • You Can Use following method...

    $result = $html2pdf->Output($_POST["fournisseur"].".pdf", 'F');
    header("Content-type:application/pdf");
    echo file_get_contents($_POST["fournisseur"].".pdf");