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)
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.