Search code examples
angularprintingpdfmake

I have a list of invoices - i need to display a print dialog with all invoices then the user can send them all to a printer


~html
click printSelectedInvoices();

~typescript 

import * as pdfMake from 'pdfmake/build/pdfmake';
import * as pdfFonts from 'pdfmake/build/vfs_fonts';
printSelectedInvoices(){
     invoiceArray.forEach((invoice) => {
         var invoicePdf = {
             // DESIGN 
         };
         pdfMake.createPdf(invoicePdf).open({});
     })
}

"Currently, this is open the invoice count tabs and can print individual. this should be one print dialog and print all invoices. Does anyone have any idea how to do this? please help me."


Solution

  • Install npm module: PDF-lib

    npm i pdf-lib

    https://printjs.crabbly.com/

    import JsBarcode from 'jsbarcode/bin/JsBarcode'
    import * as printJS from 'print-js';
    import { PDFDocument } from 'pdf-lib'
    
    invoiceJsArray.forEach(async (pdfs) => {
        pdfMake.createPdf(pdfs).getBuffer(async function (buffer) {
            const pdf = await PDFDocument.load(buffer);
            const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
            copiedPages.forEach((page) => {
                mergedPdf.addPage(page);
            });
            const mergedPdfFile = await mergedPdf.save();
            const blob = new Blob([mergedPdfFile], { type: 'application/pdf' });
            const url = window.URL.createObjectURL(blob);
            // window.open(url);
            printJS({
                printable: url,
                type: 'pdf',
            })
        })
    });