【问题标题】:Error opening pdf laravel (not display correctly in browser)打开 pdf laravel 时出错(在浏览器中无法正确显示)
【发布时间】:2015-05-03 08:21:14
【问题描述】:

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

%PDF-1.5 3 0 obj /Contents 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();

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

【问题讨论】:

  • 您的输出看起来好像浏览器没有意识到它是一个 PDF 文件。您是否发送了正确的 mime 类型?
  • 其实不是;我从表单发送我需要的数据,只需 我在 routes.php Route :: post ( '/ evaluacion_PDF' 'HomeController@evaluacion_pdf') 和我的控制器 evaluacion_pdf public function () { View :: make return ('frontend.evaluacion_pdf'); }
  • 我不知道在哪里添加 pdf mime 类型,如果你能指导我一下,将不胜感激
  • 你需要设置content-type:stackoverflow.com/a/18685427/955026我觉得你需要的是application/pdf的类型

标签: php pdf laravel blade laravel-response


【解决方案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'
        ]
    );

【讨论】:

  • woooow 非常感谢你,这对我来说非常完美
猜你喜欢
  • 2013-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多