swipe
swipe11mo ago

I've been playing around with the HTML

I've been playing around with the HTML to PDF node, which is nifty. Is there any way to pipe output the PDF directly with pdf headers?
6 Replies
Gaurav Chadha
Gaurav Chadha11mo ago
A possible solution could be Merging PDF one with headers and and one without. We can explore this solution - https://github.com/PejmanNik/puppeteer-report. cc @Deepanshu Found this similar question here - https://stackoverflow.com/questions/51458286/how-does-header-and-footer-printing-work-in-puppeters-page-pdf-api/51460641#51460641.
Stack Overflow
How does header and footer printing work in Puppeter's page.pdf API?
I've noticed a few inconsistencies when trying to use the headerTemplate and footerTemplate options with page.pdf: The DPI for headers and footers seems to be lower (72 vs 96 for the main body, I ...
swipe
swipeOP11mo ago
I haven’t had much luck on this yet, definitely keen on finding out if it’s possible!
Deepanshu
Deepanshu11mo ago
@swipe I haven't tested the solutions suggested by @Gaurav Chadha yet, let me test those and get back to you!
swipe
swipeOP11mo ago
This is the code I'm working on, but it's throwing an error in test mode: import fetch from 'node-fetch'; export default async function postRequestPuppeteerStore(req, res, { htmlContent, options }) { try { const response = await fetch('https://puppeteer.buildship.run/v1/pdf', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ htmlContent, options }) }); const buffer = await response.buffer(); // Set headers to indicate the response type is a PDF res.setHeader('Content-Type', 'application/pdf'); res.setHeader('Content-Disposition', 'attachment; filename="output.pdf"'); // Send the buffer as the response res.send(buffer); } catch (error) { // Handle errors here (e.g., send a 500 server error response) res.status(500).send('Error generating PDF'); } } The error I am getting is: { "error": "Cannot destructure property 'htmlContent' of 'undefined' as it is undefined." }
Deepanshu
Deepanshu11mo ago
have you added the htmlContent input in your node?
swipe
swipeOP10mo ago
Yes, it's defined and passed in the test params curiously, if I remove "res, req," from the params, and I comment out the lines that use it, the error goes away. I wonder if there's an issue with modifying headers using this method?