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:
- First, install TCPDF using composer:
composer require tecnickcom/tcpdf
- 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');
- Add a new page to the PDF document:
$pdf->AddPage();
- Load the blade view or HTML file using Laravel's view() function:
$html = view('some_view', $data)->render();
- Write the HTML content to the PDF document:
$pdf->writeHTML($html, true, false, true, false, '');
- 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.