H2P - PHP library to convert HTML files to PDF

Posted on March 24, 2013

H2P is a simple library to convert HTML files with images, JS and CSS into PDF files, using the awesome PhantomJS.

Fork me on GitHub

Usage

If you’re using composer (I strongly recommend that), just add “kriansa/h2p” into your require attribute, like so:

{
    "name": "acme/blog",
    "require": {
        "kriansa/h2p": "dev-master"
    }
}

Then, you must download the PhantomJS binary on its website. Extract and move the bin/phantomjs (or bin/phantomjs.exe, if you’re using Windows) to H2P/bin/. These bins are not shipped with the main package because they’re too big (about 40mb).

That’s all, it should work fine. Take a look at the sample code below:

<?php

use H2P\Converter;
use H2P\Adapter\PhantomJS;
use H2P\TempFile;

// Set the input content to convert
$input = new TempFile($htmlString);
$input = 'http://www.google.com/';
$input = '/path/to/file.html';

// Then do the conversion
$output = new TempFile();
$instance = new Converter(new PhantomJS(), $input, $output);
$instance->convert();

// Save it somewhere
$output->save('/another/path/to/file.pdf');
// or output it
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Type: application/pdf');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($output->getFileName()));
echo $output->getContent();

Note that the $input is defined multiple times. That’s just to show you how many URIs are accepted by the Converter.

Advanced usage

The Converter constructor accepts 6 params, see below:

Converter::__construct(AdapterAbstract $adapter, string|TempFile $uri, string|TempFile $destination, [string $format, [string $orientation, [string $border]]])

  • AdapterAbstract $adapter

The adapter to convert the file. You can write your own adapter if you don’t like PhantomJS

  • string|TempFile $uri

The input URI to be converted. The package includes a TempFile class which can be used here

  • string|TempFile $destination

The output file. You can use the TempFile here too :)

  • string $format

Converter::FORMAT_A4 - see below a list of page formats that you can use here.

  • string $orientation

Converter::ORIENTATION_PORTRAIT or Converter::ORIENTATION_LANDSCAPE

  • string $border

A string, which can be ‘1cm’, ‘2in’, ‘20px’ or so.

In the file Converter.php, you can see a list of formats (page-sizes) available to convert to. When not specified, the default used is A4.

Format list (from Converter.php)

    const FORMAT_A0 = 'A0';
    const FORMAT_A1 = 'A1';
    const FORMAT_A2 = 'A2';
    const FORMAT_A3 = 'A3';
    const FORMAT_A4 = 'A4';
    const FORMAT_A5 = 'A5';
    const FORMAT_A6 = 'A6';
    const FORMAT_A7 = 'A7';
    const FORMAT_A8 = 'A8';
    const FORMAT_A9 = 'A9';
    const FORMAT_B0 = 'B0';
    const FORMAT_B1 = 'B1';
    const FORMAT_B2 = 'B2';
    const FORMAT_B3 = 'B3';
    const FORMAT_B4 = 'B4';
    const FORMAT_B5 = 'B5';
    const FORMAT_B6 = 'B6';
    const FORMAT_B7 = 'B7';
    const FORMAT_B8 = 'B8';
    const FORMAT_B9 = 'B9';
    const FORMAT_B10 = 'B10';
    const FORMAT_C5E = 'C5E';
    const FORMAT_COMM10E = 'Comm10E';
    const FORMAT_DLE = 'DLE';
    const FORMAT_EXECUTIVE = 'Executive';
    const FORMAT_FOLIO = 'Folio';
    const FORMAT_LEDGER = 'Ledger';
    const FORMAT_LEGAL = 'Legal';
    const FORMAT_LETTER = 'Letter';
    const FORMAT_TABLOID = 'Tabloid';

License

The MIT License (MIT) Copyright (c) 2013 Daniel Garajau Pereira

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.