【问题标题】:TCPDF QR-code in footer?页脚中的 TCPDF QR 码?
【发布时间】:2012-12-27 22:30:06
【问题描述】:

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

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

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

【问题讨论】:

  • 你看他们的demos了吗?有一个带有源代码的 QR 示例,以及显示如何做页脚的示例......?
  • 所以你终于设法复制了演示 #50 中的这两行?抱歉,但您需要详细说明什么对您不起作用以及错误是什么。也许你只需要再努力一点,就可以解决这个“问题”?

标签: php pdf tcpdf


【解决方案1】:

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

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

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

您的代码看起来不错(但前提是您的整个代码比我假设的这两行多)。

【讨论】:

    【解决方案2】:

    我的问题是,我在课堂上调用变量 - 这不起作用。我需要在类外创建常量。

    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);
    

    【讨论】:

      【解决方案3】:

      距离 Gregor 消息还有很长的时间... 在您的 Footer() 函数中,您使用的 $style 可能已在您的主代码中设置。但它不能'在类的函数中看到。 为了能够在 Footer() 函数中使用它,您必须将其声明为“全局”。所以:

           public function Footer()
            {
             global $style;
      
             // Position at 15 mm from bottom
              $this->SetY(-15);
      

      等等。

      【讨论】:

        【解决方案4】:

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

        您可以使用$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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-18
          • 2011-04-04
          • 1970-01-01
          • 2015-01-10
          • 1970-01-01
          • 1970-01-01
          • 2012-04-24
          • 1970-01-01
          相关资源
          最近更新 更多