Problems with the Zend Framework PDF

This again I guys, I have a little 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 upload a file and try to open it, I get an error message that sounds like this:

format error: not pdf or damaged

My question is: What am I doing wrong?

+3
source share
1 answer

If you try to open the file using a text editor (or a hex editor), what will you get?

Your PDF should contain only PDF data, not HTML or white space at the beginning or end.

, , Zend Framework.
- :

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


, -, , , , ...


- , , PDF ? - , :

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

, , ; , PDF , , PDF .


, :

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());
}

, ; :

  • /
  • ; .


; !

+5

Source: https://habr.com/ru/post/1716009/


All Articles