Using the DOMPDF library, you can create PDF files from HTML code. It is only necessary to impose a certain HTML template and transfer it to DOMPDF and get the generated PDF file at the output. But we must not forget that this library is not an engine for processing web pages, and therefore templates should be composed using the most basic tools: HTML tags and CSS styles. Although the developers claim that their library supports CSS 2.1 standards and even some CSS 3 properties. A full list of them can be found
here . By the way, there is one drawback - it is quite “voracious” to the RAM. Therefore, you may have to think about renting a virtual dedicated server. But it depends on the content and volume of the planned PDF documents. On the other hand, it can help you save a lot of time on the development of various reports in PDF format.
Reading the various DOMPDF documentation, and its a bit and then basically more information in the form of answers to the questions asked. I noticed that it is used for more trivial tasks. Well, for example, the answer to the question: how to set the number of the start page for the document? I have not received.
Now I want to tell you about those "pitfalls" that I had to face. And believe me, I have lost a lot of time. I would have lost even more if one kind person had not given me a couple of tips. Already had experience working with this library.
I present a list of problems that I had to face:
- the lack of the necessary directories with files (not complete assembly);
- partially working version of the library;
- the presence of a bug when trying to set the page number of the document (the number of the start page);
- the presence of a blank page at the end of the document;
- incomplete filling of the page with text (sometimes there was a lot of empty space).
As can be seen from the above, in this library there are quite unpleasant bugs, but if you try, then you can "agree" with it.
Library installation
The library itself is taken
here . I, unfortunately, cannot say (as they usually say in such cases) that it is necessary to download the latest version. Next, I will explain why. But I still hope that over time the bugs existing in the latest version will be eliminated. After receiving the archive with the library, unpack it into the directory you need on the server.
There is a configuration file in the root directory of the DOMPDF library -
dompdf_config.inc.php . It contains the basic library settings. We are interested in the following three:
')
- the value of the directive DOMPDF_DEFAULT_PAPER_SIZE contains the value of the size of a sheet of paper (a4, letter, etc.);
- In the DOMPDF_DPI directive, the quality of the document specification is indicated in numerical terms. The default is 96;
- the value of the DOMPDF_TEMP_DIR directive should be changed to the value contained in the PHP upload_tmp_dir directive, but only if the value returned by the sys_get_temp_dir PHP function is different from the value of this PHP directive. A full list of possible settings can be found here .
The minimum server configuration should be as follows:
- PHP Version 5.0;
- DOMDocument extension;
- PCRE;
- Zlib;
- MBString extension;
- GD.
It is also recommended to install the
GMagick or
IMagick program and, respectively, connect it via an extension to PHP. Here we can see such a feature: that allegedly installing one of the recommended programs will allow us to work better with transparency of images in PNG format. But in practice, it turned out the following: if none of the programs are installed on the server, then the transparency of PNG images is not processed at all. An exception was for Denwera 3. For PHP 5.3 included in the Denwera 3 package, the need to install in either of these two programs did not arise.
Font installation
Initially, the library has a set of basic fonts, but among them there are not those that would support the Cyrillic alphabet. Therefore, if you need Cyrillic fonts, or there is a need to expand the existing set of ready-made fonts, you will have to install them. And this is no big deal. You can install both TrueType (* .ttf) and OpenType (* .otf) fonts. To work, we need the PHP script
load_font.php , which is located in the root of the DOMPDF library directory. This script must be run from the command line and it takes the following parameters as input:
- font_family - the name of the font;
- n_file - file * .ttf or * .otf;
- {b | i | bi} _file - files corresponding to font styles (bold, italic, bold-italic).
Examples:
./load_font.php slkscr /usr/share/fonts/truetype/slkscr.ttf
./load_font.php Arial /mnt/c_drive/WINDOWS/Fonts/arial.ttf
For the demonstration, we will install the “roboto” font. Assume that the directory with the “roboto” font and its styles is in the “D: \ font” directory. Accordingly, if we need only a font with the “normal” style, then we need to run the script with the following parameters:
./load_font.php Roboto D: \ font \ Roboto-Regular.ttf.
But if you want to install the font “roboto” with all four of its styles (normal, bold, italic and bold-italic), then the script call will have a different look:
./load_font.php Roboto D: \ font \ Roboto-Regular.ttf D: \ font \ Roboto-Bold.ttf
D: \ font \ Roboto-Italic.ttf D: \ font \ Roboto-BoldItalic.ttf.
Pay attention to the order of passing parameters to the script to create a “roboto” font with its four styles at the same time. The first is the path to the font file with the “normal” style, then “bold”, etc. In no case can this order be violated.
Creating a template for DOMPDF
The template presents a regular HTML page with the addition of some new CSS properties that are necessary for the work of the library itself. I will not give all the source code of the template here, since there is no need for this. Moreover, all the necessary files are attached to the article. By the way, you can embed code in PHP and JavaScript into templates. In our case, the template will have six labels: a label for the page number to be set
{start-page} , a label with a color code
{color} for the footer of the page, a label
{page-1} for the text on the first page, a label
{page-2} for the text on the second page and two labels
{label-1} and
{label-2} for displaying the information text in the basement. There will also be a built-in PHP script for primitive drawing and page numbering. Data that may occupy more than one page must be enclosed in a block. You can of course not do this, but then it will not be possible to insert page breaks, and the text may not evenly fill the page. To insert a page break, you should set one of the following properties in the styles of this block:
- page-break-after : always - inserts a break after the page;
- page-break-before : always - inserts a break before the page;
- page-break-inside : auto - meaning that the DOMPDF itself will “make” a decision;
- page-break-inside : avoid - prevents the break inside the element.
Now it is necessary to make numbering of pages. To do this, you need to write a small PHP script and paste it into the template.
if ( isset($pdf) ) { $footer = $pdf->open_object();
Numbering can be done in another way. Create CSS style:
footer .page: after {
content: counter (page);
}
And then in the place you need in the HTML code to insert the structure:
<span> <? php $ PAGE_NUM?> </ span>
But only in the second option, you can not specify the number of the start page. Perhaps we need not to start the numbering from one, and for example from two, etc. In this case, only use the first method.
Note : When working with graphics in embedded scripts, the color value of each of the RGB components must be in the range of 0 - 1. That is, each RGB color component is required to be divided by 255.Creating a module for data export
The simplest thing to do is to export the data. But there is another small bug in the library. It lies in the fact that if we need to generate several files, then we need to create an instance of the
DOMPDF class object for each iteration, otherwise you will get an error message. Below is the source code of the module:
define ( 'ROOT_DIR', dirname ( __FILE__ ) ); define ( 'BASE_PATH', ROOT_DIR . '/template/icon'); define ( 'TEMPLATE', ROOT_DIR . '/template/main.html' );
The result should be a PDF file of the following sample:
findings
Although there are enough various defects in this library, but it's worth it. First, it is free and it should already close all claims to the developers. Secondly, I think that in the near future, if not all, then many bugs will be eliminated. The only problem is that as I wrote above, this library is used more for trivial tasks, so there is little description for solving various problems that arise.
All used files: PDF, template and export module can be downloaded
from here .