dougang1965 2016-02-01 08:56
浏览 97
已采纳

PDF文件无法打开

I would like to open a PDF file in my Symfony 2 project and manipulate the file with FPDI. However, the file seems to be somehow not readable.

Here comes my code:

 public function myPpfFunction()
    {
        $fileLocator = $this->container->get('file_locator');
        $pathTestfile = $fileLocator->locate('@MyBundle/Resources/public/pdf/test.pdf');

        $pdf = new \FPDI();
        $pdf->AddPage();
        $pdf->setSourceFile($pathTestfile);
        $tplIdx = $pdf->importPage(1);
        $pdf->useTemplate($tplIdx, 10, 10, 100);

        $pdf->SetFont('Helvetica');
        $pdf->SetTextColor(255, 0, 0);
        $pdf->SetXY(30, 30);
        $pdf->Write(0, 'This is just a simple text');

        return new Response($pdf->Output(), 200, array(
            'Content-Type' => 'application/pdf'));
    }

The error message:

Cannot open /srv/myproject/src/mycompany/mybundle/Resources/public/pdf/test.pdf !

Even when I want to open the pdf without any bundle, but simple viewing it in the browser with the BinaryFileResponse class, I get the message that the file is not readable. The path must be correct, because with the BinaryFileResponse class I for example can successfully view .txt-files in my browser which are located in just the same folder as in which the pdf file is.

How can I achieve that the pdf is readable? The message occurs for any pdf I want to.

UPDATE:

I just found out that surprisingly it is possible for me to open PDFs that I generated with the FPDF bundle. How can this be possible?

  • 写回答

1条回答 默认 最新

  • douyouming9180 2016-02-01 12:38
    关注

    This must be a permission issue. The error message is raised because of a simply failed fopen() call. So you may try to debug this with some file_exists() or is_readable() calls.

    Furthermore you will need to change the Output() call to:

    return new Response($pdf->Output('S'), 200, array(
            'Content-Type' => 'application/pdf'));
    

    Otherwise FPDF will send the document and headers which seems not to be your plan.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?