页脚中的TCPDF QR码?

时间:2012-12-27 22:30:07

标签: php pdf tcpdf

我正在使用PHP脚本TCPDF .. 有没有办法,我可以将QR码放在PDF文件的页脚中?

这是我用它在页面上显示的方式(不是页脚或页眉)

$pdf->write2DBarcode('www.tcpdf.org', 'QRCODE,L', 20, 30, 50, 50, $style, 'N');
$pdf->Text(20, 25, 'QRCODE L');

4 个答案:

答案 0 :(得分:1)

当然,这是完全可能的,它在示例中:

http://www.tcpdf.org/examples/example_050.pdf

根据定义,PDF文件中没有明确的“页脚”,它只是在特定的地方呈现。所以把它放在你放“脚”的花边上。

您的代码看起来很好(但只有当您的整个代码超出我假设的这两行时)。

答案 1 :(得分:1)

我的问题是,我从类调用变量 - 这不起作用。我需要创建不断的外部课程。

define('NASLOV', $naslov_pdf_poln);
class MYPDF extends TCPDF {

//Page header
public function Header() {
    // Logo
    $image_file = K_PATH_IMAGES.'logo_mali.jpg';
    $this->Image($image_file, 10, 10, 15, '', 'JPG', '', 'T', false, 300, '',     false, false, 0, false, false, false);
    // Set font
    $this->SetFont('helvetica', 'B', 20);
    // Title
    $this->Cell(0, 15, 'Ultimate Basketball Stats', 0, false, 'C', 0, '', 0, false, 'M', 'M');
    $this->Cell(0,21, 'Ultimate Basketball Stats', 0, false, 'C', 0, '', 0, false, 'M', 'M');
}

// Page footer
public function Footer() {
    // Position at 15 mm from bottom
    $this->SetY(-15);
    // Set font


    $this->SetFont('helvetica', 'I', 8);
    $this->write2DBarcode(NASLOV,'DATAMATRIX', 100, 265, 25, 25, $style, 'N');

    // Page number
    $this->Cell(0, 10, NASLOV, 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

答案 2 :(得分:0)

很长时间才能收到Gregor消息... 在Footer()函数中,您使用的是$ style,它可能是在主代码中设置的。但是在类的功能内部看不到它。 为了能够在Footer()函数中使用它,必须将其声明为“ global”。所以:

     public function Footer()
      {
       global $style;

       // Position at 15 mm from bottom
        $this->SetY(-15);

以此类推。

答案 3 :(得分:0)

我知道这是一个老问题,但是以防万一有人落在这里。

您可以使用$pdf->setBarcode( 'abcdef' );,默认情况下,条形码将作为1DBarcode包含在PDF页脚中。就我而言,我更喜欢将条形码作为QR码放置在徽标旁边的标题中,并按如下所示自定义默认的Header和Footer:

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