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

Merging pdf-documents doesn't handle merging or copying Bookmarks/outlines to the merged document #1017

Open
4 tasks done
ObaidaAlhaasan opened this issue Oct 4, 2021 · 1 comment

Comments

@ObaidaAlhaasan
Copy link

What were you trying to do?

I'm trying to merge multiple pdf-documents to a single pdf document and display it in the browser in pdf-viewer and send it to the S3 to store the merge result

I'm using pdf-lib to do the operations and react-pdf to display the documents in the broweser

Why were you trying to do this?

I'm building a web application that has a feature of pdf-viewer and that include merging pdf's appending images - scaling-rotating-etc

How did you attempt to do it?

Here attached the code that I use to merge documents

  public static async MergeDocuments(documentsContent: Array<string>): Promise<string> {
    const mergedPdf = await PDFDocument.create();

    for (let i = 0; i < documentsContent.length; i++){
      const base64 = documentsContent[i];

      const pdf = await PDFDocument.load(base64 , { ignoreEncryption: true });

      const copiedPages = await mergedPdf.copyPages(pdf, pdf.getPageIndices());

      copiedPages.forEach((page: PDFPage) => {
        mergedPdf.addPage(page);
      });
    }

    return await mergedPdf.saveAsBase64()
  }

That didn't include bookmarks with the final document, so I tried to implement it following an issue/discussion on pdf-lib repo issue127

What actually happened?

I wasn't able to get the outlines programmatically from any method or API in pdf-lib by checking the pdf that I loaded, so tried to read the code and loop through pdf.context.enumerateIndirectObjects() and with that, I got some information but I couldn't continue because my main issue now is, how I can link bookmark/outline with its page or get bookmark associated page index? Including nested bookmarks

    // both base64 have outlines 
    const mergedPdf = await MergeDocuments([base64....1, base64....2]);

    console.log("getPages", mergedPdf.getPages());
    console.log("getPageIndices", mergedPdf.getPageIndices());
    console.log("getForm", mergedPdf.getForm());
    console.log("getKeywords", mergedPdf.getKeywords());
    console.log("getTitle", mergedPdf.getTitle());
    console.log("getProducer", mergedPdf.getSubject());

// Or looping trough enumerateIndirectObjects

    for (const [key, value] of mergedPdf.context.enumerateIndirectObjects().map(x => x[1].dict).filter(x => x).entries()) {
      console.log(key, value);

      if (typeof value === "object") {
        if (value.entries) {
          for (const [k, v] of value.entries()) {
            if (v instanceof PDFString && k?.encodedName?.includes("Title")) {
              outlines.push(v.value);
            }
          }
        }
      }
    }

What did you expect to happen?

When merging multiple pdf-documents the bookmarks/outlines got merged with them, or to have a proper way to do that accurately

How can we reproduce the issue?

I have attached two pdf-files that have bookmarks, try to merge them then open the merged document with adobe-acrobat "or another reader" and check bookmarks

image
image

Result in, no outlines or any bookmarks:
merge-result

Version

1.16.0

What environment are you running pdf-lib in?

Browser

Required Reading

Additional Notes

No response

@ObaidaAlhaasan
Copy link
Author

ObaidaAlhaasan commented Oct 8, 2021

Thank you for the awesome library, I think merging and including outlines/bookmarks will be added sometimes in the future based on the road map links if you could please consider giving ways to merge outlines with merging documents if that isn't in the roadmap plan implementation with high-level API

998
567
450

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

1 participant