title: PDF - barcode printing is always failing with TCPDF
body: <p>I am always getting:</p>
  
  <blockquote>
    <p>TCPDF ERROR: Error in 1D barcode string</p>
  </blockquote>
  
  <p>On</p>
  
  <pre><code>public function testingAction() {
      $this-&gt;_helper-&gt;layout()-&gt;disableLayout();
      $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender();
      require_once(APPLICATION_PATH . '/models/tcpdf/config/lang/eng.php');
      require_once(APPLICATION_PATH . '/models/tcpdf/tcpdf.php');
  
      // create new PDF document
      $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  
      // set document information
      $pdf-&gt;SetCreator(PDF_CREATOR);
      $pdf-&gt;SetAuthor('Nicola Asuni');
      $pdf-&gt;SetTitle('TCPDF Example 027');
      $pdf-&gt;SetSubject('TCPDF Tutorial');
      $pdf-&gt;SetKeywords('TCPDF, PDF, example, test, guide');
  
      // set default header data
      $pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);
  
      // set header and footer fonts
      $pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
      $pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
  
      // set default monospaced font
      $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  
      // set margins
      $pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
      $pdf-&gt;SetHeaderMargin(PDF_MARGIN_HEADER);
      $pdf-&gt;SetFooterMargin(PDF_MARGIN_FOOTER);
  
      // set auto page breaks
      $pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
  
      // set image scale factor
      $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
  
      // set some language-dependent strings (optional)
      if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
          require_once(dirname(__FILE__).'/lang/eng.php');
          $pdf-&gt;setLanguageArray($l);
      }
  
      // ---------------------------------------------------------
  
      // set a barcode on the page footer
      $pdf-&gt;setBarcode(date('Y-m-d H:i:s'));
  
      // set font
      $pdf-&gt;SetFont('helvetica', '', 11);
  
      // add a page
      $pdf-&gt;AddPage();
  
      // print a message
      $txt = "You can also export 1D barcodes in other formats (PNG, SVG, HTML). Check the examples inside the barcodes directory.\n";
      $pdf-&gt;MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
      $pdf-&gt;SetY(30);
  
      // -----------------------------------------------------------------------------
  
      $pdf-&gt;SetFont('helvetica', '', 10);
  
      // define barcode style
      $style = array(
          'position' =&gt; '',
          'align' =&gt; 'C',
          'stretch' =&gt; false,
          'fitwidth' =&gt; true,
          'cellfitalign' =&gt; '',
          'border' =&gt; true,
          'hpadding' =&gt; 'auto',
          'vpadding' =&gt; 'auto',
          'fgcolor' =&gt; array(0,0,0),
          'bgcolor' =&gt; false, //array(255,255,255),
          'text' =&gt; true,
          'font' =&gt; 'helvetica',
          'fontsize' =&gt; 8,
          'stretchtext' =&gt; 4
      );
  
      // PRINT VARIOUS 1D BARCODES
  
      // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
      $pdf-&gt;Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 39 + CHECKSUM
      $pdf-&gt;Cell(0, 0, 'CODE 39 + CHECKSUM', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 39 +', 'C39+', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 39 EXTENDED
      $pdf-&gt;Cell(0, 0, 'CODE 39 EXTENDED', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 39 E', 'C39E', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 39 EXTENDED + CHECKSUM
      $pdf-&gt;Cell(0, 0, 'CODE 39 EXTENDED + CHECKSUM', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 39 E+', 'C39E+', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 93 - USS-93
      $pdf-&gt;Cell(0, 0, 'CODE 93 - USS-93', 0, 1);
      $pdf-&gt;write1DBarcode('TEST93', 'C93', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // Standard 2 of 5
      $pdf-&gt;Cell(0, 0, 'Standard 2 of 5', 0, 1);
      $pdf-&gt;write1DBarcode('1234567', 'S25', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // Standard 2 of 5 + CHECKSUM
      $pdf-&gt;Cell(0, 0, 'Standard 2 of 5 + CHECKSUM', 0, 1);
      $pdf-&gt;write1DBarcode('1234567', 'S25+', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // Interleaved 2 of 5
      $pdf-&gt;Cell(0, 0, 'Interleaved 2 of 5', 0, 1);
      $pdf-&gt;write1DBarcode('1234567', 'I25', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // Interleaved 2 of 5 + CHECKSUM
      $pdf-&gt;Cell(0, 0, 'Interleaved 2 of 5 + CHECKSUM', 0, 1);
      $pdf-&gt;write1DBarcode('1234567', 'I25+', '', '', '', 18, 0.4, $style, 'N');
  
  
      // add a page ----------
      $pdf-&gt;AddPage();
  
      // CODE 128 AUTO
      $pdf-&gt;Cell(0, 0, 'CODE 128 AUTO', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 128 AUTO', 'C128', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 128 A
      $pdf-&gt;Cell(0, 0, 'CODE 128 A', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 128 A', 'C128A', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 128 B
      $pdf-&gt;Cell(0, 0, 'CODE 128 B', 0, 1);
      $pdf-&gt;write1DBarcode('CODE 128 B', 'C128B', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 128 C
      $pdf-&gt;Cell(0, 0, 'CODE 128 C', 0, 1);
      $pdf-&gt;write1DBarcode('0123456789', 'C128C', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // EAN 8
      $pdf-&gt;Cell(0, 0, 'EAN 8', 0, 1);
      $pdf-&gt;write1DBarcode('1234567', 'EAN8', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // EAN 13
      $pdf-&gt;Cell(0, 0, 'EAN 13', 0, 1);
      $pdf-&gt;write1DBarcode('1234567890128', 'EAN13', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // UPC-A
      $pdf-&gt;Cell(0, 0, 'UPC-A', 0, 1);
      $pdf-&gt;write1DBarcode('12345678901', 'UPCA', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // UPC-E
      $pdf-&gt;Cell(0, 0, 'UPC-E', 0, 1);
      $pdf-&gt;write1DBarcode('04210000526', 'UPCE', '', '', '', 18, 0.4, $style, 'N');
  
      // add a page ----------
      $pdf-&gt;AddPage();
  
      // 5-Digits UPC-Based Extension
      $pdf-&gt;Cell(0, 0, '5-Digits UPC-Based Extension', 0, 1);
      $pdf-&gt;write1DBarcode('51234', 'EAN5', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // 2-Digits UPC-Based Extension
      $pdf-&gt;Cell(0, 0, '2-Digits UPC-Based Extension', 0, 1);
      $pdf-&gt;write1DBarcode('34', 'EAN2', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // MSI
      $pdf-&gt;Cell(0, 0, 'MSI', 0, 1);
      $pdf-&gt;write1DBarcode('80523', 'MSI', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // MSI + CHECKSUM (module 11)
      $pdf-&gt;Cell(0, 0, 'MSI + CHECKSUM (module 11)', 0, 1);
      $pdf-&gt;write1DBarcode('80523', 'MSI+', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODABAR
      $pdf-&gt;Cell(0, 0, 'CODABAR', 0, 1);
      $pdf-&gt;write1DBarcode('123456789', 'CODABAR', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // CODE 11
      $pdf-&gt;Cell(0, 0, 'CODE 11', 0, 1);
      $pdf-&gt;write1DBarcode('123-456-789', 'CODE11', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // PHARMACODE
      $pdf-&gt;Cell(0, 0, 'PHARMACODE', 0, 1);
      $pdf-&gt;write1DBarcode('789', 'PHARMA', '', '', '', 18, 0.4, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // PHARMACODE TWO-TRACKS
      $pdf-&gt;Cell(0, 0, 'PHARMACODE TWO-TRACKS', 0, 1);
      $pdf-&gt;write1DBarcode('105', 'PHARMA2T', '', '', '', 18, 2, $style, 'N');
  
      // add a page ----------
      $pdf-&gt;AddPage();
  
      // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
      $pdf-&gt;Cell(0, 0, 'IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200', 0, 1);
      $pdf-&gt;write1DBarcode('01234567094987654321-01234567891', 'IMB', '', '', '', 15, 0.6, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // POSTNET
      $pdf-&gt;Cell(0, 0, 'POSTNET', 0, 1);
      $pdf-&gt;write1DBarcode('98000', 'POSTNET', '', '', '', 15, 0.6, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // PLANET
      $pdf-&gt;Cell(0, 0, 'PLANET', 0, 1);
      $pdf-&gt;write1DBarcode('98000', 'PLANET', '', '', '', 15, 0.6, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
      $pdf-&gt;Cell(0, 0, 'RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)', 0, 1);
      $pdf-&gt;write1DBarcode('SN34RD1A', 'RMS4CC', '', '', '', 15, 0.6, $style, 'N');
  
      $pdf-&gt;Ln();
  
      // KIX (Klant index - Customer index)
      $pdf-&gt;Cell(0, 0, 'KIX (Klant index - Customer index)', 0, 1);
      $pdf-&gt;write1DBarcode('SN34RDX1A', 'KIX', '', '', '', 15, 0.6, $style, 'N');
  
      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      // TEST BARCODE ALIGNMENTS
  
      // add a page
      $pdf-&gt;AddPage();
  
      // set a background color
      $style['bgcolor'] = array(255,255,240);
      $style['fgcolor'] = array(127,0,0);
  
      // Left position
      $style['position'] = 'L';
      $pdf-&gt;write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Center position
      $style['position'] = 'C';
      $pdf-&gt;write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Right position
      $style['position'] = 'R';
      $pdf-&gt;write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
      // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  
      $style['fgcolor'] = array(0,127,0);
      $style['position'] = '';
      $style['stretch'] = false; // disable stretch
      $style['fitwidth'] = false; // disable fitwidth
  
      // Left alignment
      $style['align'] = 'L';
      $pdf-&gt;write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Center alignment
      $style['align'] = 'C';
      $pdf-&gt;write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Right alignment
      $style['align'] = 'R';
      $pdf-&gt;write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
      // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  
      $style['fgcolor'] = array(0,64,127);
      $style['position'] = '';
      $style['stretch'] = false; // disable stretch
      $style['fitwidth'] = true; // disable fitwidth
  
      // Left alignment
      $style['cellfitalign'] = 'L';
      $pdf-&gt;write1DBarcode('LEFT', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Center alignment
      $style['cellfitalign'] = 'C';
      $pdf-&gt;write1DBarcode('CENTER', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Right alignment
      $style['cellfitalign'] = 'R';
      $pdf-&gt;write1DBarcode('RIGHT', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
      // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
  
      $style['fgcolor'] = array(127,0,127);
  
      // Left alignment
      $style['position'] = 'L';
      $pdf-&gt;write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Center alignment
      $style['position'] = 'C';
      $pdf-&gt;write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      $pdf-&gt;Ln(2);
  
      // Right alignment
      $style['position'] = 'R';
      $pdf-&gt;write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');
  
      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      // TEST BARCODE STYLE
  
      // define barcode style
      $style = array(
          'position' =&gt; '',
          'align' =&gt; '',
          'stretch' =&gt; true,
          'fitwidth' =&gt; false,
          'cellfitalign' =&gt; '',
          'border' =&gt; true,
          'hpadding' =&gt; 'auto',
          'vpadding' =&gt; 'auto',
          'fgcolor' =&gt; array(0,0,128),
          'bgcolor' =&gt; array(255,255,128),
          'text' =&gt; true,
          'label' =&gt; 'CUSTOM LABEL',
          'font' =&gt; 'helvetica',
          'fontsize' =&gt; 8,
          'stretchtext' =&gt; 4
      );
  
      // CODE 39 EXTENDED + CHECKSUM
      $pdf-&gt;Cell(0, 0, 'CODE 39 EXTENDED + CHECKSUM', 0, 1);
      $pdf-&gt;SetLineStyle(array('width' =&gt; 1, 'cap' =&gt; 'butt', 'join' =&gt; 'miter', 'dash' =&gt; 0, 'color' =&gt; array(255, 0, 0)));
      $pdf-&gt;write1DBarcode('CODE 39 E+', 'C39E+', '', '', 120, 25, 0.4, $style, 'N');
  
      // ---------------------------------------------------------
  
      //Close and output PDF document
      $pdf-&gt;Output('example_027.pdf', 'I');
    }
  </code></pre>
  
  <p>Could somebody please help me with this?</p>
  
  <p><strong>EDIT: working</strong></p>
  
  <hr>
  
  <pre><code>  public function apprintAction() {
      $this-&gt;_helper-&gt;layout()-&gt;disableLayout();
      $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender();
      require_once(APPLICATION_PATH . '/models/tcpdf/config/lang/eng.php');
      require_once(APPLICATION_PATH . '/models/tcpdf/tcpdf.php');
  
      // create new PDF document
      $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
      $pdf-&gt;SetPrintHeader(false);
      $pdf-&gt;SetPrintFooter(false);      
  
      // set default header data
      $pdf-&gt;SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
      // set header and footer fonts
      $pdf-&gt;setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
      $pdf-&gt;setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
      // set default monospaced font
      $pdf-&gt;SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
      //set margins
      $pdf-&gt;SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
      $pdf-&gt;SetHeaderMargin(0);
      $pdf-&gt;SetFooterMargin(0);
      $pdf-&gt;setPrintFooter(false);
  
      //$pdf-&gt;SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
      $pdf-&gt;SetAutoPageBreak(false, 0);
  
  
      $pdf-&gt;setImageScale(PDF_IMAGE_SCALE_RATIO);
      $pdf-&gt;setLanguageArray($l);
      $pdf-&gt;setFontSubsetting(true);
      $pdf-&gt;SetFont('dejavusans', '', 8, '', true);
      $pdf-&gt;setPrintHeader(false);
  
      // Connect to DataBase
      $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
      $db = Zend_Db::factory($config-&gt;resources-&gt;db);
  
      $kiosk = $_GET['kiosk'];
      $sql = "select *from table0 where kiosk='{$kiosk}' and task='print' order by id desc limit 1";
      $result = $db-&gt;fetchAll($sql);
      if(count($result) &gt; 0 ){
        $qr_code = '';
        // Collect the QR code here
        $sql = "select *from table1 where rrn='{$result[0]['rrn']}' and firstname='{$result[0]['firstname']}' and lastname='{$result[0]['lastname']}' order by id desc limit 1";
  
        $result1 = $db-&gt;fetchAll($sql);   
        if(count($result1)&gt; 0) {
          $qr_code = $result1[0]['picture'];
        }
  
        $gape = 20;
        $width = 30;
  
        $sql = "select *from table2 where rd='{$result[0]['rrn']}'";
        $result2 = $db-&gt;fetchAll($sql);
        if(count($result2)&gt;0) {
          foreach($result2 as $k=&gt;$v) {
            $pdf-&gt;AddPage();
            $img_file = APPLICATION_PATH . '/models/fpdi/en.jpg';
            $pdf-&gt;Image($img_file, 0, -10, 190, 300, '', '', '', true, 300, 'C', false, false, 0);   
  
            $pdf-&gt;writeHTMLCell(80, '', 78, 25, $result2[$k]['e'], 1, 0, 1, true, 'J', true);
            $pdf-&gt;writeHTMLCell(80, '', 78, 55, $result2[$k]['d'], 1, 0, 1, true, 'J', true);
            $pdf-&gt;writeHTMLCell(80, '', 78, 65, $result2[$k]['c'], 1, 0, 1, true, 'J', true);
            $pdf-&gt;writeHTMLCell(80, '', 78, 74, $result2[$k]['b'], 1, 0, 1, true, 'J', true);
            $pdf-&gt;writeHTMLCell(80, '', 78, 84, $result2[$k]['a'], 1, 0, 1, true, 'J', true);    
  
            $pdf-&gt;writeHTMLCell(160, '', 19, 185, $qr_code, 1, 0, 1, true, 'J', true); 
            // Get the QR-code
            if($qr_code!='') {
              try{
                  $pdf-&gt;SetFont('helvetica', '', 10);
                  $style = array(
                      'border' =&gt; 2,
                      'vpadding' =&gt; 'auto',
                      'hpadding' =&gt; 'auto',
                      'fgcolor' =&gt; array(0,0,0),
                      'bgcolor' =&gt; false, //array(255,255,255)
                      'module_width' =&gt; 1, // width of a single module in points
                      'module_height' =&gt; 1 // height of a single module in points
                  );
              $pdf-&gt;write2DBarcode('www.tcpdf.org', 'PDF417', 20, 195, 0, 30, $style, 'N');
              //$pdf-&gt;write1DBarcode($qr_code, 'C128', 18, 190, $width, 13, 0, $style, 'N');   
              }catch(Exception $eo) {  }
            }
  
            // Get the Bar-codes from the list 
            $style = array(
              'border' =&gt; 0,
              'vpadding' =&gt; 'auto',
              'hpadding' =&gt; 'auto',
              'fgcolor' =&gt; array( 0, 0, 0 ),
              'bgcolor' =&gt; false,
              'stretch' =&gt; 1, 'text' =&gt; true
            );
            $pdf-&gt;write1DBarcode('00112233449', 'EAN13', 25, 195 +40, $width, 13, 0, $style, 'N');      
  
          }
        } 
  
      }
  
      $pdf-&gt;Output('a4', 'I');
  
    }  
  </code></pre>