MPDF HTML 填充页面

问题描述 投票:0回答:1

我正在使用 MPDF 从 HTML 生成 pdf 文件,该文档是一份法律文件,在特定司法管辖区要求所有页面从上到下填充文本,即文本段落不能停在页面的中间。

我看过自动计算剩余空间和调整行高等,但当文本量因文档而异时,这似乎过于复杂且难以做到。

有人知道可以在 mPDF 中使用的方法/函数来自动执行此操作吗?

php laravel documentation mpdf
1个回答
0
投票

假设法律文件不会只是单行句子。

在这种情况下,可以使用循环。首先使用最小行高并生成pdf并计算总页数,然后每次增加行高0.05(行高=行高+0.05)并执行迭代,直到总页数突然增加一,然后回落到(行高=页码增加之前的行高),现在生成pdf

因此,请运行以下脚本(例如 pre-genpdf.php)来初始化设置:

<?php
session_start();
unset($_SESSION["pageCount"]);
unset($_SESSION["initialpageCount"]);
unset($_SESSION["line-height"]);
unset($_SESSION["good-line-height"]);

?>
Initalization completed, please run genpdf.php by clicking <A href=genpdf.php>HERE</a>

之后点击超链接即可触发以下genpdf.php

<?php
session_start();

$initiallineheight=1.0;

if (!isset( $_SESSION["pageCount"] )) {
  $_SESSION["pageCount"]=0;
} 

if (!isset( $_SESSION["initialpageCount"] )) {
  $_SESSION["initialpageCount"]=0;
} 

if (!isset( $_SESSION["line-height"] )) {
  $_SESSION["line-height"]=$initiallineheight; 
} 

if (!isset( $_SESSION["good-line-height"] )) {
  $_SESSION["good-line-height"]=$initiallineheight; 
} 


$teststring0="
This section will guide you through the general configuration and installation of PHP on Unix systems. Be sure to investigate any sections specific to your platform or web server before you begin the process.

As our manual outlines in the General Installation Considerations section, we are mainly dealing with web centric setups of PHP in this section, although we will cover setting up PHP for command line usage as well.

There are several ways to install PHP for the Unix platform, either with a compile and configure process, or through various pre-packaged methods. This documentation is mainly focused around the process of compiling and configuring PHP. Many Unix like systems have some sort of package installation system. This can assist in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your web server. If you are unfamiliar with building and compiling your own software, it is worth checking to see whether somebody has already built a packaged version of PHP with the features you need.

Prerequisite knowledge and software for compiling:

Basic Unix skills (being able to operate make and a C compiler)
An ANSI C compiler
A web server
Any module specific components (such as GD, PDF libs, etc.)
When building directly from Git sources or after custom modifications you might also need:

autoconf: 2.59+ (for PHP >= 7.0.0), 2.64+ (for PHP >= 7.2.0)
automake: 1.4+
libtool: 1.4.x+ (except 1.4.2)
re2c: 0.13.4+
bison:
PHP 7.0 - 7.3: 2.4 or later (including Bison 3.x)
End TEST.
";


$teststring=$teststring0;
$teststring.=$teststring0;
$teststring.=$teststring0;

require_once __DIR__ . '/vendor/autoload.php';


$mpdf = new \Mpdf\Mpdf();

$mpdf->useFixedNormalLineHeight = true;
$mpdf->useFixedTextBaseline = true;


$mpdf->normalLineheight = $_SESSION["line-height"];
$teststring=str_replace(chr(13),'<br>',$teststring);


$mpdf->WriteHTML($teststring);

$mpdf->Output('temp1.pdf', \Mpdf\Output\Destination::FILE);

$pageCount = count($mpdf->pages);

$_SESSION["pageCount"]=$pageCount; 

if ($_SESSION["line-height"]==$initiallineheight) {
$_SESSION["initialpageCount"]=$pageCount; 
}

if ($_SESSION["initialpageCount"]==$_SESSION["pageCount"]){
$_SESSION["good-line-height"]=$_SESSION["line-height"]; 

$_SESSION["line-height"]=$_SESSION["line-height"]+0.05; 

?>
<script>
    location.reload(); 
</script>   

<?php
} else{

$mpdf = new \Mpdf\Mpdf();

$mpdf->useFixedNormalLineHeight = true;
$mpdf->useFixedTextBaseline = true;


$mpdf->normalLineheight = $_SESSION["good-line-height"];
$teststring=str_replace(chr(13),'<br>',$teststring);

$mpdf->WriteHTML($teststring);

$mpdf->Output('final.pdf', \Mpdf\Output\Destination::FILE);
?>
<script>
    alert("Done ! Please use the final.pdf"); 
</script>   
<?php
}   
?>

请注意,我使用了一些 PHP 文档作为

$teststring0
进行测试,对于真实情况,请使用您的法律文档的实际文本数据。

它将在循环上生成 temp.pdf (忽略它,循环上的每次迭代都会生成该文件并覆盖前一个文件,因为它们具有相同的名称),但最终会生成“final.pdf”,即具有最佳行高的一个,然后停止该过程。

查看结果(初始行高):

和final.pdf(最佳行高)

注意:为了获得更好的结果,您可以将行高的增量从 0.05 调整为 0.01(每次较小的步长),结果可能会更好,但当然可能需要更长的时间才能完成迭代。

© www.soinside.com 2019 - 2024. All rights reserved.