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

bruno101's avatar

pdf error with dompdf in Laravel

I am trying to generate a PDF with dompdf in Laravel 5.1 but I have a problem when it loads the html content. Apparently it does not finish loading complete parts that I add to the html content from Javascript using: $('#contenido_tabla').append(insert_table); When I perform a return $view; in my controller before generating the pdf, everything seems to be fine, it loads the content correctly. The problem arises when you do the loadHTML. The mentioned content no longer appears. Perhaps I am making a mistake or perhaps it is not possible due to some factor, I am attentive to any collaboration, thanks in advance.

It doesn't seem to let me load javasript in my view

0 likes
8 replies
vincent15000's avatar

Please write the code between backticks, otherwise it is unreadable.

```

your code here

```

Are you using a CSS framework ?

bruno101's avatar

@vincent15000

$datos = Datos::find($id);
$view =  view('pdf/info_pdf', ['datos'=>$datos,'request'=>$request])->render();  
//return $view;
$pdf = \App::make('dompdf.wrapper'); 
$pdf->loadHTML($view)->setPaper('letter', 'portrait');  
return $pdf->stream('info.pdf');

This is my controller, when I perform the $pdf->loadHTML no carga el javascript de la vista

1 like
bruno101's avatar

@shadkamel Could you guide me better, I'm new to this.

In the DOMPDF documentation it mentions that enabling the option of "enable_javascript" => true but it doesn't work either

1 like
Quiet_Molly's avatar

I can suggest you using Barryvdh package. It also uses DomPDF.

With it you create simple pdf which can get data and send it to the view like you normally would do with controller

use Barryvdh\DomPDF\Facade\Pdf;

class PDFController extends Controller
{
    function firstPDF(){
        $data =
        [
            'title' => 'Welcome to Tutsmake.com',
            'date' => date('m/d/Y')
        ];

        $pdf = PDF::loadView('testPDF', $data);

        return $pdf->download('tutsmake.pdf');
    }
}

Create route for it

Route::get('/firstPDF', [App\Http\Controllers\PDFController::class, 'firstPDF'])->name('manage/firstPDF');

and then view that you previously mentioned:

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Generate PDF From View</title>
</head>
<body>
    <h1>{{ $title }}</h1>
    <p>{{ $date }}</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris.</p>
</body>
</html>


To call that function, you could just call your function f.e. with button
1 like
bruno101's avatar

@Quiet_Molly I can't add javascript content to the view, I need to do it because I have a dynamic table

1 like
bruno101's avatar

@Quiet_Molly In my view it won't let me load something as simple as this

<!DOCTYPE html>
 <html>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <body>
       <h1 id="myH1"></h1>
       <script type="text/javascript">
           document.getElementById("myH1").innerHTML="new date";
      </script>
   </body>
 </html>
1 like

Please or to participate in this conversation.