ZF-11666: Zend_Pdf renders corrupt file after serialize-unserialize

Description

After serializing and unserializing a Zend_Pdf object with pages everything seems ok until you add additional pages to the object. Rendered PDF is corrupt, it contains unreadable text (font information missing?), blank pages appear and pages are repeated randomly.

sniplet to reproduce behauviour:


if (!isset($_SESSION['PDF'])) {

    $pdf = new Zend_Pdf();

    $pdf->pages[] = $objPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $objPage->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 16);
    $objPage->drawText('Page ' . count($pdf->pages), 100, 150);

    $pdf->pages[] = $objPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $objPage->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 16);
    $objPage->drawText('Page ' . count($pdf->pages), 100, 150);

    $_SESSION['PDF'] = serialize($pdf);
    echo 'object serialized';
} else {

    $pdf = unserialize($_SESSION['PDF']);

    $pdf->pages[] = $objPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $objPage->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 16);
    $objPage->drawText('Page ' . count($pdf->pages), 100, 150);

    $pdf->pages[] = $objPage = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
    $objPage->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 16);
    $objPage->drawText('Page ' . count($pdf->pages), 100, 150);

    header('Content-type: application/pdf');
    echo $pdf->render();

    unset($_SESSION['PDF']);
}

Comments

Switching to major improvement type - an object not being serializable is not a bug. There's never any such guarantee. I would instead use a container object to port the necessary data around in sessions, and use that to repopulate a new Zend_Pdf object as needed.

@[~padraic]: Should this issue be marked as postponed to next major release, then?...or should this also be implemented in ZFv1?

Closing as Won't Fix for ZF1. [~padraic] has provided a workaround which should work for all cases.