You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi.
With clean puppeteer we can able to use something like this:
constpuppeteer=require('puppeteer');const{ writeFile }=require('fs');const{ PDFDocument }=require('pdf-lib');(async()=>{constbrowser=awaitpuppeteer.launch({headless: 'new',ignoreHTTPSErrors: true,args: ['--lang=en-US'],});constbrowserPage=awaitbrowser.newPage();awaitbrowserPage.goto('http://localhost:5555/templates/payment-invoice/',{waitUntil: ['load','networkidle2'],});// header for the 1st page (not needed)constfirstPageHeader=`<div class="header-first"></div>`;// header for the all next pagesconstotherPagesHeader=`<div class="header-other">some header content</div>`;constprintedPdf=awaitbrowserPage.pdf({printBackground: true,format: 'A4',displayHeaderFooter: true,headerTemplate: firstPageHeader,footerTemplate: ``,margin: {bottom: '0px'},scale: 1,pageRanges: '1',// apply firstPageHeader only for first page});constprintedOtherPagesPdf=awaitbrowserPage.pdf({printBackground: true,format: 'A4',displayHeaderFooter: true,headerTemplate: otherPagesHeader,footerTemplate: ``,margin: {bottom: '0px'},scale: 1,pageRanges: '2-',// apply otherPagesHeader for other pages});awaitbrowser.close();// Safe both PDF in the temp fileswriteFile('./output/first_page.pdf',printedPdf,{},(err)=>{if(err){returnconsole.error('Error saving first page PDF');}console.log('First page PDF saved successfully!');});writeFile('./output/other_pages.pdf',printedOtherPagesPdf,{},(err)=>{if(err){returnconsole.error('Error saving other pages PDF');}console.log('Other pages PDF saved successfully!');});// Combine them into resulted oneconstfirstPagePdfBytes=awaitreadFile('./output/first_page.pdf');constotherPagesPdfBytes=awaitreadFile('./output/other_pages.pdf');constpdfDoc=awaitPDFDocument.create();const[firstPage, ...otherPages]=awaitpdfDoc.copyPages(firstPagePdfBytes,otherPagesPdfBytes);pdfDoc.addPage(firstPage);for(constpageofotherPages){pdfDoc.addPage(page);}constmergedPdfBytes=awaitpdfDoc.save();// Save combined PDFwriteFile('./output/merged.pdf',mergedPdfBytes,{},(err)=>{if(err){returnconsole.error('Error saving merged PDF');}console.log('Merged PDF saved successfully!');});})();
How to do the same with your library?
Since you have use id for header and footer, I don't have any idea how to do that conditionally
The text was updated successfully, but these errors were encountered:
Hi, you should be able to do the same within this lib, only you need to inject the header (manipulate the dom) before making the pdf file through the browser to the current location in the file (under the correct ID)
await browserPage.evaluate(() => {
let dom = document.querySelector('#header');
dom.innerHTML = "change to something"
});
Hi.
With clean puppeteer we can able to use something like this:
How to do the same with your library?
Since you have use
id
for header and footer, I don't have any idea how to do that conditionallyThe text was updated successfully, but these errors were encountered: