unable to generate PDF from spatie bowsershot

2.4k views Asked by At

i have installed browsershot in my laravel project I have installed puppeteer, installed chromium , my npm version is updated, but still i am unable to get the PDF of my simple html input , i have tried to take screenshot by URL method and save as image(It works), but my requirement is to generate PDF of my html.

This is my code in controller

$output=Browsershot::html($html)
->setNodeModulePath("/var/www/html/ProjectName/node_modules/")
->setChromePath("/usr/bin/chromium-browser") 
    ->setNodeBinary('/usr/local/bin/node')
    ->setNpmBinary('/usr/local/bin/npm')
->showBackground()
->noSandbox()->timeout(60)
->save(public_path("DevTest.pdf");          

Error Message:

"message": "The process "PATH=$PATH:/usr/local/bin NODE_PATH='/var/www/html/ProjectName/node_modules/' /usr/local/bin/node '/var/www/html/benesprint/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/1933874416-0068512001600765779\/index.html","action":"pdf","options":{"path":"\/var\/www\/html\/ProjectName\/public\/DevTest.pdf","args":["--no-sandbox"],"viewport":{"width":800,"height":600},"displayHeaderFooter":false,"executablePath":"\/usr\/bin\/chromium-browser","timeout":60000,"printBackground":true}}'" exceeded the timeout of 60 seconds.",

**i would be grateful if you can help me out.**
1

There are 1 answers

0
coda On

i have similiar problem when browsershot inside container. and my step to resolve is like this:

  1. Manual generate PDF from html using laravel tinker. if there is an error, try reinstall chromium and puppeteer library.
  2. If success, then try remove all css and javascript links from html, and check for the result.
  3. If success, then add --proxy-server="direct://" and --proxy-bypass-list=* into browsershot args
  4. If step 3 is not working, then you can put css and javascript directly into view template or using php code to inclue those files like
   <?php include public_path('css/styles.css') ?>