Reputation: 91
I'm new to nodejs, express and puppeteer as well. The task is to create webserver that convert html to pdf. After research I found puppeteer and phantomsjs for that purpose (but phantomjs is not supportable anymore). I created simple webserver that takes json with html and other settings. But phantomjs is more faster than puppeteer, maybe I have some mistakes?
Puppeteer code:
Route:
router.post("/raw", jsonParser, async function(request, response) {
html2PdfConverter(request.body.html, pdf => {
response.setHeader('Content-Type', 'application/pdf');
response.send(pdf);
},
request.body.options,
request.body.puppeteerArgs,
request.body.remoteContent).catch(err => {
console.log(err);
response.status(500).send('An error occurred');
});
});
Html2PdfConverter
const puppeteer = require('puppeteer');
let convertHTMLToPDF = async (html, callback, options = null, puppeteerArgs=null, remoteContent=true) => {
if (typeof html !== 'string') {
throw new Error(
'Invalid Argument: HTML expected as type of string and received a value of a different type. Check your request body and request headers.'
);
}
let browser;
if (puppeteerArgs) {
browser = await puppeteer.launch(puppeteerArgs);
} else {
browser = await puppeteer.launch();
}
const page = await browser.newPage();
if (!options) {
options = { format: 'Letter' };
}
if (remoteContent === true) {
await page.goto(`data:text/html,${html}`, {
waitUntil: 'networkidle0'
});
} else {
await page.setContent(html);
}
await page.pdf(options).then(callback, function(error) {
console.log(error);
});
await browser.close();
};
module.exports = convertHTMLToPDF;
I think that phantomjs works faster because pdf method returns stream and html and pdf files creates in Temp folder.
I would be grateful for any help.
Upvotes: 1
Views: 2530
Reputation: 18816
Here are two main reasons not to use phantomjs at this moment,
phantomjs
is deprecated, no support when you need itIf you want puppeteer to be faster, surely you can disable many latest features provided by puppeteer, but the pdf might not look clean as it would if all features were enabled.
Upvotes: 1