Example #1
0
 /**
  * test to combine Zend_Pdf content with tcpdf pdfs
  *
  * @param $observer
  */
 public function adjustPdf($observer)
 {
     $extractor = new Zend_Pdf_Resource_Extractor();
     $pdf = $observer->getEvent()->getPdf();
     $counter = false;
     foreach ($pdf->pages as $key => &$page) {
         if ($page instanceof Fooman_PdfCustomiser_Model_Abstract) {
             $counter = 1;
             $instance = $page;
             $firstKey = $key;
             unset($pdf->pages[$key]);
         } elseif ($counter == 1) {
             $objectArray = $page;
             $counter++;
             unset($pdf->pages[$key]);
         } elseif ($counter == 2) {
             $orderIds = $page;
             $tcpdf = Zend_Pdf::parse($instance->renderPdf($objectArray, $orderIds, null, true));
             foreach ($tcpdf->pages as $p) {
                 $pdf->pages[$firstKey] = $extractor->clonePage($p);
             }
             $counter = 0;
         }
     }
 }
Example #2
0
 /**
  * keep compatible with the Zend_Pdf way of doing things
  * collect input for later processing with render()
  *
  * @param array $input    array of items to print
  * @param array $orderIds array of order ids
  *
  * @return Fooman_PdfCustomiser_Model_Abstract | Zend_Pdf
  */
 public function getPdf($input = array(), $orderIds = null)
 {
     if (self::COMPAT_MODE) {
         try {
             $newPdf = new Zend_Pdf();
             $extractor = new Zend_Pdf_Resource_Extractor();
             if (!empty($orderIds)) {
                 $origPdf = $this->renderPdf(null, $orderIds, null, true);
             } else {
                 $origPdf = $this->renderPdf($input, $orderIds, null, true);
             }
             if ($origPdf->getPdfAnyOutput()) {
                 $pdfString = $origPdf->Output('output.pdf', 'S');
                 $tcpdf = Zend_Pdf::parse($pdfString);
                 foreach ($tcpdf->pages as $p) {
                     $newPdf->pages[] = $extractor->clonePage($p);
                 }
             }
             return $newPdf;
         } catch (Exception $e) {
             Mage::logException($e);
         }
     } else {
         $this->pages[] = array('instance' => $this, 'objectArray' => $input, 'orderIds' => $orderIds);
         return $this;
     }
 }
/**
 * Extract selected pages from PDF files using Zend_Pdf library.
 * @param object $pdfparts
 * @param object $file
 * @return string extracted pages in one PDF file (suitable for direct output to browser or saveing to disk)
 */
function pdfparse_pdf_zend_extract($pdfparts, $file)
{
    global $CFG;
    try {
        require_once "{$CFG->dirroot}/mod/pdfparts/Zend/Pdf.php";
        // load PDF document from file contents
        $pdf = Zend_Pdf::parse(trim($file->get_content()));
        $pdfpagecount = count($pdf->pages);
        // parse page range
        $pagerange = pdfparts_parse_page_range($pdfparts, $pdfpagecount);
        // new PDF for output
        $outpdf = new Zend_Pdf();
        // new PDF page extractor
        $extractor = new Zend_Pdf_Resource_Extractor();
        // good example: http://framework.zend.com/manual/1.12/en/zend.pdf.pages.html#zend.pdf.pages.cloning
        foreach ($pagerange as $value) {
            if (is_int($value)) {
                $outpdf->pages[] = $extractor->clonePage($pdf->pages[$value - 1]);
            } elseif (is_array($value)) {
                for ($k = $value[0]; $k <= $value[1]; $k++) {
                    $outpdf->pages[] = $extractor->clonePage($pdf->pages[$k - 1]);
                }
            }
        }
        // get PDF document as a string
        $pdfdata = $outpdf->render();
        return $pdfdata;
    } catch (Zend_Pdf_Exception $e) {
        print_error("Could not process the PDF file aosciated with this instance of the PDFParts module.");
    } catch (Exception $e) {
        print_error("Unknown error while processing the PDF file aosciated with this instance of the PDFParts module.");
        return null;
    }
}