Valentin BEAULE
Valentin BEAULE

Reputation: 404

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! :)

Upvotes: 0

Views: 11113

Answers (5)

Ashish
Ashish

Reputation: 332

You Can Use following method...

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

Upvotes: 3

Alex O.
Alex O.

Reputation: 1192

In symfony2 we use such code to show pdf to use

return new Response(file_get_contents($fileData['path']), 200, array('Content-Type' => 'application/pdf'));

For download file

$response = new Response(file_get_contents($path));
$response->setStatusCode(200);
$response->headers->set('Content-Type', mime_content_type($path));
$response->headers->set(
    'Set-Cookie',
    'fileDownload=true; path=/');
$response->headers->set(
    'Content-Disposition',
    sprintf('attachment;filename="%s"', utf8_decode($name))
);

$response->send();

Upvotes: 0

Rachel Gallen
Rachel Gallen

Reputation: 28563

or you could try this

$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('example.pdf');

Upvotes: 0

Ballantine
Ballantine

Reputation: 338

If you use the output function without parameter, you will be able to see the PDF, without saving it.

You can add the second function $html2pdf->Output($_POST["fournisseur"].".pdf", 'F') to save the PDF on your disk.

About the exit, it may come from your ob_start() call. This function open a response buffer, without sending it. The html2pdf output function don't need ob_start function ;)

Upvotes: 0

user3769916
user3769916

Reputation: 57

i have use something similar library before, tcpdf.

i just save the file in server, after that echo a link to browser or redirect user with php.

Upvotes: 0

Related Questions