Skip to content

PowerPoint PDF data loaded into PDF-Lib does not open in Adobe Acrobat Pro DC #1206

Open
@ahaganDEV

Description

@ahaganDEV

What were you trying to do?

Given some PDF file data retrieved from our API, pass them through into PDF-Lib to manipulate them (draw stamps etc) and merge them into one PDF document to output to disk. Then the PDF should be opened in Adobe Acrobat Pro DC.

How did you attempt to do it?

  1. Initially we receive PDF data from an API that returns it in UInt8Array format.

  2. Load the data into PDF-Lib:
    const embedDoc = await PDFDocument.load(pdfFileData);

  3. Embed the pages into the document:

const pages = embedDoc.getPages();
  for (const page of pages) {
      // Exclude blank pages
      if (!page.node.Contents()) {
        continue;
      }

      const newPage = embedDoc.addPage([page.getWidth(), page.getHeight()]);

      const embedPage = await embedDoc.embedPage(page);

      newPage.drawPage(embedPage, {
        x: 0,
        y: 0,
        xScale: 1,
        yScale: 1,
        width: page.getWidth(),
        height: page.getHeight(),
      });
  1. Later on in the process, merge multiple PDF Documents together:
for (const pdfFile of this.pdfFiles) {
        const copiedPages = await mergedPdf.copyPages(
          pdfFile,
          pdfFile.getPageIndices()
        );
        copiedPages.forEach((page, index) => {
           mergedPdf.addPage(page);
        });
    }
  1. Save and then output the merged file to disk:
const pdfData = await mergedPdf.save();
fs.writeFileSync('my-path\myfile.pdf', pdfData);

What actually happened?

The PDF generated can be opened in native PDF readers on Windows, MacOS and Ubuntu. However when trying to open it in Adobe Acrobat Pro DC, it fails to open, giving the following error:

image

When run through this PDF Checker tool https://www.pdf-online.com/osa/repair.aspx it outputs the followoing error:

0x80410306 - E - The "Length" key of the stream object is wrong.
    - Object No.: 10
    - File: Generated_Merged_File.pdf

When repaired, this PDF can then be opened in Adobe Acrobat Pro DC.

When Opened in RUPS here is the basic structure and the stream length of the above object:
image

Here is the RUPS view of the repaired PDF (notice the differing stream length highlighted)
image

What did you expect to happen?

The PDF file opens up correctly in Adobe Actobat Pro DC

How can we reproduce the issue?

Here is the original PowerPoint PDF file that is retrieved from our API (this PDF itself opens fine in Adobe Acrobat)
simple_ppt.pdf

Here is the generated PDF after it is passed through PDF-Lib and has gone through the merge process (this does NOT open in Adobe Acrobat)
Generated_Merged_File.pdf

Here is the output of the repaired PDF using the tool https://www.pdf-online.com/osa/repair.aspx (this does open in Adobe Acrobat)
Generated_Merged_File.pdf_recovered.pdf

Example code snippets are shown above.

Version

1.16.0

What environment are you running pdf-lib in?

Node

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

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions