How to generate a large PDF file with PHP?

Jessica Williams
3 min readJul 13, 2017

--

Generating PDF file is most common task in web development and many libraries are available in PHP –

Tcpdf
Dompdf
Pdflib
Htmldoc
Mpdf

While development, we got the issue that for large pdf file eg: 1000s of pages above libraries are not efficient, take a lot of time to generate PDF or utilize a lot of server resources or many times crash the browser. Above libraries are PHP classes that wrap the HTML and build the pdf so that’s easy to implement but results as a slow performance when PDF have lots of pages.

We advise when you have a need to generate large size PDF then you should use wkhtmltopdf. It’s not a PHP library but build on Perl. You need to install this on your server after that you can write code to generate PDF. We tried on PHP but assume that this will work with other programming languages as well like Ruby,Net, Java or PHP Framework Laravel etc.

wkhtmltopdf is an open source (LGPLv3) command line tools to render HTML into PDF. Wkhtmltopdf is very fast comparing to all other PHP library for PDF like dompdf, TCPDF, FPDF etc. In our case, we generated 641 pages PDF in 3–4 Secs.

Install wkhtmltopdf on server

Step 1- For install wkhtmltopdf, we need to access of SSH. Secure Shell (SSH) is a UNIX-based command interface. If you don’t have access ask Hosting Provider to enable it for you.

Step 2 - Open command line tool like git bash or CMD (windows command line tool) and follow below steps

ssh -p port-number username@ip.ad.re.ss

This is command will ask for password and enter your password

Step 3 - Get latest version of wkhtmtopdf from http://wkhtmltopdf.org/downloads.html and replace below URL in point with latest URL.

wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
tar -xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz

Now installation is complete, you don’t need to move wkhtmltopdf folder to /usr/bin/

Test using below command to generate example.pdf

./wkhtmltopdf http://www.example.com/example.pdf

Implement wkhtmltopdf on PHP

After installation on the server, you need to write code to generate PDF. Below are two methods which we did in PHP but you can write in other programming languages as well.

  • Method 1: Using PHP Library

https://github.com/mikehaertl/phpwkhtmltopdf

require __DIR__ . '/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;

// You can pass a filename, a HTML string, an URL or an options array to the constructor

$pdf = new Pdf('test.html');

// On some systems you may have to set the path to the wkhtmltopdf executable

$pdf->binary = '~/wkhtmltox/bin/wkhtmltopdf';
if (!$pdf->saveAs('test.pdf')) {
echo $pdf->getError();
}

Method 2: Generate PDF without library

echo passthru("xvfb-run -a wkhtmltopdf -T 0 -R 0 -B 0 -L 0 --orientation Portrait --page-size A4 https://www.example.com/ output.pdf 2>&1");

I hope this guide will help you for generating large PDF. Please leave a comment below if you got any issue and I will try to reply as soon as possible.

source by

--

--

Jessica Williams
Jessica Williams

Written by Jessica Williams

Jessica Williams lives in California, USA. She is a software engineer| Freelancer| Remote Worker | Technical Content Writer

Responses (5)