KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Export package error using chrome binary #557

Open EGROW SOLUTIONS, LLC opened this topic on on Dec 11, 2018 - 14 comments

EGROW SOLUTIONS, LLC commented on Dec 11, 2018

Hello,

Trying to create pdf using chrome binary and getting the following error

Uncaught Exception: An error occurred while creating PDF ; <255> with message "Failed without error message: C: && cd "C:\Program Files (x86)\Google\Chrome\Application" && chrome --print-to-pdf="C:\Windows\Temp\5c10413c8db8e2.pdf" --headless --disable-gpu --incognito --enable-viewport

David Winterburn commented on Dec 12, 2018

Hi there,

Would you please post your php code for exporting? Thanks!

EGROW SOLUTIONS, LLC commented on Dec 12, 2018

Here's the code

               require_once("lib/koolreport/autoload.php");
               $printTemplate = "myTemplate.php";
               $clsExport = new \koolreport\instant\Exporter;
               $clsExport::export($printTemplate)
                ->pdf(array(
                    "chromeBinary" => "C:\Program Files (x86)\Google\Chrome\Application\chrome",
                    "format"=>"Letter",
                    "orientation"=>"portrait",
                    "margin"=>array(
                      "top"=>"0.5in",
                      "bottom"=>"0.5in",
                      "left"=>"0.5in",
                      "right"=>"0.5in"
                    )
                ))
                ->toBrowser("exported.pdf",true);
David Winterburn commented on Dec 13, 2018

Hi there,

Please check if there's the file chrome.exe in this folder on your machine and let us know: "C:\Program Files (x86)\Google\Chrome\Application"

On another note, chrome binary running in headless mode has very limited ability. It doesn't support format, orientation or margin properties. To support that you had better install nodejs to use with our export function and use "nodeBinary" => "C:\Program Files\nodejs\node.exe" instead.

EGROW SOLUTIONS, LLC commented on Dec 13, 2018

yes, chrome.exe is present in C:\Program Files (x86)\Google\Chrome\Application ok will try with node.js

EGROW SOLUTIONS, LLC commented on Dec 13, 2018

Tried using nodejs - installed using node js installer for windows., getting the following error.

Failed to generate PDFNULL Fatal error: Uncaught Error: Call to a member function toBrowser() on null

I see the temp html is generated in c:\windows\temp

require_once("lib/koolreport/autoload.php");
               $printTemplate = "myTemplate.php";
               $clsExport = new \koolreport\instant\Exporter;
               $clsExport::export($printTemplate)
                ->pdf(array(
                    "nodeBinary"=>"C:\Program Files\nodejs\node.exe",
                    "format"=>"Letter",
                    "orientation"=>"portrait",
                    "margin"=>array(
                      "top"=>"0.5in",
                      "bottom"=>"0.5in",
                      "left"=>"0.5in",
                      "right"=>"0.5in"
                    )
                ))
                ->toBrowser("exported.pdf",true);
David Winterburn commented on Dec 14, 2018

Hi,

At the moment we haven't publicly released this feature yet because we think it still need some polishing. However, here's a guide how to use chromeheadless API via nodejs:

  1. If you have admin right, download nodejs from this link for your operating system: https://nodejs.org/en/download/ Then install it on your server.
  2. If you can't install node.js on your server, please download the following portable nodejs for your operating system: Then extract it to koolreport/packages/export
  3. After installing nodejs, go to folder koolreport/packages/export and run the following command line:
npm i puppeteer

Note: if node and npm commands are not found, either use the exact paths to their executable files (in the installed nodejs folder) or add the paths to your environment path.

  1. Apply pdf exporting for your report:
$report->export("MyReportPdf")
->pdf(array(
	"nodeBinary" => 'C:\Program Files\nodejs\node.exe',
	"format"=>"A4",
	"landscape"=>true,
))
->toBrowser("MyReport.pdf");

For many other pdf options when exporting please use this link: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions

Let us know if there's any issue when following this guide. Thanks!

EGROW SOLUTIONS, LLC commented on Dec 14, 2018

Followed the steps, installed node js and pupperteer, still getting the error tested to make sure node and npm are correctly installed.

Failed to generate PDF NULL Fatal error: Uncaught Error: Call to a member function toBrowser() on null

David Winterburn commented on Dec 14, 2018

Hi,

Please open the file packages/export/vendor/its404/php-puppeteer/src/Browser.php, search for this command:

exec($fullCommand, $output, $returnVal);

Then add this right before that command:

echo $fullCommand; echo '<br>';

Finally try exporting again to see the echoed export command. Open a command line/terminal on your server (or your localhost) and copy and paste the echoed command in the terminal. If there's any error message in the terminal let us know. Thanks!

EGROW SOLUTIONS, LLC commented on Dec 14, 2018
"C:\www\lib\koolreport\packages\export/C:\Program Files odejs ode.exe" "C:\www\lib\koolreport\packages\export\vendor\its404\php-puppeteer\src/js/puppeteer-api.js" "{\"goto\":{\"waitUntil\":[\"load\",\"domcontentloaded\",\"networkidle0\",\"networkidle2\"]},\"viewport\":{\"width\":1024,\"height\":800},\"pdf\":{\"format\":\"Letter\",\"printBackground\":true,\"nodeBinary\":\"C:\\\\Program Files\\nodejs\\node.exe\",\"orientation\":\"portrait\",\"margin\":{\"top\":\"0.5in\",\"bottom\":\"0.5in\",\"left\":\"0.5in\",\"right\":\"0.5in\"},\"path\":\"C:\\\\Windows\\\\TEMP\\/5c13f53e1add02.pdf\"},\"url\":\"file:\\/\\/C:\\\\Windows\\\\TEMP\\/5c13f53e170e01.html\",\"nodeBinary\":\"C:\\\\eGrow\\\\GitHub\\\\eGrowConnect\\\\www\\\\lib\\\\koolreport\\\\packages\\\\export\\/C:\\\\Program Files\\nodejs\\node.exe\",\"chromeBinary\":null}"
David Winterburn commented on Dec 17, 2018

Hi,

It seems you set the path to node binary like this: "C:\Program Files\nodejs\node.exe" with doulbe quotes. The problem is that when using double quotes in PHP the back slash "\" acts as an escape character to some character after it (like \n). Thus the node binary path is no longer correct.

To fix this, please either 1. use the path with single quotes like this: 'C:\Program Files\nodejs\node.exe' or 2. use double back slash: "C:\Program Files\nodejs\node.exe"

After you get get the command, please paste it into your server's (or localhost's) terminal to run it. Thanks!

EGROW SOLUTIONS, LLC commented on Dec 17, 2018

ok. understood, updated to use single quotes, still getting error - pdf null

here the echo of full command


"C:\www\lib\koolreport\packages\export/C:\Program Files\nodejs\node.exe" "C:\www\lib\koolreport\packages\export\vendor\its404\php-puppeteer\src/js/puppeteer-api.js" "{\"goto\":{\"waitUntil\":[\"load\",\"domcontentloaded\",\"networkidle0\",\"networkidle2\"]},\"viewport\":{\"width\":1024,\"height\":800},\"pdf\":{\"format\":\"Letter\",\"printBackground\":true,\"nodeBinary\":\"C:\\\\Program Files\\\\nodejs\\\\node.exe\",\"Orientation\":\"portrait\",\"margin\":{\"top\":\"0.5in\",\"bottom\":\"0.5in\",\"left\":\"0.5in\",\"right\":\"0.5in\"},\"path\":\"C:\\\\Windows\\\\TEMP\\/5c17ffaf167f62.pdf\"},\"url\":\"file:\\/\\/C:\\\\Windows\\\\TEMP\\/5c17ffaf12b061.html\",\"nodeBinary\":\"C:\\\\www\\\\lib\\\\koolreport\\\\packages\\\\export\\/C:\\\\Program Files\\\\nodejs\\\\node.exe\",\"chromeBinary\":null}"
Failed to generate PDFNULL 
Fatal error: Uncaught Error: Call to a member function toBrowser() on null
David Winterburn commented on Dec 18, 2018

Hi,

There seems to be a bug here. Please open the file export/Handler.php, find the following line:

if (! file_exists($nodeBinary) || ! file_exists($nodeBinary . ".exe"))

and replace it with:

if (! file_exists($nodeBinary) && ! file_exists($nodeBinary . ".exe"))

Then run the export again and let us know the result. Thanks!

EGROW SOLUTIONS, LLC commented on Dec 18, 2018

That fixed it, was able to generate the pdf. - Performance is way better with node.js and headless chrome than phanthom.js.

There is another problem - code generates the temp html file in C:\Windows\Temp - so none of the resources like .js and .css files are loaded.

I was able to modify the code to save the temp html file in the web root and then it worked.

Also, suggestion, will all methods, when displaying in browser inline - to automatically delete the temp html files and pdf after the pdf is generated and rendered in browser.

Thanks for all your help!

David Winterburn commented on Dec 19, 2018

That's great to hear it worked for you! Even though the temporary html file is saved in temp folder, its resource files are linked to your web server so there should be no problem. Just try to open the html file in Chrome to see if it's fine.

Regarding the temp html and pdf files, we will explore options to enable cleaning them after exporting. Thanks!

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

None