PHP Classes
elePHPant
Icontem

PHP Chrome PDF: Generate PDF or images of HTML pages with Chrome

Recommend this page to a friend!

  Author Author  
Name: Dawood Ikhlaq <contact>
Classes: 3 packages by
Country: Italy Italy
Age: 23
All time rank: 3096123 in Italy Italy
Week rank: 45 Up2 in Italy Italy Up
Innovation award
Innovation award
Nominee: 1x


  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This class can generate PDF or images of HTML pages with Chrome(headless).

It can take the URL of Web page or a local HTML page file and runs the Chrome(headless) browser to display the given page.

The class can make Chrome generate a PDF file or an image file from the opened page.

Details

PHP ChromeToPdf

Latest Stable Version Total Downloads License

PHP ChromeToPdf provides a simple and clean interface to ease PDF and image creation with Google Chrome. The Google Chrome must be installed and working on your system. See the section below for details.

History

Every time i had convert url to pdf or to take screenshot of some url i had to install several libraries , i know there are very good libraries available like phantomJs,wkhtmltopdf and some others... but there are always rendering issue like some supports bootstrap ,some not some time html5 problem etc.. and this was all annoying so i decided to make this little wrapper around chrome by using chrome results i achieved following results. No more css issues (bootstrap,css3 )* No more html5 issues* No more need to use xvfb*

in-fact painless conversion

Installation

Install the package through composer:

composer require dawood/phpchromepdf

Make sure, that you include the composer autoloader somewhere in your codebase.

Examples

There are several examples provided in examples folder too

Url To PDF (accessing site as bigger browser)


use dawood\phpChrome\Chrome;


$chrome=new Chrome('https://youtube.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
//not necessary to set window size
$chrome->setWindowSize($width=1477,$height=768);
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;



Url To PDF (accessing site as mobile browser)


use dawood\phpChrome\Chrome;

$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
$chrome->useMobileScreen();
//not necessary to set window size
$chrome->setWindowSize($width=768,$height=768);
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;

Take screenshot of url (accessing site as bigger browser)


use dawood\phpChrome\Chrome;


$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
//not necessary to set window size
$chrome->setWindowSize($width=1366,$height=1024);
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;



Take screenshot of url (accessing site as mobile browser)


use dawood\phpChrome\Chrome;


$chrome=new Chrome('https://facebook.com','/usr/bin/google-chrome');
$chrome->setOutputDirectory(__DIR__);
$chrome->useMobileScreen();

//not necessary to set window size
$chrome->setWindowSize($width=768,$height=768);
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;


Take screenshot of Html File


include '../vendor/autoload.php';

use dawood\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtmlFile(__DIR__.'/index.html');
print "Image successfully generated :".$chrome->getScreenShot().PHP_EOL;



convert Html file to pdf


include '../vendor/autoload.php';

use dawood\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtmlFile(__DIR__.'/index.html');
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;


convert Html code to pdf / screenshot


include '../vendor/autoload.php';

use dawood\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtml("<h2>I am test html</h2>");
print "Pdf successfully generated :".$chrome->getPdf().PHP_EOL;
print "screenShot successfully generated :".$chrome->getScreenShot().PHP_EOL;


convert Html code to pdf / screenshot and save at desired location


include '../vendor/autoload.php';

use dawood\phpChrome\Chrome;

$chrome=new Chrome(null,'/usr/bin/google-chrome');
$chrome->useHtml("<h2>I am test html</h2>");
print "Pdf successfully generated :".$chrome->getPdf("/tmp/mypdf.pdf").PHP_EOL;
print "screenShot successfully generated :".$chrome->getScreenShot("/tmp/hello/test.jpg").PHP_EOL;


Setting options

The google-chrome shell command accepts different types of options: for complete list of options you can visit https://peter.sh/experiments/chromium-command-line-switches/

Wrapper Methods

* constructor: Accepts $url to visit(for pdf/screenshot) as first parameter you can pass this as null and later use setUrl, and second parameter is binary path of google-chrome installed in your system as second parameter if no binaryPath is provided is uses default location /usr/bin/google-chrome but you still can provide binary path later using setBinaryPath, constructor also put some default arguments like headless , disable-gpu which are necessary for google-chrome to work on cli * setBinaryPath which accepts binary path and set it for you * setArguments to set options of google-chrome it accepts array of options in a format

 [
    $argument1=>$value1,
    $argument2=>$value2,  
 ]

if your argument doesn't has values like --headless you can pass empty value e.g [--headless=>''] * setArgument to set option of google-chrome it accepts two parameter $argument , $value if your argument doesn't has a value like --headless you can pass empty value e.g setArgument('--headless','')

  • setChromeDirectory the directory where google-chrome will save your profile it is not mandatory as google-chrome by default uses some directory but in need you can use this method to change that
  • setUrl to set the url to convert to pdf or to take screenshot
  • useHtmlFile to use the file instead of url to convert to pdf or to take screenshot
  • useHtml to use the html code instead of url to convert to pdf or to take screenshot
  • setOutputDirectory directory to save the output (screenshots and pdf) the default directory is temporary directory of your operating system
  • getPdf it receives optional path parameter to save the pdf file at if not provided it will save in output directory or temp directory of your operating system depending if you properly set up the output directory, for this check setOutputDirectory option, it will convert your provided url to pdf and return the location of newly saved pdf
  • getScreenShot it receives optional path parameter to save the pdf file at if not provided it will save in output directory or temp directory of your operating system depending if you properly set up the output directory for this check setOutputDirectory option, it will take screenshot of your provided url and return the location of newly saved image
  • setWindowSize you can set the chrome window size using this method it accepts two parameters $width and $height
  • useMobileScreen ask chrome to access site as mobile browser
  • getArguments returns all the arguments set

there are some other getters available too in case you need getUrl , getBinaryPath , getOutPutDirectory

Note for Windows users

until now i can't properly test this on windows so if you wanna try please try it and if it goes successful please send me a pull request Thanks

Installation of google-Chrome

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add - 
sudo sh -c 'echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'

sudo apt-get install libxss1 libappindicator1 libindicator7 libosmesa6

sudo apt-get update
sudo apt-get install -y google-chrome-stable

sudo ln -s /usr/lib/x86_64-linux-gnu/libOSMesa.so.6 /opt/google/chrome/libosmesa.so

then try running google-chrome from shell to verify it's installation

License

The PhpChromeToPdf is open-sourced software licensed under the MIT license.

Contribution

Thanks to all of the contributors , fork this repository and send me a pull request

Author

Dawood Ikhlaq and Open source community


  Classes of Dawood Ikhlaq  >  PHP Chrome PDF  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Chrome PDF
Base name: phpchrometopdf
Description: Generate PDF or images of HTML pages with Chrome
Version: 1.0.1
PHP version: 5
License: MIT/X Consortium License
All time users: 264 users
All time rank: 7406
Week users: 8 users
Week rank: 75 Up
 
  Groups   Rate classes User ratings   Applications   Files Files  

  Groups  
Group folder image HTML HTML generation and processing View top rated classes
Group folder image PHP 5 Classes using PHP 5 specific features View top rated classes
Group folder image Graphics Colors, images, charts and picture formats View top rated classes
Group folder image Files and Folders Listing, accessing and manipulating files and folders View top rated classes


  Recommendations  

What is the best PHP pdf to image class?
Need to convert PDF file to image

What is the best PHP pdf creation class?
CREATE PDF


  Innovation Award  
PHP Programming Innovation award nominee
July 2017
Nominee
Vote
Many applications need to generate documents for printing. They usually generate documents in PDF format.

Composing a PDF document directly is usually hard. A simpler solution is to generate an HTML page and convert it to PDF.

One way to perform such conversion is to use a browser. This class provides means to use the Chrome browser from the command line and render a HTML page to output it as a PDF document.

Manuel Lemos

  User ratings  
Not enough user ratings

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Files folder imageexamples (9 files)
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  examples  
File Role Description
  Accessible without login HTML file index.html Data Auxiliary data
  Accessible without login Plain text file pdf.php Example Example script
  Accessible without login Plain text file pdfFromHtmlCode.php Example Example script
  Accessible without login Plain text file pdfFromHtmlFile.php Example Example script
  Accessible without login Plain text file pdfInDefaultDirectory.php Example Example script
  Accessible without login Plain text file pdfMobileScreen.php Example Example script
  Accessible without login Plain text file screenshot.php Example Example script
  Accessible without login Plain text file screenshotFromHtmlFile.php Example Example script
  Accessible without login Plain text file screenShotMobileScreen.php Example Example script

  Files folder image Files  /  src  
File Role Description
  Plain text file Chrome.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file ChromeTestSuite.php Class Class source

Install with Composer Install with Composer - Download Download all files: phpchrometopdf.tar.gz phpchrometopdf.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.