Store HTML content in a pdf

Published 2 months ago by adamjhn

I have this code to get the content of a certificate:

$certificate = RegistrationType::find($request->send_to)->certificate;
$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);
dd($certificateContent->content);

The content is stored in the "content" column of the certificates table, can be for example:

<p>certificate content</p>

Do you know how put this content in a pdf using the laravel-dompdf?

To put the content in a view in a pdf can be like below but Im not understanding how to put the html content stored in the content column into a pdf.

$pdf = PDF::loadView('pdf.invoice', compact('invoice'));
return $pdf->download('invoice.pdf');


Best Answer (As Selected By adamjhn)
lostdreamer_nl

I think you're not using the facade, but the PDF class directly.

// config/app.php
'PDF' => Barryvdh\DomPDF\Facade::class,

And use the facade like this (note the \ in front)

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);

$pdf = \PDF::loadHTML($certificateContent);
return $pdf->download('invoice.pdf');

Or use the class directly via app()->make

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);

$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML($certificateContent);
return $pdf->download('invoice.pdf');

Procat

Reading Barryvdh's github page I would assume PDF::loadHTML() would to the trick.

adamjhn

Thanks, like that it shows "Non-static method Barryvdh\DomPDF\PDF::loadHTML() should not be called statically".

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);

        $pdf = PDF::loadHTML($certificateContent);
lostdreamer_nl

I think you're not using the facade, but the PDF class directly.

// config/app.php
'PDF' => Barryvdh\DomPDF\Facade::class,

And use the facade like this (note the \ in front)

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);

$pdf = \PDF::loadHTML($certificateContent);
return $pdf->download('invoice.pdf');

Or use the class directly via app()->make

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to);

$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML($certificateContent);
return $pdf->download('invoice.pdf');

adamjhn

Thanks, but like that it shows "Class 'App\Http\Controllers\App' not found". Is necessary to import "use Illuminate\Support\Facades\App; "? Importing that it shows "Object of class Illuminate\Database\Eloquent\Builder could not be converted to string ".

adamjhn

Thanks, but it appears always "Object of class Illuminate\Database\Eloquent\Builder could not be converted to string".

I have this in app.php "'PDF' => Barryvdh\DomPDF\Facade::class,".

lostdreamer_nl

Sorry, was sleeping:

$certificateContent = RegistrationType::with('certificate')->find($request->send_to);

$pdf = app()->make('dompdf.wrapper');
$pdf->loadHTML($certificateContent->content);
return $pdf->download('invoice.pdf');

If you want to use a facade, start it with a \ ( read up on namespacing to see why ) Most of the times, I use the helper functions instead of facades (app()->make() instead of \App::make() )

adamjhn

Thanks, now dont appears any error, but the pdf is blank, dont have the certificate content.

The certificate content is html like

<p>test content</p>
adamjhn

It shows a blank pdf because I was using

    $pdf->loadHTML($certificateContet);

instead of

 $pdf->loadHTML($certificateContent->content);

But with " $pdf->loadHTML($certificateContent->content);" shows "Trying to get property of non-object"..

eriktobben

Did you forget to add ->first() at the end?

$certificateContent = RegistrationType::with('certificate')->where('id', $request->send_to)->first();
adamjhn

Thanks, but still appear a blank pdf.

Please sign in or create an account to participate in this conversation.