打开pdf laravel时出错(在浏览器中无法正确显示)

时间:2015-05-03 08:21:15

标签: php pdf laravel blade laravel-response

我在laravel目录中打开现有pdf时遇到问题。代码与纯PHP完美配合,但由于它在浏览器中无法正确显示,因此无法正常工作,它看起来像这样:

%PDF-1.5 3 0 obj<> /内容4 0 R>> endobj 4 0 obj<>流x } NAE 1 E= bq~ I = .....

代码如下:

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

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

1 个答案:

答案 0 :(得分:1)

你应该使用这样的东西:

而不是$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'
        ]
    );