【问题标题】:how TCPDF prevent the extra blank pageTCPDF 如何防止多余的空白页
【发布时间】:2014-05-01 06:34:46
【问题描述】:

我已经创建了使用 TCPDF 制作页面的类。

我需要将 HTML 转换为 pdf,所以我使用 writeHTMLAcceptPageBreak()

$html 是动态更改的,可能很长。

class MY_TCPDF extends TCPDF{
    public function makePage($html){
        $head_image="header.jpg";
        $this->SetMargins(PDF_MARGIN_LEFT, 70, PDF_MARGIN_RIGHT);
        $this->setPrintHeader(false);
        $this->AddPage();
        // get the current page break margin
        $bMargin = $this->getBreakMargin();
        // get current auto-page-break mode
        $auto_page_break = $this->getAutoPageBreak();
        // disable auto-page-break
        $this->SetAutoPageBreak(false, 0);
        // set bacground image
        $img_file = $head_image;
        $this->Image($img_file, 0, 0, 210, 68, '', '', '', false, 300, '', false, false, 0);
        // restore auto-page-break status
        //$this->SetAutoPageBreak($auto_page_break, PDF_MARGIN_BOTTOM);
        // set the starting point for the page content
        $this->setPageMark();
        $this->writeHTML($html, true, false, true, false, '');
        $this->lastPage();


        ob_start();
        //Close and output PDF document
        $this->Output('my.pdf', 'I');
        ob_end_flush();
    }

    public function AcceptPageBreak() {
        $this->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
        $this->AddPage();   
        return false;
    }
}

问题是我生成 PDF,但 PDF 末尾总是有一个额外的空白页。

我尝试使用 $this->delete($this->getPage()) ,但它只删除有内容的最后一页,并保留额外的空白页。这似乎writeHTML 会在它之后创建一个分页符。

如何防止出现这个多余的空白页?

【问题讨论】:

    标签: php pdf tcpdf


    【解决方案1】:

    您应该检查您的 $html 变量。

    1) 如果它可以包含任何<html />, <head />, <title />, <body /> 标记,请删除它们并在<body /> 之前和之前获取html 内容。

    2) 您应该避免在$html 内容中使用任何 css、js 链接文件。

    3) 最后你应该在$this->writeHTML($html, true, false, true, false, ''); 之前使用$html=utf8_encode($html);

    4) 您可能需要调整您的 MARGIN_LEFT、MARGIN_TOP、MARGIN_RIGHT 和 MARGIN_BOTTOM 来解决此类问题。请检查$this->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);$this->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    希望它能解决你的问题。

    【讨论】:

    • 检查是否在 HTML 内容的末尾添加了分页符,如果是,请删除它或根据要求在其周围添加一些条件
    【解决方案2】:

    还要检查封闭 div 的高度。 它不应该是 100%。 尝试从封闭 div 的 CSS 样式中删除任何高度属性(我的意思是包含所有内容的 div)。

    【讨论】:

      【解决方案3】:

      试试这个deletePage函数

      $lastPage = $this->getPage();
      $this->deletePage($lastPage);
      

      使用deletePage代替删除

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题: 我修复了它:

        class TCPDFextended extends \TCPDF {
        
            public function Output($name = 'doc.pdf', $dest = 'I')
            {
                $this->tcpdflink = false;
                return parent::Output($name, $dest);
            }
        
        }
        

        【讨论】:

        • 这应该是被接受的答案。非常适合我
        • 你能解释一下吗?
        【解决方案5】:

        我的回答类似于@kanti。我认为我们甚至可以在输出生成之前将默认值设置为 false。

        背景。我们看到的额外页面基本上是

        "如果真打印 TCPDF 元链接"。

        所以默认情况下 TCPDF::$tcpdflink = true 设置为 true。 我们只需要

         class My_PDF extends TCPDF {
             public function changeTheDefault($tcpdflink) {
                    $this->tcpdflink = $tcpdflink;
                  }
        }
        

        稍后在需要时调用您的公共函数。 ...

        $get_pdf = new My_PDF (your_parameters);
        $get_pdf->changeTheDefault(false); # changes the default to false
        

        祝你好运。

        【讨论】:

          【解决方案6】:

          问题在于您的 create_pdf.php 文件中的第四个参数 (unicode = true)。此参数在第 1838 行传入 tcpdf.php

          $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'ISO-8859-1', false);
          

          把它改成假的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多