我知道这是一个老问题,但以防万一有人降落在这里。
您可以使用$pdf->setBarcode( 'abcdef' );,默认情况下,条形码将作为 1DBarcode 包含在 PDF 页脚中。在我的情况下,我更喜欢将条形码作为 QR 码放在徽标旁边的页眉中,并按如下方式自定义默认页眉和页脚:
class MYPDF extends TCPDF {
/**
* This method is used to render the page header.
* It is automatically called by AddPage() and could be overwritten in your own inherited class.
* @public
*/
public function Header() {
$barcode = $this->getBarcode();
if ( $this->header_xobjid === false ) {
// start a new XObject Template
$this->header_xobjid = $this->startTemplate( $this->w, $this->tMargin );
$headerfont = $this->getHeaderFont();
$headerdata = $this->getHeaderData();
$this->y = $this->header_margin;
if ( $this->rtl ) {
$this->x = $this->w - $this->original_rMargin;
} else {
$this->x = $this->original_lMargin;
}
if ( ( $headerdata['logo'] ) AND ( $headerdata['logo'] != K_BLANK_IMAGE ) ) {
$imgtype = TCPDF_IMAGES::getImageFileType( K_PATH_IMAGES . $headerdata['logo'] );
if ( ( $imgtype == 'eps' ) OR ( $imgtype == 'ai' ) ) {
$this->ImageEps( K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width'] );
} elseif ( $imgtype == 'svg' ) {
$this->ImageSVG( K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width'] );
} else {
$this->Image( K_PATH_IMAGES . $headerdata['logo'], '', '', $headerdata['logo_width'] );
}
$imgy = $this->getImageRBY();
} else {
$imgy = $this->y;
}
$cell_height = $this->getCellHeight( $headerfont[2] / $this->k );
// set starting margin for text data cell
if ( $this->getRTL() ) {
$header_x = $this->original_rMargin + ( $headerdata['logo_width'] * 1.1 );
} else {
$header_x = $this->original_lMargin + ( $headerdata['logo_width'] * 1.1 );
}
$cw = $this->w - $this->original_lMargin - $this->original_rMargin - ( $headerdata['logo_width'] * 1.1 );
$this->SetTextColorArray( $this->header_text_color );
if ( ! empty( $barcode ) ) {
$this->write2DBarcode( $barcode, 'QRCODE,H', $headerdata['logo_width'] + 20, '', 12, 12, [], '' );
}
// header title
$this->SetFont( $headerfont[0], 'B', $headerfont[2] + 1 );
$this->SetX( $header_x );
$this->Cell( $cw, $cell_height, $headerdata['title'], 0, 1, 'R', 0, '', 0 );
// header string
$this->SetFont( $headerfont[0], $headerfont[1], $headerfont[2] );
$this->SetX( $header_x );
$this->MultiCell( $cw, $cell_height, $headerdata['string'], 0, 'R', 0, 0, '', '', true, 0, false, true, 0, 'T', false );
// print an ending header line
$this->SetLineStyle( [
'width' => 0.85 / $this->k,
'cap' => 'butt',
'join' => 'miter',
'dash' => 0,
'color' => $headerdata['line_color']
] );
$this->SetY( ( 2.835 / $this->k ) + max( $imgy, $this->y ) );
if ( $this->rtl ) {
$this->SetX( $this->original_rMargin );
} else {
$this->SetX( $this->original_lMargin );
}
$this->Cell( ( $this->w - $this->original_lMargin - $this->original_rMargin ), 0, '', 'T', 0, 'C' );
$this->endTemplate();
}
// print header template
$x = 0;
$dx = 0;
if ( ! $this->header_xobj_autoreset AND $this->booklet AND ( ( $this->page % 2 ) == 0 ) ) {
// adjust margins for booklet mode
$dx = ( $this->original_lMargin - $this->original_rMargin );
}
if ( $this->rtl ) {
$x = $this->w + $dx;
} else {
$x = 0 + $dx;
}
$this->printTemplate( $this->header_xobjid, $x, 0, 0, 0, '', '', false );
if ( $this->header_xobj_autoreset ) {
// reset header xobject template at each page
$this->header_xobjid = false;
}
}
/**
* This method is used to render the page footer.
* It is automatically called by AddPage() and could be overwritten in your own inherited class.
* @public
*/
public function Footer() {
$cur_y = $this->y;
$this->SetTextColorArray( $this->footer_text_color );
//set style for cell border
$line_width = ( 0.85 / $this->k );
$this->SetLineStyle( [
'width' => $line_width,
'cap' => 'butt',
'join' => 'miter',
'dash' => 0,
'color' => $this->footer_line_color
] );
$w_page = isset( $this->l['w_page'] ) ? $this->l['w_page'] . ' ' : '';
if ( empty( $this->pagegroups ) ) {
$pagenumtxt = $w_page . $this->getAliasNumPage() . ' / ' . $this->getAliasNbPages();
} else {
$pagenumtxt = $w_page . $this->getPageNumGroupAlias() . ' / ' . $this->getPageGroupAlias();
}
$this->SetY( $cur_y );
//Print page number
if ( $this->getRTL() ) {
$this->SetX( $this->original_rMargin );
$this->Cell( 0, 0, $pagenumtxt, 'T', 0, 'L' );
} else {
$this->SetX( $this->original_lMargin );
$this->Cell( 0, 0, $this->getAliasRightShift() . $pagenumtxt, 'T', 0, 'R' );
}
}
}
为了使用自定义页眉和页脚,您需要执行以下操作:
$pdf = new MYPDF( PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false );
$pdf->setBarcode( 'abcdef );
//Keep on building the pdf as usual