import fs from "fs" import path from "path" import puppeteer from "puppeteer" import {…
Private research annotation
Sign in to add a private research label or note.
- User
import fs from "fs" import path from "path" import puppeteer from "puppeteer" import { fileURLToPath } from "url" import os from "os" import { Request, Response } from "express"
const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename)
export const UploadController = { async UploadPDF(req: Request, res: Response) { try { const { htmlContent } = req.body
codeif (!htmlContent) { return res.status(400).send("HTML content is missing") } const browser = await puppeteer.launch({ headless: true, args: ["--no-sandbox", "--disable-setuid-sandbox"], executablePath: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", }) const page = await browser.newPage() await page.setContent(htmlContent, { waitUntil: "networkidle0" }) console.log("Content set to page") await page.setViewport({ width: 1200, height: 1600 }) const pdfBuffer = await page.pdf({ format: "A4", printBackground: true, margin: { top: "0.5cm", right: "0.5cm", bottom: "1cm", left: "0.2cm" }, scale: 0.8, preferCSSPageSize: true, }) await browser.close() const pdfPath = path.join(os.tmpdir(), "output.pdf") await fs.promises.writeFile(pdfPath, pdfBuffer) res.download(pdfPath, "schedule.pdf", async (err) => { if (err) { console.error("Error sending file:", err) return res.status(500).send("Error sending file") } try { await fs.promises.unlink(pdfPath) } catch (unlinkError) { console.error("Error deleting file:", unlinkError) } }) } catch (error) { console.error("Error generating PDF:", error) res.status(500).send("Error generating PDF") }}, }
export default UploadController Error generating PDF: Error: Browser was not found at the configured executablePath (/Applications/Google Chrome.app/Contents/MacOS/Google Chrome) 2024-09-03 03:40:06 at ChromeLauncher.launch (file:///var/www/server/node_modules/puppeteer/node_modules/puppeteer-core/lib/esm/puppeteer/node/BrowserLauncher.js:50:19) 2024-09-03 03:40:06 at async UploadPDF (file:///var/www/server/src/controllers/upload.controller.js:15:29)
- Assistant
The error message indicates that Puppeteer is unable to find the Chrome executable at the specified path (
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome). This could be due to several reasons, like the path being incorrect, Chrome not being installed, or the application running on an environment where Chrome is located at a different path. - User
FROM node:21.7.2
WORKDIR /var/www/server
RUN apt-get update
&& apt-get install -y wget gnupg
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | gpg --dearmor -o /usr/share/keyrings/googlechrome-linux-keyring.gpg && sh -c 'echo "deb [arch=amd64 signed-by=/usr/share/keyrings/googlechrome-linux-keyring.gpg] stable main" >> /etc/apt/sources.list.d/google.list' && apt-get update && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-khmeros fonts-kacst fonts-freefont-ttf libxss1 dbus dbus-x11 --no-install-recommends && rm -rf /var/lib/apt/lists/* && groupadd -r pptruser && useradd -rm -g pptruser -G audio,video pptruser - Assistant
- User
E: Unable to locate package google-chrome-stable
- Assistant
If you're still receiving
E: Unable to locate package google-chrome-stable, there are several potential reasons and solutions you can explore: - User
How to Make Puppeteer Work in Docker: Both in Local and Production Environments
- Assistant