Sourcefabric Manuals

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

Booktype 2.3 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. mPDF 6.0 (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 5 and the GD graphics library on Debian jessie or Ubuntu 14.04 GNU/Linux with the command:

sudo apt-get install php5-cli php5-gd unzip

Later versions of these distributions include PHP 7, which has different package names:

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

Next, download and unzip the mPDF file from http://www.mpdf1.com/mpdf/index.php?page=Download on your server, maintaining the directory structure, in a directory such as /var/www/mpdf60/

sudo mkdir -p /var/www/
sudo wget http://mpdf1.com/repos/MPDF60.zip
sudo unzip MPDF60.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/mpdf60/

The output from this command should be similar to:

total 1676
-rw-r--r-- 1 root root  147904 Dec 14 00:36 CHANGELOG.txt
dr-xr-xr-x 2 root root    4096 Mar  5 19:06 classes
drwxr-xr-x 2 root root   12288 Mar  5 19:06 collations
-rw-r--r-- 1 root root    5521 Apr 20  2014 compress.php
-rw-r--r-- 1 root root    8156 Dec  7 19:36 config_fonts-distr-without-OTL.php
-rw-r--r-- 1 root root   10294 Dec  7 18:25 config_fonts.php
-rw-r--r-- 1 root root   13336 Sep 28 14:25 config_lang2fonts.php
-rw-r--r-- 1 root root   26210 Dec  7 19:09 config.php
-rw-r--r-- 1 root root    4453 Dec 21  2013 config_script2lang.php
-rw-r--r-- 1 root root    4926 Jul 20  2010 CREDITS.txt
drwxr-xr-x 2 root root    4096 Mar  5 19:06 examples
drwxr-xr-x 2 root root    4096 Mar  5 19:06 font
drwxr-xr-x 2 root root    4096 Mar  5 19:06 graph_cache
-rw-r--r-- 1 root root   27417 Jul 10  2011 graph.php
drwxr-xr-x 2 root root    4096 Mar  5 19:06 iccprofiles
drwxr-xr-x 2 root root    4096 Mar  5 19:06 includes
-rw-r--r-- 1 root root    9520 Sep 22  2014 lang2fonts.css
-rw-r--r-- 1 root root   18349 Jul 30  2007 LICENSE.txt
-rw-r--r-- 1 root root    2189 Oct 29 22:05 mpdf.css
drwxr-xr-x 3 root root    4096 Mar  5 19:06 mpdfi
-rw-r--r-- 1 root root 1328410 Dec  7 13:01 mpdf.php
drwxr-xr-x 2 root root    4096 Mar  5 19:06 patterns
-rw-r--r-- 1 root root    1209 Sep 19  2010 progbar.css
drwxr-xr-x 3 root root    4096 Mar  5 19:06 qrcode
-rw-r--r-- 1 root root    6009 Dec 14 00:39 README.txt
drwxr-xr-x 2 root root    4096 Mar  5 19:06 tmp
drwxr-xr-x 2 root root    4096 Mar  5 19:06 ttfontdata
drwxr-xr-x 2 root root    4096 Mar  5 19:07 ttfonts
drwxr-xr-x 2 root root    4096 Mar  5 19:07 utils

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/mpdf60/ 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. The examples/test.pdf file should also be writable by the user you are testing mPDF with. To set ownership of this file and these subdirectories to the www-data user, you can use the chown command:

cd /var/www/mpdf60/
sudo chown www-data.www-data examples/test.pdf graph_cache/ tmp/ ttfontdata/

Testing mPDF

You can test that mPDF is working by navigating to the examples directory and running one of the example PHP files there:

cd /var/www/mpdf60/examples/
php example01_basic.php > /tmp/example01_basic.php.pdf

After a couple of seconds, the file example01_basic.pdf should appear in the /tmp directory of your server. If that works, you might like to generate all examples automatically, with the commands:

sudo su www-data
for i in example*.php; do php $i > /tmp/$i.pdf; done

The user account which owns the ttfontdata directory is required to run the more complex examples with embedded fonts. By default, Ubuntu 14.04 LTS (Trusty) and Debian 8 (jessie) do not allow the Apache user www-data to log in and enter commands. If this is the case on your server, in order to follow the example above, you will need to edit the line for www-data in the /etc/passwd file to replace /usr/sbin/nologin with /bin/sh as follows:

www-data:x:33:33:www-data:/var/www:/bin/bash

Custom fonts

mPDF 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/mpdf60/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/mpdf60/ you could edit this file with the command:

sudo nano /var/www/mpdf60/config_fonts.php

Add a new stanza to the array $this->fontdata (which begins on line 85 in mPDF 6.0). 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.  mPDF uses the 'c' parameter in some of its example files which means 'use core system fonts only'. It is the first parameter after the opening bracket:

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

This parameter generates a very small output file, 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 on line 42 of config.php in the mPDF directory:

$this->percentSubset = 100;

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.