Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.6k stars 635 forks source link

Merge PDF #1650

Open Fred-Cleverli opened 3 weeks ago

Fred-Cleverli commented 3 weeks ago

What were you trying to do?

Hallo.

I want to merge serveral PDF to one pdf.

How did you attempt to do it?

async function MergePDF(pdfstring) { const { PDFDocument } = PDFLib

const pdf = JSON.parse(pdfstring)
var pdfsToMerge = [];

Microsoft.Dynamics.NAV.InvokeExtensibilityMethod("MergePDF_Begin", '');

for (var j = 0; j < pdf.length; j++) {
    var pdf64 = pdf[j].pdf;
    var raw1 = window.atob(pdf64);
    var len1 = raw1.length;
    var pdfBuffer1 = new Uint8Array(new ArrayBuffer(len1));
    for (var i = 0; i < len1; i++) {
      pdfBuffer1[i] = raw1.charCodeAt(i);
    }

    pdfsToMerge[j] = pdfBuffer1;
}

const mergedPdf = await PDFDocument.create();
for (const pdfBytes of pdfsToMerge) {
    const pdf = await PDFDocument.load(pdfBytes);
    const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());
    copiedPages.forEach((page) => {
      mergedPdf.addPage(page);
    }
  );
}

// ---

const buf = await mergedPdf.saveAsBase64();
var pdftoNAV = [buf]

Microsoft.Dynamics.NAV.InvokeExtensibilityMethod("DownloadPDF", pdftoNAV);

}

// The Lines Microsoft.Dynamics.NAV.* are Eevents to Outside Javascript. The Javascript is called by Microsoft BC 365.

What actually happened?

I test with PDF Dokuments I have on may disk. Some PDF not Merged. This PDF -"Part" are Missing in merged PDF

What did you expect to happen?

I want that all PDF Dokuments to merge are Merged in one PDF.

How can we reproduce the issue?

-

Version

the last of your homepage

What environment are you running pdf-lib in?

Browser

Checklist

Additional Notes

-