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

KikoLdasd's avatar

It s possible to save pdf on the server with spatie media library and DOM PDF

Hi, I'm trying to save a pdf on the server after I load it with DOM pdf but I can't How can I save a pdf when I upload it to a server? I tried something like that

 #[NoReturn] public function downloadReportPdf(Reports $report)
    {
        $pdf = Pdf::loadView('pdf.reports', ['report' => $report]);
        $report->addMedia($pdf->output())->toMediaCollection('docs');
        //dd($file);
        $pdf->download();
    }

Is it possible for me to do something from scratch?

0 likes
1 reply
Niush's avatar

I am not sure if the output() function returns string or base64 or stream.

But, with spatie media library, you also get these functions. One of that should work, depending on what dompdf returns.

$report->addMediaFromString($pdf->output())->toMediaCollection('docs');
// OR
$report->addMediaFromStream($pdf->output())->toMediaCollection('docs');
// OR
$report->addMediaFromBase64($pdf->output(), 'application/pdf')->toMediaCollection('docs');

My bet is on String. https://spatie.be/docs/laravel-medialibrary/v10/api/adding-files#content-addmediafromstring

1 like

Please or to participate in this conversation.