Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge PDF #1650

Open
2 tasks done
Fred-Cleverli opened this issue Jul 4, 2024 · 1 comment
Open
2 tasks done

Merge PDF #1650

Fred-Cleverli opened this issue Jul 4, 2024 · 1 comment

Comments

@Fred-Cleverli
Copy link

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

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

@Nicholas-EG
Copy link

Not sure I understand what you're trying to do with the Microsoft stuff. If you need a method that merges PDFDocument objects, I wrote this one up:

/***

  • @param frontPDF - a PDFDocument object
  • @param rearPDF - a PDFDocument object
    */
    async function mergeDocuments(frontPDF, rearPDF) {
    const copiedPages = await frontPDF.copyPages(rearPDF, [...Array(rearPDF.getPageCount()).keys()]);
    copiedPages.forEach((page) => {
    frontPDF.addPage(page);
    })
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants