【问题标题】:mPDF creates an extra blank page when the content reaches a certain width. How can I get rid of the extra blank page?当内容达到一定宽度时,mPDF 会创建一个额外的空白页。我怎样才能摆脱多余的空白页?
【发布时间】:2020-06-25 03:01:37
【问题描述】:

我在使用 mpdf 时遇到了这个问题,如果某些文本到达页面的最后一行,它将创建一个额外的空白页面。

我已经尝试了一百万件事,但似乎无法弄清楚这一点。我想要一个可靠的方法来确保我的 pdf 中没有空白页。

所以,我想知道是否有一个 {PAGEEND} 标记或我在我的 html 中放置的某个标记让 mPDF 知道这是页面的结尾,并且不要在此页面之后创建页面。我在文档中没有看到任何关于此的内容。

这是我的 mpdf 配置中的内容:

$mpdf = new \Mpdf\Mpdf([
    'format' => 'A4',
    'margin_left' => 0,
    'margin_right' => 0,
    'margin_top' => 6,
    'margin_bottom' => 20,
    'margin_header' => 5,
    'margin_footer' => 5
]);

if ( isset( $_REQUEST['css_files'] ) ) {
    $css_files = $_REQUEST['css_files'];
    if ( is_array($css_files) ) {
        foreach ( $_REQUEST['css_files'] as $cssurl ) {
            $stylesheet = file_get_contents($cssurl);
            $mpdf->WriteHTML($stylesheet,1);
        }
    }
}

$mpdf->useSubstitutions = false; // optional - just as an example
$mpdf->simpleTables = false;
$mpdf->SetAuthor("My Website");
$mpdf->SetDisplayMode('fullpage');
$mpdf->setAutoBottomMargin = 'stretch';
$mpdf->SetHTMLFooter('<div width="90%" align="right">{PAGENO}</div>');
$mpdf->setBasePath($url);
$mpdf->WriteHTML( $html );
$mpdf->Output($title . ".pdf", 'D'); exit;

我将 margin-bottom 设置为 20mm,这样内容就不会与我的页脚冲突。

css_files 是从我的模板接收的数组。 css 文件是网站样式和打印样式表。我正在加载它们并将它们应用到页面。

我已经尝试在我的 print.css 上洒page-break-after: avoid;,但我仍然得到那个讨厌的额外空白页。

我还清理并清除了任何额外的底部边距,但这似乎没有帮助。

【问题讨论】:

    标签: mpdf


    【解决方案1】:

    经过几个小时的故障排除,这被证明是完全愚蠢的事情。

    发生的情况是我的“打印到 pdf”按钮被打印在第 4 页上。我看不到它,因为它在 pdf 中的白色背景上有白色文本。

    直到我将生成的 pdf 文件分享到 Teams 中,我们才看到了文本。我们有一个条件,我们设置从 pdf 中不需要的页面中删除 html。

    请记住,带有 print to pdf 按钮的表单被包装在一个带有 hidden-print 类的 div 中,并且按钮本身也有 hidden-print 类。不知道为什么它没有在页面中的这个元素上采用这种样式。所以从代码中删除它是最好的选择。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2021-12-26
      • 1970-01-01
      相关资源
      最近更新 更多