I am having problems opening an existing pdf from a directory in laravel. The code works perfectly with pure PHP, but taking it to laravel not work properly because it does not display correctly in the browser, it looks like this:
%PDF-1.5 3 0 obj <> /Contents 4 0 R>> endobj 4 0 obj <> stream x�}��NAE���1�E=��bq~�I�� ��= .....
the code is as follows:
//eval_pdf.blade.php
File::requireOnce('fpdf.php');
File::requireOnce('fpdi.php');
$pdf = new FPDI('P','mm','Letter');
$pdf->AddPage();
$pdf->SetAutoPageBreak(true,10);
$sourceFileName = app_path().'/include/formato.pdf';
$pdf->setSourceFile($sourceFileName);
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('helvetica','B',8);
.
.
.
$pdf->SetXY(86, 21);
$pdf->Write(0, utf8_decode($comp));
.
.
.
$pdf->Output();
What could be the problem and how to solve it?
You should use something like this:
instead of $pdf->Output();
use:
$pdfContent = $pdf->Output('', "S");
to save content into string
and then return response for example:
return response($pdfContent, 200,
[
'Content-Type' => 'application/pdf',
'Content-Length' => strlen($pdfContent),
'Content-Disposition' => 'attachment; filename="mypdf.pdf"',
'Cache-Control' => 'private, max-age=0, must-revalidate',
'Pragma' => 'public'
]
);