ZF-10820: PDF's created with Zend_Pdf::save($file, true) won't open in Adobe Reader

Issue Type: Bug Created: 2010-12-13T09:40:19.000+0000 Last Updated: 2011-02-17T09:02:06.000+0000 Status: Resolved Fix version(s): - 1.11.4 (03/Mar/11)

Reporter: James Logsdon (jlogsdon) Assignee: Alexander Veremyev (alexander) Tags: - Zend_Pdf

Related issues: Attachments: - Pdf.php.diff


We have a report that generates large PDFs (almost 1,000 pages on our largest) that we've been working on cleaning up and fixing memory issues on. One of the biggest things we can change is incrementally saving the PDF instead of collecting all of the pages in memory before saving. Unfortunately every PDF we've created with the $updateOnly flag set will not open in Adobe Reader or OSX's Interestingly enough the PDF viewer evince on Linux displays the file correctly.

The error message from Reader is: Adobe Reader could not open 'test.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

<pre class="highlight">

set_include_path(__DIR__ . '/library');

require './library/Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();

if (is_file($file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'test.pdf')) {

$pdf = new Zend_Pdf();
for ($i = 0; $i < 5; $i++) {
    $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER), 7);

    $page->drawText('Test Page ' . ($i + 1), 25, $page->getHeight() - 25);
    $pdf->pages[] = $page;
    $pdf->save($file, true);



Posted by James Logsdon (jlogsdon) on 2010-12-13T12:31:19.000+0000

I have found the solution! Setting the $updateOnly flag on a brand new file does not work. I expected the library to be smart enough to realize it's a new file and operate on it as such. If this is intended behavior I believe it should be documented somewhere.

Posted by Christian Wenz (wenz) on 2010-12-16T04:42:27.000+0000

I don't know if this is considered a breaking change, but IMVHO it would make sense to check whether the target file already exists before appending to it. The write mode (ab vs. wb) does not matter, but the flag value passed to the render() method does. I have prepared a simple patch that changes the $updateOnly flag to false if the target file does not exist.

Posted by Christian Wenz (wenz) on 2010-12-16T04:51:05.000+0000

Patch for ZF-10820

Posted by Alexander Veremyev (alexander) on 2011-02-17T09:02:04.000+0000


PDF is always saved as full document (not only incremental part) if it's created using:

<pre class="highlight">
$pdf = new Zend_Pdf();

but if document is loaded it may be saved as incremental section for different purposes, it also allows to work with streams or other environments where file_exists() is not relevant.

Have you found an issue?

See the Overview section for more details.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.