Skip to content

Missing outline when merging PDF files #308

Description

@condorheroblog

I use the following code to merge two PDFs, both of which have outlines. After merging, the newly generated PDF loses its outline. What should I do to ensure that the outline is preserved while merging PDFs.

const fs = require("node:fs");
const path = require("node:path");
const pdf = require("pdfjs");

const doc = new pdf.Document();
const pdf1 = fs.readFileSync(path.resolve(__dirname, "1.pdf"));
const pdf2 = fs.readFileSync(path.resolve(__dirname, "2.pdf"));

const ext1 = new pdf.ExternalDocument(pdf1);
const ext2 = new pdf.ExternalDocument(pdf2);

doc.addPagesOf(ext1);
doc.addPagesOf(ext2);

doc.asBuffer((err, data) => {
	if (err)
		console.error(err);

	else
		fs.writeFileSync("merge.pdf", data, { encoding: "binary" });
});

At present, I only know that the root outline can be obtained through the following code:

const fs = require("node:fs");
const Parser = require("./parser.js");

const parser = new Parser(fs.readFileSync("vitePress.pdf"));
parser.parse();
const getOutline = catalog.get("Outlines").object.properties;

Looking forward to your reply😉.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions