node.jspdfherokunode-html-pdf

html-pdf-node library not working with heroku


I am using this html-pdf-node library to convert html to pdf file in nodejs side. After converting it returns the pdf buffer, that i use to send to mail.

Here is the code

const file = { content: attachementPdfContentString };

return htmlToPdf.generatePdf(file, options).then(pdfBuffer => {
      try {
        return this.sendMail({
          to: hotelEmail,
          subject: "Dashback Invoice",
          body: `Hi `,
      attachments: [
        {
          filename: 'invoice.pdf',
          content: Buffer.from(pdfBuffer, 'utf-8')
        }
      ]
    });
  } catch (err) {
    console.error("Unable to send mail for hotel invitation", JSON.stringify(invoice));
    throw err;
  }

this works on local system, pdf is getting sent to mail using nodemailer. But when I run the same code on heroku dyno, it shows

2022-11-29T15:43:13.506732+00:00 app[web.1]: Error: Failed to launch the browser process!
2022-11-29T15:43:13.506734+00:00 app[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-901912/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
2022-11-29T15:43:13.506734+00:00 app[web.1]: 
2022-11-29T15:43:13.506734+00:00 app[web.1]: 
2022-11-29T15:43:13.506735+00:00 app[web.1]: TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md
2022-11-29T15:43:13.506735+00:00 app[web.1]: 
2022-11-29T15:43:13.506735+00:00 app[web.1]:     at onClose (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:197:20)
2022-11-29T15:43:13.506736+00:00 app[web.1]:     at Interface.<anonymous> (/app/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:187:68)
2022-11-29T15:43:13.506736+00:00 app[web.1]:     at Interface.emit (node:events:525:35)
2022-11-29T15:43:13.506737+00:00 app[web.1]:     at Interface.close (node:internal/readline/interface:536:10)
2022-11-29T15:43:13.506738+00:00 app[web.1]:     at Socket.onend (node:internal/readline/interface:262:10)
2022-11-29T15:43:13.506738+00:00 app[web.1]:     at Socket.emit (node:events:525:35)
2022-11-29T15:43:13.506738+00:00 app[web.1]:     at endReadableNT (node:internal/streams/readable:1359:12)
2022-11-29T15:43:13.506739+00:00 app[web.1]:     at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

How can i solve this, or any other library or way?


Solution

  • It looks like you're missing a library.

    libnss3.so is found in the libnss3 Ubuntu package. You can install it using the apt buildpack:

    1. Add the apt buildpack as your first buildpack, e.g. by running

      heroku buildpacks:add --index 1 heroku-community/apt
      
    2. Create a file called Aptfile in the root directory of your project that contains the names of the Ubuntu packages you wish to install, e.g.

      libnss3
      

      Note that additional packages may be required (see below).

    3. Commit and redeploy.

    Note that the apt buildpack does not do dependency resolution. If you add packages here that depend on other packages, you may need to manually list those dependencies.

    Chromium dependencies

    The exact dependencies required to run Chromium or Chrome may vary from release to release and from Ubuntu version to Ubuntu version, but a good starting point is this list of dependencies from the Puppeteer website:

    ca-certificates
    fonts-liberation
    libappindicator3-1
    libasound2
    libatk-bridge2.0-0
    libatk1.0-0
    libc6
    libcairo2
    libcups2
    libdbus-1-3
    libexpat1
    libfontconfig1
    libgbm1
    libgcc1
    libglib2.0-0
    libgtk-3-0
    libnspr4
    libnss3
    libpango-1.0-0
    libpangocairo-1.0-0
    libstdc++6
    libx11-6
    libx11-xcb1
    libxcb1
    libxcomposite1
    libxcursor1
    libxdamage1
    libxext6
    libxfixes3
    libxi6
    libxrandr2
    libxrender1
    libxss1
    libxtst6
    lsb-release
    wget
    xdg-utils
    

    This list is subject to change and not guaranteed to be complete. I suggest you visit the Puppeteer site for an updated list, and if you find that additional libraries are missing you can add them one at a time as above.