Use Zend PDF to Load Multiple PDFs and Draw on Each One

Discussion in 'PHP' started by klown, Oct 8, 2012.

  1. #1
    I'm trying to use zend pdf to load multiple pdfs from file (each a 1 page pdf) then draw text, images etc. onto each one. Ideally the drawn text and images would be added within a function. However when I try to do some basic draw it comes back with an error in the pdf. The code works perfectly fine until I try draw text onto the page. The Error: Adobe Reader could not Open 'mergedpdf.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an attachment and wasn't correctly decoded).


    require_once 'zendframework/library/Zend/Loader/Autoloader.php';
    include 'form-structures.php'; //
    
    $loader = Zend_Loader_Autoloader::getInstance();
    
    $pdfMerged = new Zend_Pdf();
    
    //load pdf 
    $loadedpdf = Zend_Pdf::load("form-url.pdf");
    
    //clone pdf 
    $page1 = clone $loadedpdf->pages[0];
    
    // add text etc.
    $page1->drawText('Some text...', 400, 500);
    
    drawmore($page1);
    
    //merge pdf
    $pdfMerged->pages[] = $page1;
    
    
    // do again
    $loadedpdf = Zend_Pdf::load("form-url.pdf");
    $page = clone $loadedpdf->pages[0];
    $pdfMerged->pages[] = $page;
    
    echo $pdfMerged->render();
    
    PHP:
    The form-structure.php file is below with a function to draw something extra on the page:

    function drawmore($page)
    {
            $page->drawText("Height is: 600px", 300,800);
            return $page;
    }
    
    PHP:

     
    klown, Oct 8, 2012 IP
  2. klown

    klown Peon

    Messages:
    2,093
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Resolved.

    Zend PDF can't draw text on the screen unless you already specified the font.

    I just added:

    $page1->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20);

    and it fixed the error.
     
    klown, Oct 8, 2012 IP
  3. Drent123

    Drent123 Peon

    Messages:
    105
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Zend pdf used for attaching many file.
     
    Drent123, Oct 9, 2012 IP