Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6176

NodeJS: Generating PDF with Puppeteer and Docker

$
0
0

I am trying to generate pdf with puppeteer inside a docker environment but it doesn't work.

My code looks like this:

Dockerfile

FROM node:slimENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD trueRUN apt-get update && apt-get install gnupg wget -y && \  wget --quiet --output-document=- https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor > /etc/apt/trusted.gpg.d/google-archive.gpg && \  sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'&& \  apt-get update && \  apt-get install google-chrome-stable -y --no-install-recommends && \  rm -rf /var/lib/apt/lists/*WORKDIR /appCOPY package.json package-lock.json ./RUN npm installRUN npx puppeteer browsers install chromeCOPY . .EXPOSE 3000CMD npm start

docker-compose.yml

version: "3.4"services:  puppeteer:    platform: linux/amd64    image: ghcr.io/puppeteer/puppeteer:latest    volumes:      - .:/app    working_dir: /app  report:    platform: linux/amd64    container_name: report    build:      context: .      dockerfile: Dockerfile    restart: "no"    ports:      - 3000:3000    volumes:      - .:/app      - /app/node_modules/

index.js

var express = require("express");var router = express.Router();const puppeteer = require("puppeteer");router.get("/pdf", async function (req, res, next) {  let html = fs.readFileSync("views/template/index.html", "utf8");  const headerHTML = fs.readFileSync("views/template/header.html", "utf8");  const data = require("../extract.json");  const header = Handlebars.compile(headerHTML)(dataExtractor(data));  html = Handlebars.compile(html)(dataExtractor(data));  const browser = await puppeteer.launch({    args: ["--no-sandbox", "--disable-setuid-sandbox"],  });  const page = await browser.newPage();  await page.setContent(html);  const result = await page.pdf({    headerTemplate: header,    displayHeaderFooter: true,    preferCSSPageSize: true,    margin: {      top: "47mm",      right: "0mm",      bottom: "4mm",      left: "0mm",    },    footerTemplate:'<div style="width: 100%;"><p style="color: #444; margin: 0 auto;">Page {{page}}</p></div>',    format: "A4",  });  res.setHeader("Content-Type", "application/pdf");  res.setHeader("Content-Type", "application/pdf");  res.setHeader("Content-Disposition", "attachment; filename=output.pdf");  res.send(result);});module.exports = router;

When I run the code above using docker-compose up. This is the error I get in my console:

report                  | /app/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:267report                  |                 reject(new Error([report                  |                        ^report                  | report                  | Error: Failed to launch the browser process!report                  | qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumpedreport                  | qemu: uncaught target signal 5 (Trace/breakpoint trap) - core dumpedreport                  | [60:83:0320/121207.974805:ERROR:file_path_watcher_inotify.cc(337)] inotify_init() failed: Function not implemented (38)report                  | [60:86:0320/121208.037150:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directoryreport                  | [0320/121208.131701:ERROR:scoped_ptrace_attach.cc(27)] ptrace: Function not implemented (38)report                  | Assertion failed: p_rcu_reader->depth != 0 (/qemu/include/qemu/rcu.h: rcu_read_unlock: 101)report                  | report                  | report                  | TROUBLESHOOTING: https://pptr.dev/troubleshootingreport                  | report                  |     at ChildProcess.onClose (/app/node_modules/puppeteer/node_modules/@puppeteer/browsers/lib/cjs/launch.js:267:24)report                  |     at ChildProcess.emit (node:events:531:35)report                  |     at ChildProcess._handle.onexit (node:internal/child_process:294:12)report                  | report                  | report                  | Node.js v21.7.1report                  | report                  | [nodemon] app crashed - waiting for file changes before starting...

Please how can I fix this error and make puppeteer work successfully with Docker Compose.


Viewing all articles
Browse latest Browse all 6176

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>