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

Ligonsker's avatar

How to use TCPDF to load blade views or HTML pages?

Hello,

Is it possible to load a blade view (or any HTML file) with TCPDF in a simple way, similar to laravel-dompdf where you can just do something like:

    $pdf = Pdf::loadView('some_view', $data);
    return $pdf->stream();

Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, it is possible to load a blade view or any HTML file with TCPDF. Here's an example of how to do it:

  1. First, install TCPDF using composer:
composer require tecnickcom/tcpdf
  1. Create a new TCPDF instance and set the document properties:
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('Document Title');
$pdf->SetSubject('Document Subject');
$pdf->SetKeywords('keywords, here');
  1. Add a new page to the PDF document:
$pdf->AddPage();
  1. Load the blade view or HTML file using Laravel's view() function:
$html = view('some_view', $data)->render();
  1. Write the HTML content to the PDF document:
$pdf->writeHTML($html, true, false, true, false, '');
  1. Output the PDF document:
$pdf->Output('document.pdf', 'I');

Here's the complete code:

use TCPDF;

$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Your Name');
$pdf->SetTitle('Document Title');
$pdf->SetSubject('Document Subject');
$pdf->SetKeywords('keywords, here');

$pdf->AddPage();

$html = view('some_view', $data)->render();

$pdf->writeHTML($html, true, false, true, false, '');

$pdf->Output('document.pdf', 'I');

Note: Make sure to include the TCPDF namespace at the top of your file.

Please or to participate in this conversation.