错误打开PDF Laravel(在浏览器中未正确显示)



我在Laravel中的目录打开现有的PDF问题。该代码与纯PHP完美搭配,但是将其带到Laravel无法正常工作,因为它在浏览器中未正确显示,看起来像这样:

%pdf-1.5 3 0 obj<>/目录4 0 r>> endobj 4 0 obj<>流x} x} | nae。�= .....

代码如下:

//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();

可能是什么问题以及如何解决?

您应该使用类似的东西:

而不是$pdf->Output();

使用:

$pdfContent = $pdf->Output('', "S");

将内容保存到字符串

,然后返回响应:

    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'
        ]
    );

最新更新