PHP 7 Chrome API to Take Website Screenshots as PDF Document in Browser

You are currently viewing PHP 7 Chrome API to Take Website Screenshots as PDF Document in Browser

PHP 7 Chrome API to Take Website Screenshots as PDF Document in Browser

composer require dawood/phpchromepdf
 

<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-s">'https://youtube.com'</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setOutputDirectory</span>(__DIR__);
<span class="pl-c">//not necessary to set window size</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setWindowSize</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>width</span>=<span class="pl-c1">1477</span>,<span class="pl-s1"><span class="pl-c1">
lt;/span>height</span>=<span class="pl-c1">768</span>);
print <span class="pl-s">"Pdf successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getPdf</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-s">'https://facebook.com'</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setOutputDirectory</span>(__DIR__);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useMobileScreen</span>();
<span class="pl-c">//not necessary to set window size</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setWindowSize</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>width</span>=<span class="pl-c1">768</span>,<span class="pl-s1"><span class="pl-c1">
lt;/span>height</span>=<span class="pl-c1">768</span>);
print <span class="pl-s">"Pdf successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getPdf</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-s">'https://facebook.com'</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setOutputDirectory</span>(__DIR__);
<span class="pl-c">//not necessary to set window size</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setWindowSize</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>width</span>=<span class="pl-c1">1366</span>,<span class="pl-s1"><span class="pl-c1">
lt;/span>height</span>=<span class="pl-c1">1024</span>);
print <span class="pl-s">"Image successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getScreenShot</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-s">'https://facebook.com'</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setOutputDirectory</span>(__DIR__);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useMobileScreen</span>();
 
<span class="pl-c">//not necessary to set window size</span>
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">setWindowSize</span>(<span class="pl-s1"><span class="pl-c1">
lt;/span>width</span>=<span class="pl-c1">768</span>,<span class="pl-s1"><span class="pl-c1">
lt;/span>height</span>=<span class="pl-c1">768</span>);
print <span class="pl-s">"Image successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getScreenShot</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">include</span> <span class="pl-s">'../vendor/autoload.php'</span>;
 
<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-c1">null</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useHtmlFile</span>(__DIR__.<span class="pl-s">'/index.html'</span>);
print <span class="pl-s">"Image successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getScreenShot</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">include</span> <span class="pl-s">'../vendor/autoload.php'</span>;
 
<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-c1">null</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useHtmlFile</span>(__DIR__.<span class="pl-s">'/index.html'</span>);
print <span class="pl-s">"Pdf successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getPdf</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">include</span> <span class="pl-s">'../vendor/autoload.php'</span>;
 
<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-c1">null</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useHtml</span>(<span class="pl-s">"<h2>I am test html</h2>"</span>);
print <span class="pl-s">"Pdf successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getPdf</span>().<span class="pl-c1">PHP_EOL</span>;
print <span class="pl-s">"screenShot successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getScreenShot</span>().<span class="pl-c1">PHP_EOL</span>;

<span class="pl-k">include</span> <span class="pl-s">'../vendor/autoload.php'</span>;
 
<span class="pl-k">use</span> dawood\phpChrome\<span class="pl-v">Chrome</span>;
 
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>=<span class="pl-k">new</span> <span class="pl-v">Chrome</span>(<span class="pl-c1">null</span>,<span class="pl-s">'/usr/bin/google-chrome'</span>);
<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">useHtml</span>(<span class="pl-s">"<h2>I am test html</h2>"</span>);
print <span class="pl-s">"Pdf successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getPdf</span>(<span class="pl-s">"/tmp/mypdf.pdf"</span>).<span class="pl-c1">PHP_EOL</span>;
print <span class="pl-s">"screenShot successfully generated :"</span>.<span class="pl-s1"><span class="pl-c1">
lt;/span>chrome</span>-><span class="pl-en">getScreenShot</span>(<span class="pl-s">"/tmp/hello/test.jpg"</span>).<span class="pl-c1">PHP_EOL</span>;

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

wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub <span class="pl-k">|</span> sudo apt-key add -
sudo sh -c <span class="pl-s"><span class="pl-pds">'</span>echo "deb http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list<span class="pl-pds">'</span></span>
 
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

Just Install updated version of chrome(after V 61.*)  
that-s it
 

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply