Zend Framework PDF problems

眉间皱痕 提交于 2019-12-23 17:14:26

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/1329302/zend-framework-pdf-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!