Issues

ZF-305: Problem with line endings on Mac

Description

I'm trying to use Zend_Pdf to add some text to an existing pdf but I'm getting the following error:

Fatal error: Uncaught exception 'Zend_Pdf_Exception' with message 'PDF file syntax error. Offset - 0xED6. 'stream' must be followed by either cr-lf sequence or lf character only.' in /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/StringParser.php:592 Stack trace: #0 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/Element/Reference.php(167): Zend_Pdf_StringParser->getObject('19', Object(Zend_Pdf_Element_Reference_Context)) #1 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/Element/Reference.php(128): Zend_Pdf_Element_Reference->_dereference() #2 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/Page.php(389): Zend_Pdf_Element_Reference->getType() #3 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/Page.php(419): Zend_Pdf_Page->flush() #4 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf.php(385): Zend_Pdf_Page->render(Object(Zend_Pdf_ElementFactory)) #5 /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf.php(483): Zend_Pdf->_dumpPages() #6 /Users/tonybrady/PHP/Z in /Users/tonybrady/PHP/ZendFramework-0.1.5/library/Zend/Pdf/StringParser.php on line 592

I've stripped out all code which isn't contributing to the problem and can reproduce the error with the following code snippet:

  $pdf = Zend_Pdf::load('crr0303.pdf');
  $page = $pdf->pages[0];   // only has one page
  // Create new font
  $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
  // Apply font
  $page->setFont($font, 10);
  $pdf->save('new.pdf');

It's the call to save() that triggers the error but removing the setFont() call eliminates the error. I'm on a Mac (OS X 10.4.7) and given the reference to line endings in the exception message I wondered if this is a Mac specific problem?

Comments

I found this same exception occurs if you try to modify a document which has PDF security applied to it.

The class doesn't mind opening and saving the file, but if you perform any actions which will modify it... well, it ends in the dump provided in the previous post.

When a PDF document has security applied, its content streams are encrypted. Those encrypted streams are usually stored as raw binary data, which is likely tripping up the stream parser. Linking to ZF-13 which adds support for PDF security (encryption).

Changing fix version to 0.6.0.

It's not clear, if this is a Mac problem or document encryption support problem. Document example should be provided.

Document example should be provided.

This issue is fixed in 0.9.3 (possibly fixed earlier but I've only just tried it with 0.9.3). I tested it with the pdf that caused the original error and it works perfectly now.

Thanks for feedback!

It looks it was ZF-15 related issue.