In today's world, PDF is defined as the electronic paper. It is popular because it is easy to read, portable and flexible. Generating PDFs is one of the most common features in any web applications. Invoices, reports and certificates are some of the common forms, that require PDF versions of them.
Generating PDFs in PHP can be tricky. The main struggle we have faced was making PDFs look exactly the same way as it looked in HTML. In a previous tutorial, we demonstrated how to use TCPDF. Despite that it is a great package, it is not super friendly with HTML/CSS. The pain to make the PDF look decent with HTML/CSS is unforgettable.
In this tutorial, we will introduce a new library for generating PDFs from HTML.
Snappy is a PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. Under the hood, it is using two popular open source HTML to PDF command line tool. They are wkhtmltopdf and wkhtmltoimage.
Snappy is built with some simple yet very friendly API. We like it because of its simplicity.
It is time to get our hands dirty.
As mentioned earlier, Snappy uses wkhtmltopdf and wkhtmltoimage under the hood. Since both of they are command line tools, we will need to download their binary files.
The process of finding the correct binary files for your operating system and place them in the correct locations can be painful. Fortunately Terry made a composer package, that consolidates all the binaries of wkhtmltopdf and wkhtmltoimage into one place. Now we can install the binary files easily with composer.
Depends on the operating system you are running, you should specify the binary package accordingly. Here is the list of available composer packages.
Run the composer command composer require
in your terminal to install the respective wkhtmltopdf and wkhtmltoimage in your project. Since this tutorial is writen in OSX, we will use the package wkhtmltopdf-binaries-osx:
composer require profburial/wkhtmltopdf-binaries-osx:0.12.1
We also need to install Snappy as the last dependency. We will use composer to install Snappy as normal. Run the command as shown below in your terminal:
composer require knplabs/knp-snappy
Let's create the PdfWriter class, that will be responsible for generating PDF from a HTML file.
PdfWriter's constructor will configure the path to the ** wkhtmltopdf** binary file:
<?php
use Knp\Snappy\Pdf;
class PdfWriter
{
private $binaryPath;
public function __construct()
{
$this->binaryPath = __DIR__ . '/vendor/bin/wkhtmltopdf-amd64-osx';
}
}
Next we will create a write
method that will turn any HTML file into a PDF. write
method accepts a string that represents the path to the target HTML file:
public function write($template)
{
}
In write
method, we will first decide what the generated PDF's filename will be and where to output it. Its filename will be string 'export-' appended with current stamp, and the file will be output at where write
method is invoked:
$generatedPdfFilename = sprintf('export-%s.pdf', time());
$generatedPdfFilePath = __DIR__ . DIRECTORY_SEPARATOR . $generatedPdfFilename;
Next we will instaniate a Snappy instance and use its method generateFromHtml
to generate a PDF:
$snappy = new Pdf($this->binaryPath);
$snappy->generateFromHtml(file_get_contents($template), $generatedPdfFilePath);
Finally we will return the generated PDF's file path:
return $generatedPdfFilePath;
Below is the full source code for the PdfWriter class:
<?php
use Knp\Snappy\Pdf;
class PdfWriter
{
private $binaryPath;
public function __construct()
{
$this->binaryPath = __DIR__ . '/vendor/bin/wkhtmltopdf-amd64-osx';
}
public function write($template)
{
$generatedPdfFilename = sprintf('export-%s.pdf', time());
$generatedPdfFilePath = __DIR__ . DIRECTORY_SEPARATOR . $generatedPdfFilename;
$snappy = new Pdf($this->binaryPath);
$snappy->generateFromHtml(file_get_contents($template), $generatedPdfFilePath);
return $generatedPdfFilePath;
}
}
To use the PdfWriter class, we will first need to include the autoloader from Composer and the PdfWriter class:
include 'vendor/autoload.php';
include 'PdfWriter.php'
Next we will create an instance of the PdfWriter class and call its method write
to generate a PDF:
$pdfWriter = new PDFWriter();
$outputFilePath = $pdfWriter->write('template.html');
The $outputFilePath will contain the path that points to the generated PDF file.
In this tutorial, we have learned how to generate PDF from HTML. In our opinion, generating PDF from HTML file is the most easiest way to create custom PDF. Because it is the easiest to customize the layout and change the look and feel.
In next tutorial, we will see how to use this tactic in CakePHP 3 framework.