MPDF HTML Fill page

61 views Asked by At

I am using MPDF to generate a pdf file from HTML, the document is a legal document that in a particular jurisdiction requires all pages to filled top to bottom with text i.e. the paragraphs of text can not stop half way down the page.

I have looked at auto calculating remaining space and adjusting line heights etc but it seems to be overly complicated and difficult to do when the amount of text can vary from document to document.

Does anyone know a way / function that can be used within mPDF to automatically do this?

1

There are 1 answers

0
Ken Lee On BEST ANSWER

Assuming that the legal document will not be just a single-line sentence.

In that case one can use a loop.

  • First use a minimum line height and generate the pdf and calculate the total number of pages
$pageCount = count($mpdf->pages);
  • then increase the line height by say 0.05 each time (line height=line height +0.05)
$mpdf->normalLineheight = $_SESSION["line-height"];

.....

 
$_SESSION["line-height"]=$_SESSION["line-height"]+0.05;
  • perform the iteration until suddenly the total number of pages increase by one
  • then fall back to (line height = previous line height before the increase of page number), now generate the pdf

So, please run the following script (e.g. pre-genpdf.php) to initalize the setting:

<?php
session_start();

// unset session variables first 

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>

After that, click the hyperlink to trigger the following 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
}   
?>

Please note that I have used some PHP documentation as $teststring0 for testing, for real case please use the actual textual data of your legal document.

It will generate temp.pdf in the loop (just ignore it, each iteration over the loop will generate this temp file, overwriting the previous one since they are of the same name), but finally will generate the final.pdf which is the one with best line-height and then the process will STOP.

See the result

  1. initial line-height:

enter image description here

  1. final.pdf (best line-height) enter image description here

Note: for better result you may adjust the increment value of line-height each time from 0.05 to say 0.01 (each time smaller step) and the result may even be better, but of course it may then take longer time for the iteration to complete.