简体   繁体   中英

Zend Framework PDF problems

It's me again guys, I have a small problem:

        // Create new PDF 
    $pdf = new Zend_Pdf(); 

    // Add new page to the document 
    $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 
    $pdf->pages[] = $page; 

    // Set font 
    $page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20); 

    // Draw text 
    $page->drawText('Hello world!', 100, 510); 

    $this->getResponse()
         ->setHeader('Content-Disposition', 'attachment; filename=result.pdf')
         ->setHeader('Content-type', 'application/x-pdf');

    echo $pdf->render(); 

When I download the file, and try to open it, i'm getting an error, which sounds like this:

format error: not a PDF or corrupted

My question is: What i'm doing wrong?

If you try to open the file with a text editor (or an hexadecimal editor), what do you get ?

Your PDF file must contain only PDF data, and not any HTML or blank space at the beginning or the end.

One thing that could cause troubles is the automatic rendering of a View by Zend Framework.
Using something like this at the beginning of your action might help :

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();


I've looked at a sample I wrote sometime action, and it's the only difference I see with what you did, actually...


If it still doesn't work, what if you try saving the PDF to a file instead of sending it to the user ? With something like that, for instance :

$pdf->save(CACHE_DIR . '/test-pdf.pdf');

I know it's not what you want to do ; but it would allow you to check if the PDF is well-generated, to identify if the problem is with the PDF generation, or with it's output.


And here is the full example I was talking about :

public function pdfAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $pdf = new Zend_Pdf();
    $pdf->properties['Title'] = "TITLE";
    $pdf->properties['Author'] = "AUTHOR";

    $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
    $width  = $page->getWidth();        // A4 : 595
    $height = $page->getHeight();       // A4 : 842

    $imagePath = WEB_DIR . '/images/logo.png';
    $image = Zend_Pdf_Image::imageWithPath($imagePath);
    $x = 15;
    $y = $height - 15 - 106/2;
    $page->drawImage($image, $x, $y, $x+155/2, $y+106/2);

    $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    $page->setFont($font, 36);

    $page->drawText('Hello world!', 72, 720, 'UTF-8');

    $pdf->pages[] = $page;

    $this->getResponse()->setHeader('Content-type', 'application/x-pdf', true);
    $this->getResponse()->setHeader('Content-disposition', 'inline; filename=my-file.pdf', true);
    $this->getResponse()->setBody($pdf->render());
}

As far as I remember, this was working fine a couple of months ago ; and the only differences with your code are :

  • disabling the layout / rendering
  • using a logo ; but that shouldn't do much of a difference


Hope this helps ; Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM