title: Zend Framework Render Barcodes Into Multiple PDF Pages with other content
body: <p>I am trying to create pages of incrementing labels that include barcodes.  I can get a barcode into a PDF, and I can get it superimposed on other content in the PDF (see below). But I can't figure out how to assign a barcode to a certain page of a pdf using something maybe like </p>
  
  <pre><code>Zend_Barcode::factory('code39', 'pdf',
  $barcodeOptions, $rendererOptions)-&gt;setResource($page)-&gt;draw();
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$page = Zend_Barcode::factory('code39', 'pdf', $barcodeOptions,
  $rendererOptions)-&gt;setResource($pdf)-&gt;draw();
  </code></pre>
  
  <p>Both of the above snippets cause errors when set in the context of my larger code.  Further down is code that renders with no errors, but doesn't give me what I need.</p>
  
  <p>This question is identical to <a href="https://stackoverflow.com/questions/9049046/zend-framework-render-barcodes-into-pdf-pages">Zend Framework Render Barcodes Into PDF Pages</a>, which never reached a working answer from what I can see.</p>
  
  <p>First, to get this to work, I copied the Zend version 1.11 Barcode library into my Zend version 1.7.2 library folder.  I also copied a free non-monospaced font into my application/library as the code shows.</p>
  
  <p>I can create a multi-page PDF of labels.  And I can put a barcode in a PDF.  What I can't do is put barcodes on multiple pages.  I'll show what I am doing, and hopefully you can tell me what I am doing wrong or need to do.</p>
  
  <p>First I have this little snippet that puts a barcode in a PDF:</p>
  
  <pre><code>// A font path is mandatory for Barcode Pdf output.  
  // This application defines ROOT_DIR in index.php.  Others may define APPLICATION_PATH.
  // Monospaced fonts apparently don't work here.
  $barcodeOptions = array(
      'text' =&gt; '11111',
      'font' =&gt; ROOT_DIR . '/library/Rbs/Barcode/FreeSerif.ttf'
  );
  $rendererOptions = array(
      'topOffset' =&gt; 50,
      'leftOffset' =&gt; 50
  );
  Zend_Barcode::factory('code39', 'pdf',
  $barcodeOptions, $rendererOptions)-&gt;setResource($pdf)-&gt;draw();
  </code></pre>
  
  <p>Then I have this (long) code that creates labels. I've commented below near the bottom the two main places I have experimented putting the barcode, but the core problem seems to be that I don't know how to put a barcode on a certain page of a PDF:</p>
  
  <pre><code>    public function labelsAction()
      {
  
          $request['starting_label_number'] = '100001';
          $request['label_count'] = 32;
  
          $this-&gt;_helper-&gt;layout-&gt;disableLayout();
          $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender();
  
          $filename= 'files/inventory_labels.pdf';
          $form['units_name'] = 'inches';
          $form['units_factor'] = 72;
          $form['margin_bottom'] = 0.5;
          $form['margin_left'] = 0.19;
          $form['label_width'] = 2.625;
          $form['label_height'] = 1.0;
          $form['label_spacing_column'] = 0.125;
          $form['label_margin'] = 0.1;
          $form['label_column_count'] = 3;
          $form['label_row_count'] = 10;
  
          try 
          {
              // create PDF
              $pdf = new Zend_Pdf();
  
              // define font resource
              $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ROMAN);
  
              $total_pages = ceil($request['label_count'] / ($form['label_column_count'] * $form['label_row_count']));
              $current_label_number = $request['starting_label_number'];
  
              // Create each page
              for ($page_number = 1; $page_number &lt;= $total_pages; $page_number++)
              {
  
                  // create A4 page
                  $page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_LETTER);
  
                  // write text to page
                  // Fill up each page with labels or blanks
                  for ($row = 1; $row &lt;= $form['label_row_count']; $row++)
                  {
                      for ($column = 1; $column &lt;= $form['label_column_count']; $column++)
                      {
                          if ($current_label_number - $request['starting_label_number'] &lt; $request['label_count'])
                          {
  
                              $label_center_y = 
                                  (
                                      $form['margin_bottom'] 
                                      + ($form['label_row_count'] - $row + 0.5) * $form['label_height']
                                  ) * $form['units_factor'];
                              $label_center_x = 
                                  (
                                      $form['margin_left'] 
                                      + ($column - 0.5) * $form['label_width'] 
                                      + ($column - 1) * $form['label_spacing_column']
                                  ) * $form['units_factor'];
  
                              // Get our bearings $xleft, $ybottom, $xright, $ytop, $filltype
                              $page-&gt;drawRectangle(
                                  $label_center_x - $form['label_width'] * $form['units_factor'] / 2, 
                                  $label_center_y - $form['label_height'] * $form['units_factor'] / 2,
                                  $label_center_x + $form['label_width'] * $form['units_factor'] / 2, 
                                  $label_center_y + $form['label_height'] * $form['units_factor'] / 2,
                                  Zend_Pdf_Page::SHAPE_DRAW_STROKE
                              );
  
                              // define image resource
                              $image = Zend_Pdf_Image::imageWithPath('images/new/logo.jpg');
  
                              // write image to page $xleft, $ybottom, $xright, $ytop
                              $image_width = 125;
                              $image_height = 30;
                              $image_y_offset = 13;
                              $page-&gt;drawImage(
                                  $image, 
                                  $label_center_x - $image_width / 2, 
                                  $label_center_y - $image_height / 2 + $image_y_offset,
                                  $label_center_x + $image_width / 2, 
                                  $label_center_y + $image_height / 2 + $image_y_offset
                              );
                              $text_width = 108;
                              $page-&gt;setFont($font, 10)
                                 -&gt;drawText('www.pristineauction.com', $label_center_x - $text_width / 2, $label_center_y - 10);
                              $text_width = 68;
                              $page-&gt;setFont($font, 22)
                                 -&gt;drawText($current_label_number, $label_center_x - $text_width / 2, $label_center_y - 32);
                              $current_label_number += 1;
                          }
                      }
                  }
  
  /* If I insert the barcode creator here inside the label loop, I get a barcode all by itself on the first PDF page, with labels following on subsequent pages.  Not bad, but not right. */                
                      // add page to document
                      $pdf-&gt;pages[] = $page;
                  }
  
  /* If I insert the barcode creator here outside the label loop, I get a barcode superimposed on the first PDF page.  Not bad, but not in page loop, and not repeating. */                
                  // save as file
                  $pdf-&gt;save($filename);
                  $this-&gt;_redirect('/' . $filename);
              } catch (Zend_Pdf_Exception $e) {
                  die ('PDF error: ' . $e-&gt;getMessage());  
              } catch (Exception $e) {
                  die ('Application error: ' . $e-&gt;getMessage());    
              }
          }
  </code></pre>