📜 ⬆️ ⬇️

Generating PDF files in a web project: wkhtmltopdf program

When working on a web project, it is sometimes necessary to generate PDF files with large tables: price lists for thousands of items. There were different libraries for generating a PDF file from a PHP script:

• FPDF
• MPDF is an FPDF-based library that allows you to generate a pdf file from any html code
• DOMPDF
• TCPDF

and many different other libraries. The MPDF library turned out to be the most powerful and suitable, besides, initially working correctly with Cyrillic, if not a critical drawback in our case: large tables and large files in general were extremely slowly generated. Moreover, often the generation did not occur at all, and the script stopped with an error 504.
')
Further search helped find the wkhtmltopdf program. Program website: http://wkhtmltopdf.org .

Unlike php-libraries, it is a server program distributed, including in the form of packages and executable files for linux, windows and other operating systems. The program accepts html-code (in the form of a web address, path to the file or a string of code) and generates on its basis a pdf-file on the server.

Preliminary experience has shown that on a local XAMPP server under Windows a huge html-table of 300-500 pages is converted into a pdf-file in 1-2 seconds!

Installing wkhtmltopdf on CentOs 6
The program requires a webkit and qt.

So, install the required environment and program on the server. CentOs 6 is installed on our server. Log in to the server as root and execute the following commands.

We will get the rkh-package of the wkhtmltopdf program using the link from the developer’s site and install it on the production server:

wget download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-centos6-i386.rpm
yum --nogpgcheck localinstall wkhtmltox-0.12.2.1_linux-centos6-i386.rpm


All package dependencies should be automatically checked and satisfied. If the environment is not established for some reason, use the commands:

yum install urw-fonts libXext openssl-devel libXrender
yum install xorg-x11-fonts-cyrillic.noarch xorg-x11-fonts-misc.noarch xorg-x11-fonts-truetype.noarch xorg-x11-fonts-100dpi.noarch xorg-x11-fonts-75dpi.noarch fonts-ISO8859-2.noarch fonts-ISO8859-2-100dpi.noarch fonts-ISO8859-2-75dpi.noarch freefont.noarch


Until recently, the program was not provided as an rpm package, and you had to copy the binary file and manually install all the necessary packages.

Using wkhtmltopdf on CentOs 6
The general format of the program launch is:

wkhtmltopdf <, .html> <, .pdf>


In addition, the program allows you to automatically embed the header and footer of the document from separate html files. For this, the syntax is:

wkhtmltopdf --header-html <, .html> --footer-html <, .html> <, .html> <, .pdf>


Also among the launch options of the program is the custom size of the fields of the resulting pdf-file. In the upper and lower fields, the program inserts a header and a basement:

wkhtmltopdf --margin-top 35mm --margin-bottom 27mm --margin-left 10mm --margin-right 10mm --header-html <, .html> --footer-html <, .html> <, .html> <, .pdf>


In this example:
• top margin: 35 mm
• bottom margin: 27 mm
• left, right margins: 10mm each

I will also give an example of a basement code. In our case, page numbers are automatically generated and substituted into the footer. Thus, in our document the pages are automatically numbered:

//





Also among the useful startup options of the program:

- encoding - specifies the encoding of the source html file, for example:
--encoding windows-1251


- page-size - specifies the page format, for example:
--page-size A4


- orientation - page orientation, for example:
--orientation Landscape


In our web project on the php page that forms the pdf file, this php code is used:
$tmp=time();

$f=fopen(ABSPATH.'/tmp/'.$tmp.'.html','w');
fputs($f, $llg);
fclose($f);

$cd = "cd ".ABSPATH.'/tmp';
exec($cd);
$command = "wkhtmltopdf-i386 --margin-top 35mm --margin-bottom 27mm --margin-left 10mm --margin-right 10mm --footer-html ".ABSPATH."/tpl-sm/pl_pdf/pdf_footer.html --header-html ".ABSPATH."/tpl-sm/pl_pdf/pdf_header.html ".ABSPATH.'/tmp/'.$tmp.'.html'." ".ABSPATH.'/tmp/'."$tmp.pdf";

exec($command);
if (file_exists(ABSPATH.'/tmp/'.$tmp.'.pdf')) {
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="pricelist.pdf"');
readfile(ABSPATH.'/tmp/'.$tmp.'.pdf');
}
unlink(ABSPATH.'/tmp/'.$tmp.'.pdf');
unlink(ABSPATH.'/tmp/'.$tmp.'.html');


In this code:
• variable $ llg - contains price list html-code
• constant ABSPATH - absolute path to the web project folder on the server.
The code does the following:
• Writes the price list html code to a temporary file;
• Moves to a temporary directory;
• Runs wkhtmltopdf with the required options;
• If the pdf-file was successfully created - then returns it to the user in the browser, offering to download the file as pricelist.pdf;
• Removes temporary html and pdf files from the temporary directory.

Source: https://habr.com/ru/post/266571/


All Articles