Converting HTML to Pdf Using PHP

Converting HTML to PDF using PHP?

If you wish to create a pdf from php, pdflib will help you (as some others suggested).

Else, if you want to convert an HTML page to PDF via PHP, you'll find a little trouble outta here.. For 3 years I've been trying to do it as best as I can.

So, the options I know are:

DOMPDF : php class that wraps the html and builds the pdf. Works good, customizable (if you know php), based on pdflib, if I remember right it takes even some CSS. Bad news: slow when the html is big or complex.

HTML2PS: same as DOMPDF, but this one converts first to a .ps (ghostscript) file, then, to whatever format you need (pdf, jpg, png). For me is little better than dompdf, but has the same speed problem.. but, better compatibility with CSS.

Those two are php classes, but if you can install some software on the server, and access it throught passthru() or system(), give a look to these too:

wkhtmltopdf: based on webkit (safari's wrapper), is really fast and powerful.. seems like this is the best one (atm) for converting html pages to pdf on the fly; taking only 2 seconds for a 3 page xHTML document with CSS2. It is a recent project, anyway, the google.code page is often updated.

htmldoc : This one is a tank, it never really stops/crashes.. the project looks dead since 2007, but anyway if you don't need CSS compatibility this can be nice for you.

Convert html page into pdf using php

Preparation

In the interest of complete clarity, I'm going to take you through all of the steps I use to make this work on Windows with Wamp.

When you install Wamp on windows, the Apache document root gets set to c:/wamp64/www or something similar by default. This is a good thing because you can then set up virtual hosts on your Windows machine which will allow you to visit actual urls in your browser (like dev.mpdftest.com for example) instead of having to visit localhost/my-project-directory.

So the first thing to do is create a folder in the www directory, calling it whatever you want (I called mine mpdf_test).

Next, set up a virtual host for that project. To do that:

  1. Left-click the Wamp icon in your system tray.
  2. Under the 'Apache' menu item, click the 'httpd-vhosts.conf' file - this will open it in your text editor.
  3. If you have never added a virtual host, there will only be one entry in this file (for localhost) that looks like this:

    Sample Image

  4. Copy that entire entry and paste it underneath, making appropriate changes to point at your new project directory. Here's how mine looks (you can make yours the same, just change the project folder name to whatever you called yours):

    Sample Image

  5. Save this file.

  6. Open 'C:\Windows\System32\drivers\etc\hosts' in your text editor.

  7. Create an entry for your new site directly below the entry for localhost, using the ServerName from the virtual hosts entry, like this:

    Sample Image

  8. Save this file.

  9. Restart your Wamp services by left-clicking the Wamp icon in your system tray and clicking 'Restart All Services'.

  10. You should now be able to visit dev.yourprojectname.com in your browser. You can add a simple index.php file to your project that just prints "Hello World" to test this.

Installing Composer

To install composer on Windows, you should be able to just download and execute (double-click on) this file. Just accept all the defaults.

Installing Mpdf In Your Project

  1. Switch into your project directory in a terminal window (command prompt on Windows) and execute the following command:

    • composer require mpdf/mpdf
  2. This will add the vendor directory (which includes mpdf) to your project. Your project structure will look like this:

    Sample Image

  3. You need to change the permissions for three of the sub-directories. If you don't already have it, install Git for Windows.

  4. One of the programs that will be installed is called Git BASH. It gives you the ability to set file permissions the same way you would in Linux or on a Mac. Run this program and cd into your project directory:

    • cd /c/wamp64/www/your_project_directory/
  5. Execute the commands below:

    • chmod 775 vendor/mpdf/mpdf/ttfontdata
    • chmod 775 vendor/mpdf/mpdf/tmp
    • chmod 775 vendor/mpdf/mpdf/graph_cache

Testing Your Mpdf Installation

  1. If you have not done so already, create an index.php file in your project directory with the following code:

    Sample Image

  2. Save that file and visit your site in your browser (dev.yoursitename.com). If you have done everything correctly, you should be immediately redirected to a PDF. The sole contents of that PDF will be the h1 you defined in your code. Here's mine:

    Sample Image

How Can I add HTML And CSS Into PDF

Important:
Please note that this answer was written in 2009 and it might not be the most cost-effective solution today in 2019. Online alternatives are better today at this than they were back then.

Here are some online services that you can use:

  • PDFShift
  • Restpack
  • PDF Layer
  • DocRaptor
  • HTMLPDFAPI
  • HTML to PDF Rocket

Have a look at PrinceXML.

It's definitely the best HTML/CSS to PDF converter out there, although it's not free (But hey, your programming might not be free either, so if it saves you 10 hours of work, you're home free (since you also need to take into account that the alternative solutions will require you to setup a dedicated server with the right software)

Oh yeah, did I mention that this is the first (and probably only) HTML2PDF solution that does full ACID2 ?

PrinceXML Samples

How to convert html into pdf with php?

DOMpdf is the best free one.

If money isn't an issue, PrinceXML is best.

Converting html to pdf in php?

Use wkhtmltopdf via a system call. See How to install wkhtmltopdf on a linux based (shared hosting) web server for installation help.

wkhtmltopdf is a command line program
which permits to create a pdf from an
url, a local html file or stdin. It
produces a pdf like rendred with the
WebKit engine.

See the sample from this page here.

php code tested on Ubuntu (you'll need to change the /tmp/ to a temporary directory on Windows):

$url = 'http://www.google.com';
$pdffile = tempnam('/tmp/', 'wkhtmltopdf_');
$handle = popen("wkhtmltopdf $url $pdffile 2>&1", "r");
while (!feof($handle))
fread($handle, 4096);
pclose($handle);
header('Content-type: application/pdf');
$file = fopen($pdffile, "r");
while(!feof($file))
print fread($file, 4096);
unlink($pdffile);

There are also php bindings which removes the need to use a system call yourself, which is a simpler (and safer!) option.

try {
$wkhtmltopdf = new Core_Wkhtmltopdf(array('path' => APPLICATION_PATH . '/../public/uploads/'));
$wkhtmltopdf->setTitle("Title");
$wkhtmltopdf->setHtml("Content");
$wkhtmltopdf->output(Wkhtmltopdf::MODE_DOWNLOAD, "file.pdf");
} catch (Exception $e) {
echo $e->getMessage();
}

Converting HTML to PDF (not PDF to HTML) using PHP

Regarding wkhtmltopdf:

  • This thing works blazingly fast and it can also handle all kinds of HTML/CSS you throw at it, so when you need speed, you should seriosly consider it. We switch to it recently in our company and our PDF serving got enourmous speed-boost.

  • At least under Linux it needs XOrg libraries to be installed - servers usually don't have them, so that might be your problem.



Related Topics



Leave a reply



Submit