PDF Generation

Generating PDFs

You can easily generate PDFs from HTML files using mikehaertl/phpwkhtmltopdf.

You can find clear documentation for this project in it's README.

This library makes use of wkhtmltopdf. This is an external tool and will need to be installed on your server for this library to work.

Installing Wkhtmltopdf


Don't install wkhtmltopdf from your package manager. This will be missing a number of features and will require a graphical environment to be installed / running on your server.

  • First you need to grab the latest version of wkhtmltopdf for your server from here.
  • Next you need to install the package. To install the package use your servers normal package management tool ie yum / dnf rather than rpm and apt rather than dpkg as they'll resolve and install any dependencies for you.
    yum install wkhtmltox-*.centos7.x86_64.rpm

Stitching PDFs

To stitch PDFs together you can make use of Ghostscript.

This is another external tool and will need to be installed on your server.

Installing Ghostscript

This one's nice and simple as you can just use your distro's package manager.

yum install ghostscript

Using Ghostscript

You can use the following command to stitch together a number of PDFs

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=all_parts.pdf part1.pdf part2.pdf part3.pdf

This can easily be wrapped in a PHP class using exec to create a PDF stitcher for your project.