Sourcefabric Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Booktype 2.4 for Authors and Publishers

Installing a PDF renderer

Booktype can use a variety of renderers to convert a book's HTML chapters into a single PDF file for export. Of all the known open source PDF renderers, mPDF 6 (http://www.mpdf1.com) is currently recommended, due to its extensive support for pre-press features including CMYK and spot colours, bleeds and crop marks, right-to-left and bi-directional text, and typographical hints in non-Latin scripts. mPDF is written by Ian Back, based on FPDF and HTML2FPDF with a number of enhancements, and released as Free Software under the GNU GPL v2.

Installing mPDF

mPDF requires PHP's (http://php.net/) command-line interpreter to be installed on the server. You can install the command-line interpreter for PHP and the GD graphics library on Debian stretch or Ubuntu 18.04 GNU/Linux with the command:

sudo apt install php-cli php-mbstring php-gd unzip

Next, download and unzip the mPDF file on your server, maintaining the directory structure, in a directory such as /var/www/mpdf-6.1.3/ as follows:

sudo mkdir -p /var/www/
cd ~
sudo wget https://github.com/mpdf/mpdf/archive/v6.1.3.zip
sudo unzip v6.1.3.zip -d /var/www/

The unzipped mPDF files should now belong to the root user, which you can confirm with the command:

ls -l /var/www/mpdf-6.1.3/

The output from this command should be similar to:

total 1744
-rw-r--r-- 1 root root  146414 Dec 12  2016 CHANGELOG.txt
drwxr-xr-x 2 root root    4096 Dec 12  2016 classes
drwxr-xr-x 2 root root   12288 Dec 12  2016 collations
-rw-r--r-- 1 root root     941 Dec 12  2016 composer.json
-rw-r--r-- 1 root root    5399 Dec 12  2016 compress.php
-rw-r--r-- 1 root root    7932 Dec 12  2016 config_fonts-distr-without-OTL.php
-rw-r--r-- 1 root root    9939 Dec 12  2016 config_fonts.php
-rw-r--r-- 1 root root   13333 Dec 12  2016 config_lang2fonts.php
-rw-r--r-- 1 root root   25400 Dec 12  2016 config.php
-rw-r--r-- 1 root root    4371 Dec 12  2016 config_script2lang.php
-rw-r--r-- 1 root root    4829 Dec 12  2016 CREDITS.txt
drwxr-xr-x 2 root root    4096 Dec 12  2016 font
drwxr-xr-x 2 root root    4096 Dec 12  2016 graph_cache
-rw-r--r-- 1 root root   26044 Dec 12  2016 graph.php
drwxr-xr-x 2 root root    4096 Dec 12  2016 iccprofiles
drwxr-xr-x 2 root root    4096 Dec 12  2016 includes
-rw-r--r-- 1 root root    9126 Dec 12  2016 lang2fonts.css
-rw-r--r-- 1 root root   18009 Dec 12  2016 LICENSE.txt
-rw-r--r-- 1 root root    2105 Dec 12  2016 mpdf.css
-rw-r--r-- 1 root root      50 Dec 12  2016 MpdfException.php
-rw-r--r-- 1 root root 1142942 Dec 12  2016 mpdf.php
drwxr-xr-x 2 root root    4096 Dec 12  2016 patterns
-rw-r--r-- 1 root root     366 Dec 12  2016 phpunit.xml
-rw-r--r-- 1 root root    1143 Dec 12  2016 progbar.css
drwxr-xr-x 3 root root    4096 Dec 12  2016 qrcode
-rw-r--r-- 1 root root    2355 Dec 12  2016 README.md
-rw-r--r-- 1 root root  261861 Dec 12  2016 Tag.php
drwxr-xr-x 2 root root    4096 Dec 12  2016 tmp
drwxr-xr-x 2 root root    4096 Dec 12  2016 ttfontdata
drwxr-xr-x 2 root root    4096 Dec 12  2016 ttfonts

To use mPDF with Booktype, the directory you install mPDF in and its subdirectories must be readable by the web server, such as the www-data user in the case of Apache on Debian or Ubuntu GNU/Linux. By default, the /var/www/mpdf-6.1.3/ directory will be owned by root and have chmod 755 permissions, enabling entry and read access for all users.

The subdirectories graph_cache/ and tmp/ and ttfontdata/ must also be writable by the web server user. To set ownership of these subdirectories to the www-data user, you can use the chown command:

cd /var/www/mpdf-6.1.3/
sudo chown www-data.www-data graph_cache/ tmp/ ttfontdata/

Testing mPDF

You can test that mPDF is working by creating a small PHP file in your home directory which includes the mPDF installation path.

nano ~/example.php

For example, this file example.php could contain:

<?php

include("/var/www/mpdf-6.1.3/mpdf.php");

$mpdf=new mPDF('c');
$mpdf->WriteHTML('<h1>Hello World!</h1>');
$mpdf->Output('/tmp/example.pdf');

exit;

?>

Then use PHP to execute the script:

php ~/example.php

If mPDF is installed correctly, the file example.pdf should appear in the /tmp directory of your server, with the text content 'Hello World!' as used in the example.

Custom fonts for standalone mPDF output

Booktype sets up the theme fonts it needs for mPDF output when you publish a book. For standalone mPDF use, mPDF 6 is bundled with a number of open source fonts, including DejaVu Sans and Free Serif. Additional OpenType and TrueType fonts can be installed in the ttfonts/ subdirectory of your mPDF installation, using a command such as:

sudo cp *.ttf /var/www/mpdf-6.1.3/ttfonts/

The file config_fonts.php must also be updated to list the new filenames for regular (R), bold (B), italic/oblique (I) and bold italic/oblique (BI) variants of the fonts you have installed. For example, if mPDF was installed in /var/www/mpdf-6.1.3/ you could edit this file with the command:

sudo nano /var/www/mpdf-6.1.3/config_fonts.php

You can add a new stanza to the array $this->fontdata (which begins on line 83 in mPDF 6.1.3). mPDF converts font names to lower case. For a font referred to in a book's CSS file as:

font-family: FreeMono; 

the stanza added to the config_fonts.php file might be:

     "freemono" => array(
             'R' => "FreeMono.ttf",
             'B' => "FreeMonoBold.ttf",
             'I' => "FreeMonoOblique.ttf",
             'BI' => "FreeMonoBoldOblique.ttf",
             ),

When using custom fonts, mPDF should be configured to embed the fonts specified in the book's CSS file, instead of the PDF viewer falling back to core fonts. The mPDF 'c' parameter means 'use core system fonts only'. It is the first parameter after the opening bracket, for example:

$mpdf=new mPDF('c','A5','','',22,14,12,23,9,9);

This parameter generates a very small output file size, but there is no font embedding at all, and therefore any custom fonts will not be visible in the PDF. If you do not set the first parameter, then the custom fonts specified in CSS should be embedded:

$mpdf=new mPDF('','A5','','',22,14,12,23,9,9);

Font subsetting

mPDF has a configurable threshold for when a font should be subset, which reduces the PDF file size by excluding the embedding of unused characters.

If the threshold is set to the default of 30%, mPDF will embed the whole font if more than 30% of the characters in that font are found in the PDF. To force subsetting for all fonts, set percentSubset to 100 in the file config.php found in the mPDF directory:

$this->percentSubset = 100;

In mPDF 6.1.3 the percentSubset setting is found on line 40 of the config.php file.

There is also an 's' parameter which forces subsetting, regardless of the value in the config.php file. It can be used in example files like so:

 $mpdf=new mPDF('s','A5','','',22,14,12,23,9,9);

This use of subsetting can produce a medium-sized file with custom fonts.

There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.