Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pavlen's avatar

Save PDF file with Dompdf

Hi,

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

0 likes
5 replies
pavlen's avatar

Solved on this way :

 return PDF::loadHTML('<h1>Test</h1> ')->save('/path//my_stored_file.pdf');

sghimire's avatar

Is there anyway it can be saved in the database?

sghimire's avatar

That's what I am doing right now. I want to save it to the database.

view()->share('lab',$data);
    $pdf = PDF::loadView('lab.test');
    return $pdf->download('test_'. $request['Id'] . '_lab.pdf');
cmdobueno's avatar

@Raian14 you should not save a pdf directly to the database, but instead save the path to the pdf in the database

example:

$my_pdf_path_for_example = 'my/really/cool/path/' . str_random(25) . '.pdf';
PDF::loadHTML('<h1>Test</h1> ')->save($my_pdf_path_for_example );
$model = SomeCoolModel::find(request('id') );
$model->path_to_pdf = $my_pdf_path_for_example ;
$model->save();
return response()
    ->download($my_pdf_path_for_example );

Obviously this is a rough example... do as you will with it... i just threw it together really fast, so there are probably errors, but you get the concepts.

1 like

Please or to participate in this conversation.