Save PDF file with Dompdf

pavlenko picture pavlenko · Sep 3, 2015 · Viewed 9.7k times · Source

using Dompdf to store data in pdf file:

This function work fine :

$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1> ');
return $pdf->stream();

Now,when try

$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML('<h1>Test</h1> ');
file_put_contents("test.pdf", $pdf->output());

Get error:

file_put_contents(test.pdf): failed to open stream: Permission denied

Do I need to create some extra folder for saving file or something ?

Tnx, P

Answer

Pedro Lobito picture Pedro Lobito · Apr 29, 2017

To save the generated pdf to a file, use output(), i.e.:

$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>hello world</h1>');
$output = $dompdf->output();
file_put_contents('filename.pdf', $output);