H2P - PHP library to convert HTML files to PDF
H2P is a simple library to convert HTML files with images, JS and CSS into PDF files, using the awesome PhantomJS.
If you’re using composer (I strongly recommend that), just add “kriansa/h2p” into your require attribute, like so:
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:
Note that the $input is defined multiple times. That’s just to show you how many URIs are accepted by the Converter.
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)