Max
Max

Reputation: 860

PDF file cannot be opened

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?

Upvotes: 0

Views: 3037

Answers (1)

Jan Slabon
Jan Slabon

Reputation: 5058

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.

Upvotes: 1

Related Questions