I'm trying to create a pdf from html template calling an api, so i'm using puppeteer.
The problem is, when i try to create a new page from puppeteer launch, it doesn't work and also it restarts the server and I don't know why. I'm working on localhost and the version of puppeteer is ^9.1.1
Here is the code when I call the api by clicking a href link
export default async (templateName, { totalPages, currentPage, ...data }) => {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
})
const html = await handlebars(
templateName,
{
...data,
},
)
const tmpId = uuidV4()
const tempFolder = path.join(__dirname, '../', tmpId)
const tempFilePath = path.join(tempFolder, 'index.html')
await mkdir(tempFolder)
await writeFile(tempFilePath, html)
await copyFolder(
path.join(__dirname, '../templates', 'quote', 'assets'),
path.join(tempFolder, 'assets'),
)
console.log("here")
const page = await browser.newPage()
console.log("after")
// the code to make the pdf...
return pdf
}
Here is the api server:
import http_errors from 'http-errors';
const { NotFound } = http_errors;
import 'dotenv/config.js'
import './config/init_redis.js'
import express, { json, urlencoded } from "express"
import htmltopdfRouter from "./html_to_pdf/routes/index.js"
const app = express();
import cookieParser from 'cookie-parser'
import cors from 'cors'
app.use(json())
app.use(cookieParser())
app.use(cors({ credentials: true, origin: "http://localhost:8081" }));
app.use(urlencoded({ extended: true }))
app.use("/api", htmltopdfRouter)
app.use(async (req, res, next) => {
next(NotFound())
})
app.use((err, req, res, next) => {
res.status(err.status || 500)
res.send({
success: err.status,
message: err.message,
})
})
const port = process.env.APP_PORT || 4000;
app.listen(port, () => {
console.log("Server up and running on PORT :", port);
});