<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Minicli | Nerdpress.org</title>
	<atom:link href="https://nerdpress.org/tag/minicli/feed/" rel="self" type="application/rss+xml" />
	<link>https://nerdpress.org</link>
	<description>...dev, tech problems and solutions.</description>
	<lastBuildDate>Mon, 31 Jul 2023 06:33:43 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Testing PDF creation with headless chrome and PHP</title>
		<link>https://nerdpress.org/2023/07/28/testing-pdf-creation-with-headless-chrome-and-php/</link>
		
		<dc:creator><![CDATA[Ivo Bathke]]></dc:creator>
		<pubDate>Fri, 28 Jul 2023 09:56:50 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Minicli]]></category>
		<category><![CDATA[pdf]]></category>
		<guid isPermaLink="false">https://nerdpress.org/?p=3249</guid>

					<description><![CDATA[<p>I had the task the other day to use a headless chrome to generate PDF files from websites in a PHP app. The plan was to use chrome-php with a headless chrome to generate the PDF. Usually you would install chrome/chromium on a linux server via apt and just run chrome from the PATH with &#8230; </p>
<p class="link-more"><a href="https://nerdpress.org/2023/07/28/testing-pdf-creation-with-headless-chrome-and-php/" class="more-link">Continue reading<span class="screen-reader-text"> "Testing PDF creation with headless chrome and PHP"</span></a></p>
The post <a href="https://nerdpress.org/2023/07/28/testing-pdf-creation-with-headless-chrome-and-php/">Testing PDF creation with headless chrome and PHP</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></description>
										<content:encoded><![CDATA[<p>I had the task the other day to use a headless chrome to generate PDF files from websites in a PHP app.</p>



<p>The plan was to use <a href="https://github.com/chrome-php/chrome" target="_blank" rel="noopener" title="">chrome-php</a> with a headless chrome to generate the PDF.</p>



<p>Usually you would install chrome/chromium on a linux server via <em>apt</em> and just run chrome from the PATH with chrome.<br />Since i was on shared hosting i was not sure if this was possible since i was not allowed to run <em>apt</em> commands.<br />So i tried to use Puppeteer which ships a headless chrome executable and use just this directly.<br />I installed Puppeteer with <em>npm</em> locally and uploaded the chrome executable to the shared hosting.<br />Puppeteer will place the headless chrome in the .cache dir in your home directory, f.e.:</p>



<p><code>~/.cache/puppeteer/chrome/linux-113.0.5672.63/chrome-linux64/chrome</code></p>



<p>Upload:</p>



<p><code>scp -r ~/.cache/puppeteer/chrome/linux-113.0.5672.63/chrome-linux64 me@sharedhosting:/usr/home/test</code></p>



<span id="more-3249"></span>



<p>I also created a nice little command line tool to create the pdf with chrome-php.<br />I used <a href="https://github.com/minicli/minicli" target="_blank" rel="noopener" title="">minicli</a> which is a &#8220;minimalist, dependency-free framework for building CLI-centric PHP applications&#8221;.<br />So you can create cli commands fast and without all the clutter, exactly what i needed for this small test.<br />You can find the code on Github: <a href="https://github.com/ivoba/chrome-php-minicli" target="_blank" rel="noopener" title="">https://github.com/ivoba/chrome-php-minicli</a><br />The command is here: <a href="https://github.com/ivoba/chrome-php-minicli/blob/main/command.php" target="_blank" rel="noopener" title="">https://github.com/ivoba/chrome-php-minicli/blob/main/command.php</a></p>



<p>Then i tried to generate the pdf:<br /><code>CHROME_PATH="/usr/home/test/chrome" php command.php pdf url=https://ivo-bathke.name</code></p>



<p>However this failed because the server lacked required dependencies to run the chrome executable.<br />You can check this by running this command:</p>



<p><code>me@sharedhosting:/usr/home/test$ ldd chrome-linux64/chrome | grep not<br />libatk-1.0.so.0 =&gt; not found<br />libatk-bridge-2.0.so.0 =&gt; not found<br />libxkbcommon.so.0 =&gt; not found<br />libatspi.so.0 =&gt; not found<br />libXcomposite.so.1 =&gt; not found<br />libXrandr.so.2 =&gt; not found<br />libgbm.so.1 =&gt; not found</code></p>



<p>Ok, so it wont work with the uploaded chrome executable.<br />What a pity but still a finding.</p>



<p>In the end i contacted the support and fortunatly they were willing and able to install chromium via <em>apt</em>. Nice!<br /><br />Eventually it worked with:<br /><code>CHROME_PATH="chromium" php command.php pdf url=https://ivo-bathke.name</code></p>



<p>Pdf created :)</p>



<p></p>The post <a href="https://nerdpress.org/2023/07/28/testing-pdf-creation-with-headless-chrome-and-php/">Testing PDF creation with headless chrome and PHP</a> first appeared on <a href="https://nerdpress.org">Nerdpress.org</a>.]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
