PDF - 使用 TCPDF 打印条形码总是失败

PDF - barcode printing is always failing with TCPDF

我总是得到:

TCPDF ERROR: Error in 1D barcode string

开启

public function testingAction() {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->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->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 027');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 027', PDF_HEADER_STRING);

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    // set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    // set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    // set image scale factor
    $pdf->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->setLanguageArray($l);
    }

    // ---------------------------------------------------------

    // set a barcode on the page footer
    $pdf->setBarcode(date('Y-m-d H:i:s'));

    // set font
    $pdf->SetFont('helvetica', '', 11);

    // add a page
    $pdf->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->MultiCell(70, 50, $txt, 0, 'J', false, 1, 125, 30, true, 0, false, true, 0, 'T', false);
    $pdf->SetY(30);

    // -----------------------------------------------------------------------------

    $pdf->SetFont('helvetica', '', 10);

    // define barcode style
    $style = array(
        'position' => '',
        'align' => 'C',
        'stretch' => false,
        'fitwidth' => true,
        'cellfitalign' => '',
        'border' => true,
        'hpadding' => 'auto',
        'vpadding' => 'auto',
        'fgcolor' => array(0,0,0),
        'bgcolor' => false, //array(255,255,255),
        'text' => true,
        'font' => 'helvetica',
        'fontsize' => 8,
        'stretchtext' => 4
    );

    // PRINT VARIOUS 1D BARCODES

    // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
    $pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
    $pdf->write1DBarcode('CODE 39', 'C39', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 39 + CHECKSUM
    $pdf->Cell(0, 0, 'CODE 39 + CHECKSUM', 0, 1);
    $pdf->write1DBarcode('CODE 39 +', 'C39+', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 39 EXTENDED
    $pdf->Cell(0, 0, 'CODE 39 EXTENDED', 0, 1);
    $pdf->write1DBarcode('CODE 39 E', 'C39E', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 39 EXTENDED + CHECKSUM
    $pdf->Cell(0, 0, 'CODE 39 EXTENDED + CHECKSUM', 0, 1);
    $pdf->write1DBarcode('CODE 39 E+', 'C39E+', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 93 - USS-93
    $pdf->Cell(0, 0, 'CODE 93 - USS-93', 0, 1);
    $pdf->write1DBarcode('TEST93', 'C93', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // Standard 2 of 5
    $pdf->Cell(0, 0, 'Standard 2 of 5', 0, 1);
    $pdf->write1DBarcode('1234567', 'S25', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // Standard 2 of 5 + CHECKSUM
    $pdf->Cell(0, 0, 'Standard 2 of 5 + CHECKSUM', 0, 1);
    $pdf->write1DBarcode('1234567', 'S25+', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // Interleaved 2 of 5
    $pdf->Cell(0, 0, 'Interleaved 2 of 5', 0, 1);
    $pdf->write1DBarcode('1234567', 'I25', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // Interleaved 2 of 5 + CHECKSUM
    $pdf->Cell(0, 0, 'Interleaved 2 of 5 + CHECKSUM', 0, 1);
    $pdf->write1DBarcode('1234567', 'I25+', '', '', '', 18, 0.4, $style, 'N');


    // add a page ----------
    $pdf->AddPage();

    // CODE 128 AUTO
    $pdf->Cell(0, 0, 'CODE 128 AUTO', 0, 1);
    $pdf->write1DBarcode('CODE 128 AUTO', 'C128', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 128 A
    $pdf->Cell(0, 0, 'CODE 128 A', 0, 1);
    $pdf->write1DBarcode('CODE 128 A', 'C128A', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 128 B
    $pdf->Cell(0, 0, 'CODE 128 B', 0, 1);
    $pdf->write1DBarcode('CODE 128 B', 'C128B', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 128 C
    $pdf->Cell(0, 0, 'CODE 128 C', 0, 1);
    $pdf->write1DBarcode('0123456789', 'C128C', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // EAN 8
    $pdf->Cell(0, 0, 'EAN 8', 0, 1);
    $pdf->write1DBarcode('1234567', 'EAN8', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // EAN 13
    $pdf->Cell(0, 0, 'EAN 13', 0, 1);
    $pdf->write1DBarcode('1234567890128', 'EAN13', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // UPC-A
    $pdf->Cell(0, 0, 'UPC-A', 0, 1);
    $pdf->write1DBarcode('12345678901', 'UPCA', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // UPC-E
    $pdf->Cell(0, 0, 'UPC-E', 0, 1);
    $pdf->write1DBarcode('04210000526', 'UPCE', '', '', '', 18, 0.4, $style, 'N');

    // add a page ----------
    $pdf->AddPage();

    // 5-Digits UPC-Based Extension
    $pdf->Cell(0, 0, '5-Digits UPC-Based Extension', 0, 1);
    $pdf->write1DBarcode('51234', 'EAN5', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // 2-Digits UPC-Based Extension
    $pdf->Cell(0, 0, '2-Digits UPC-Based Extension', 0, 1);
    $pdf->write1DBarcode('34', 'EAN2', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // MSI
    $pdf->Cell(0, 0, 'MSI', 0, 1);
    $pdf->write1DBarcode('80523', 'MSI', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // MSI + CHECKSUM (module 11)
    $pdf->Cell(0, 0, 'MSI + CHECKSUM (module 11)', 0, 1);
    $pdf->write1DBarcode('80523', 'MSI+', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODABAR
    $pdf->Cell(0, 0, 'CODABAR', 0, 1);
    $pdf->write1DBarcode('123456789', 'CODABAR', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // CODE 11
    $pdf->Cell(0, 0, 'CODE 11', 0, 1);
    $pdf->write1DBarcode('123-456-789', 'CODE11', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // PHARMACODE
    $pdf->Cell(0, 0, 'PHARMACODE', 0, 1);
    $pdf->write1DBarcode('789', 'PHARMA', '', '', '', 18, 0.4, $style, 'N');

    $pdf->Ln();

    // PHARMACODE TWO-TRACKS
    $pdf->Cell(0, 0, 'PHARMACODE TWO-TRACKS', 0, 1);
    $pdf->write1DBarcode('105', 'PHARMA2T', '', '', '', 18, 2, $style, 'N');

    // add a page ----------
    $pdf->AddPage();

    // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
    $pdf->Cell(0, 0, 'IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200', 0, 1);
    $pdf->write1DBarcode('01234567094987654321-01234567891', 'IMB', '', '', '', 15, 0.6, $style, 'N');

    $pdf->Ln();

    // POSTNET
    $pdf->Cell(0, 0, 'POSTNET', 0, 1);
    $pdf->write1DBarcode('98000', 'POSTNET', '', '', '', 15, 0.6, $style, 'N');

    $pdf->Ln();

    // PLANET
    $pdf->Cell(0, 0, 'PLANET', 0, 1);
    $pdf->write1DBarcode('98000', 'PLANET', '', '', '', 15, 0.6, $style, 'N');

    $pdf->Ln();

    // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
    $pdf->Cell(0, 0, 'RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)', 0, 1);
    $pdf->write1DBarcode('SN34RD1A', 'RMS4CC', '', '', '', 15, 0.6, $style, 'N');

    $pdf->Ln();

    // KIX (Klant index - Customer index)
    $pdf->Cell(0, 0, 'KIX (Klant index - Customer index)', 0, 1);
    $pdf->write1DBarcode('SN34RDX1A', 'KIX', '', '', '', 15, 0.6, $style, 'N');

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // TEST BARCODE ALIGNMENTS

    // add a page
    $pdf->AddPage();

    // set a background color
    $style['bgcolor'] = array(255,255,240);
    $style['fgcolor'] = array(127,0,0);

    // Left position
    $style['position'] = 'L';
    $pdf->write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Center position
    $style['position'] = 'C';
    $pdf->write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Right position
    $style['position'] = 'R';
    $pdf->write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->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->write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Center alignment
    $style['align'] = 'C';
    $pdf->write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Right alignment
    $style['align'] = 'R';
    $pdf->write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->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->write1DBarcode('LEFT', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Center alignment
    $style['cellfitalign'] = 'C';
    $pdf->write1DBarcode('CENTER', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Right alignment
    $style['cellfitalign'] = 'R';
    $pdf->write1DBarcode('RIGHT', 'C128A', 105, '', 90, 15, 0.4, $style, 'N');

    $pdf->Ln(2);
    // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    $style['fgcolor'] = array(127,0,127);

    // Left alignment
    $style['position'] = 'L';
    $pdf->write1DBarcode('LEFT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Center alignment
    $style['position'] = 'C';
    $pdf->write1DBarcode('CENTER', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    $pdf->Ln(2);

    // Right alignment
    $style['position'] = 'R';
    $pdf->write1DBarcode('RIGHT', 'C128A', '', '', '', 15, 0.4, $style, 'N');

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // TEST BARCODE STYLE

    // define barcode style
    $style = array(
        'position' => '',
        'align' => '',
        'stretch' => true,
        'fitwidth' => false,
        'cellfitalign' => '',
        'border' => true,
        'hpadding' => 'auto',
        'vpadding' => 'auto',
        'fgcolor' => array(0,0,128),
        'bgcolor' => array(255,255,128),
        'text' => true,
        'label' => 'CUSTOM LABEL',
        'font' => 'helvetica',
        'fontsize' => 8,
        'stretchtext' => 4
    );

    // CODE 39 EXTENDED + CHECKSUM
    $pdf->Cell(0, 0, 'CODE 39 EXTENDED + CHECKSUM', 0, 1);
    $pdf->SetLineStyle(array('width' => 1, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(255, 0, 0)));
    $pdf->write1DBarcode('CODE 39 E+', 'C39E+', '', '', 120, 25, 0.4, $style, 'N');

    // ---------------------------------------------------------

    //Close and output PDF document
    $pdf->Output('example_027.pdf', 'I');
  }

有人可以帮我解决这个问题吗?

编辑:工作


  public function apprintAction() {
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->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->SetPrintHeader(false);
    $pdf->SetPrintFooter(false);      

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
    //set margins
    $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(0);
    $pdf->SetFooterMargin(0);
    $pdf->setPrintFooter(false);

    //$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
    $pdf->SetAutoPageBreak(false, 0);


    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
    $pdf->setLanguageArray($l);
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('dejavusans', '', 8, '', true);
    $pdf->setPrintHeader(false);

    // Connect to DataBase
    $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', 'production');
    $db = Zend_Db::factory($config->resources->db);

    $kiosk = $_GET['kiosk'];
    $sql = "select *from table0 where kiosk='{$kiosk}' and task='print' order by id desc limit 1";
    $result = $db->fetchAll($sql);
    if(count($result) > 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->fetchAll($sql);   
      if(count($result1)> 0) {
        $qr_code = $result1[0]['picture'];
      }

      $gape = 20;
      $width = 30;

      $sql = "select *from table2 where rd='{$result[0]['rrn']}'";
      $result2 = $db->fetchAll($sql);
      if(count($result2)>0) {
        foreach($result2 as $k=>$v) {
          $pdf->AddPage();
          $img_file = APPLICATION_PATH . '/models/fpdi/en.jpg';
          $pdf->Image($img_file, 0, -10, 190, 300, '', '', '', true, 300, 'C', false, false, 0);   

          $pdf->writeHTMLCell(80, '', 78, 25, $result2[$k]['e'], 1, 0, 1, true, 'J', true);
          $pdf->writeHTMLCell(80, '', 78, 55, $result2[$k]['d'], 1, 0, 1, true, 'J', true);
          $pdf->writeHTMLCell(80, '', 78, 65, $result2[$k]['c'], 1, 0, 1, true, 'J', true);
          $pdf->writeHTMLCell(80, '', 78, 74, $result2[$k]['b'], 1, 0, 1, true, 'J', true);
          $pdf->writeHTMLCell(80, '', 78, 84, $result2[$k]['a'], 1, 0, 1, true, 'J', true);    

          $pdf->writeHTMLCell(160, '', 19, 185, $qr_code, 1, 0, 1, true, 'J', true); 
          // Get the QR-code
          if($qr_code!='') {
            try{
                $pdf->SetFont('helvetica', '', 10);
                $style = array(
                    'border' => 2,
                    'vpadding' => 'auto',
                    'hpadding' => 'auto',
                    'fgcolor' => array(0,0,0),
                    'bgcolor' => false, //array(255,255,255)
                    'module_width' => 1, // width of a single module in points
                    'module_height' => 1 // height of a single module in points
                );
            $pdf->write2DBarcode('www.tcpdf.org', 'PDF417', 20, 195, 0, 30, $style, 'N');
            //$pdf->write1DBarcode($qr_code, 'C128', 18, 190, $width, 13, 0, $style, 'N');   
            }catch(Exception $eo) {  }
          }

          // Get the Bar-codes from the list 
          $style = array(
            'border' => 0,
            'vpadding' => 'auto',
            'hpadding' => 'auto',
            'fgcolor' => array( 0, 0, 0 ),
            'bgcolor' => false,
            'stretch' => 1, 'text' => true
          );
          $pdf->write1DBarcode('00112233449', 'EAN13', 25, 195 +40, $width, 13, 0, $style, 'N');      

        }
      } 

    }

    $pdf->Output('a4', 'I');

  }  

如果您想将 1D 条形码 放在页脚,那么您可以简单地扩展 TCPDF class:

public function testingAction() {

    require_once(APPLICATION_PATH . '/models/tcpdf/config/lang/eng.php');
    require_once(APPLICATION_PATH . '/models/tcpdf/tcpdf.php');

    class MYTCPDF extends TCPDF{

        public function Footer(){

            $barcode = $this->getBarcode();
            if( !empty( $barcode ) ){
                $style = array(
                    'border' => 0,
                    'vpadding' => 'auto',
                    'hpadding' => 'auto',
                    'fgcolor' => array( 0, 0, 0 ),
                    'bgcolor' => false,
                    'stretch' => 1
                );
                $this->write1DBarcode( $barcode, 'EAN13', 84, 4, 75, 13, 0, $style, 'N' );

            }

        }

    }

    $pdf = new MYTCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    $pdf->setBarcode( date('Y-m-d H:i:s') );

}

更新

根据 OP 的评论

I need to place it bottom of the page where i have some space

我会提供这个片段

public function testingAction() {

    require_once(APPLICATION_PATH . '/models/tcpdf/config/lang/eng.php');
    require_once(APPLICATION_PATH . '/models/tcpdf/tcpdf.php');

    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 027');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');

    $params = $pdf->serializeTCPDFtagParameters(
        array(
            date('Y-m-d H:i:s'), 'C39', '', '', 80, 30, 0.4,
            array(
                'position'=>'S',
                'border'=>true,
                'padding'=>4,
                'fgcolor'=>array(0,0,0),
                'bgcolor'=>array(255,255,255),
                'text'=>true,
                'font'=>'helvetica',
                'fontsize'=>8,
                'stretchtext'=>4
            ),
            'N'
        )
    );

    $html = '';
    $html .= '<tcpdf method="write1DBarcode" params="'.$params.'" />';
    $pdf->writeHTML($html, true, 0, true, 0);
    $pdf->Output('my_file.pdf', 'I');

}