Skip to content
This repository has been archived by the owner on Dec 20, 2017. It is now read-only.

Provides an easy interface to generate pdfs using phantomjs's rendering engine

License

Notifications You must be signed in to change notification settings

ababkov/phantomjs-html-to-pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

phantomjs-html-to-pdf

PHP library that provides an easy interface to phantomjs HTML to PDF / rasterize capability.

Good alternative to wkhtmltopdf.

This library does depend on the phantom js being installed on your system. Binaries can be found here: http://phantomjs.org/download.html

Custom Binary Path

If the phantomjs binary is not in your system path you should call setBinaryPath. You may also need to change examples/config.php $CONFIG.

Usage Examples

The class can be used to render either local html content (from a variable), a remote url or a local file.

Render Remote Url

$renderer = new Rex\PhantomJs\Renderer();
$renderer->setHtmlContentFromUri("http://www.google.com");
$output_file = $renderer->save();

Render HTML content from variable

$renderer = new Rex\PhantomJs\Renderer();
$renderer->setHtmlContent("<h1>Hello</h1>");
$output_file = $renderer->save();

Serve for Download

use Rex\PhantomJs\Constants;

$renderer = new Rex\PhantomJs\Renderer();
$renderer->setHtmlContentFromUri("http://www.google.com");
$output_file = $renderer->save();

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($output_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($output_file));
ob_clean();
flush();
readfile($output_file);
exit();

Setting Options

$renderer = new Rex\PhantomJs\Renderer();
$renderer->setHtmlContentFromUri("http://www.google.com");
$renderer->setOption(Constants::OPTION_MARGIN,"1cm");
$output_file = $renderer->save();

Available Options

  • format : The page format e.g. 'A4', '10cm20cm' or any of the Constants::FORMAT_ constants
  • margin_left : The left margin as an int / float + a unit. E.g. 1cm or 1.1in
  • margin_right : The right margin as an int / float + a unit. E.g. 1cm or 1.1in
  • margin_top : The top margin as an int / float + a unit. E.g. 1cm or 1.1in
  • margin_bottom : The bottom margin as an int / float + a unit. E.g. 1cm or 1.1in
  • orientation : The orientation: Constants::ORIENTATION_PORTRAIT, Constants::ORIENTATION_LANDSCAPE
  • zoom : The zoom level where 1 is 100%. e.g. for 140% use 1.4
  • header_html : Html to be used in the header. Use {{page_number}} for the page number, {{total_pages}} for the total pages. Ensure you also set the header height option.
  • footer_html : Html to be used in the footer. Use {{page_number}} for the page number, {{total_pages}} for the total pages. Ensure you also set the footer height option.
  • header_height : The height of the footer as an int / float + a unit. E.g. 1cm or 1.1in
  • footer_height : The height of the header as an int / float + a unit. E.g. 1cm or 1.1in

Examples

If the binary isn't in your path, make sure you set it in examples/config.php.

About

Provides an easy interface to generate pdfs using phantomjs's rendering engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published