feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
@@ -0,0 +1,9 @@
import type { PDFDocument, PDFNumber, PDFRef, PDFName as _PDFName, PDFDict as _PDFDict } from "pdf-lib";
export declare function createOutlineDictFactory(_: {
PDFName: typeof _PDFName;
PDFDict: typeof _PDFDict;
}): (doc: PDFDocument, _: {
First: PDFRef;
Last: PDFRef;
Count: PDFNumber;
}) => _PDFDict;
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOutlineDictFactory = void 0;
function createOutlineDictFactory(_) {
const { PDFDict, PDFName } = _;
return function createOutlineDict(doc, _) {
const outlinesDictMap = new Map();
outlinesDictMap.set(PDFName.Type, PDFName.of("Outlines"));
outlinesDictMap.set(PDFName.of("First"), _.First);
outlinesDictMap.set(PDFName.of("Last"), _.Last);
outlinesDictMap.set(PDFName.of("Count"), _.Count);
return PDFDict.fromMapWithContext(outlinesDictMap, doc.context);
};
}
exports.createOutlineDictFactory = createOutlineDictFactory;
@@ -0,0 +1,14 @@
import type { PDFDocument, PDFNumber, PDFRef, PDFHexString, PDFArray, PDFName as _PDFName, PDFDict as _PDFDict } from "pdf-lib";
export declare function createOutlineNodeFactory(_: {
PDFName: typeof _PDFName;
PDFDict: typeof _PDFDict;
}): (doc: PDFDocument, _: {
Title: PDFHexString;
Parent: PDFRef;
Prev?: PDFRef;
Next?: PDFRef;
First?: PDFRef;
Last?: PDFRef;
Count?: PDFNumber;
Dest: PDFArray;
}) => _PDFDict;
@@ -0,0 +1,24 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOutlineNodeFactory = void 0;
function createOutlineNodeFactory(_) {
const { PDFDict, PDFName } = _;
return function createOutlineNode(doc, _) {
const map = new Map();
map.set(PDFName.Title, _.Title);
map.set(PDFName.Parent, _.Parent);
if (_.Prev !== undefined)
map.set(PDFName.of("Prev"), _.Prev);
if (_.Next !== undefined)
map.set(PDFName.of("Next"), _.Next);
if (_.First !== undefined)
map.set(PDFName.of("First"), _.First);
if (_.Last !== undefined)
map.set(PDFName.of("Last"), _.Last);
if (_.Count !== undefined)
map.set(PDFName.of("Count"), _.Count);
map.set(PDFName.of("Dest"), _.Dest);
return PDFDict.fromMapWithContext(map, doc.context);
};
}
exports.createOutlineNodeFactory = createOutlineNodeFactory;
@@ -0,0 +1,4 @@
import type { PDFDocument, PDFPageLeaf as _PDFPageLeaf, PDFRef } from "pdf-lib";
export declare function getPageRefsFactory(_: {
PDFPageLeaf: typeof _PDFPageLeaf;
}): (pdfDoc: PDFDocument) => PDFRef[];
@@ -0,0 +1,15 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPageRefsFactory = void 0;
function getPageRefsFactory(_) {
const { PDFPageLeaf } = _;
return function getPageRefs(pdfDoc) {
const refs = [];
pdfDoc.catalog.Pages().traverse((kid, ref) => {
if (kid instanceof PDFPageLeaf)
refs.push(ref);
});
return refs;
};
}
exports.getPageRefsFactory = getPageRefsFactory;
@@ -0,0 +1,2 @@
import type { IOutlinePdfFactory } from "../publicApi";
export declare const outlinePdfFactory: IOutlinePdfFactory;
@@ -0,0 +1,71 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.outlinePdfFactory = void 0;
const createOutlineDictFactory_1 = require("./createOutlineDictFactory");
const createOutlineNodeFactory_1 = require("./createOutlineNodeFactory");
const getPageRefsFactory_1 = require("./getPageRefsFactory");
const outline_pdf_data_structure_1 = require("@lillallol/outline-pdf-data-structure");
const outlinePdfFactory = function outlinePdfFactory(pdfLib) {
const { PDFArray, PDFDocument, PDFName, PDFNull, PDFNumber, PDFHexString } = pdfLib;
const getPageRefs = getPageRefsFactory_1.getPageRefsFactory(pdfLib);
const createOutlineNode = createOutlineNodeFactory_1.createOutlineNodeFactory(pdfLib);
const createOutlineDict = createOutlineDictFactory_1.createOutlineDictFactory(pdfLib);
return async function outlineToPdf(parameters) {
const { outline: inputOutline, pdf: inputPdf } = parameters;
const doc = await (() => {
if (inputPdf instanceof PDFDocument)
return inputPdf;
return PDFDocument.load(inputPdf);
})();
const pageRefs = getPageRefs(doc);
const pageRefsLength = pageRefs.length;
const outlineRootRef = doc.context.nextRef();
//Pointing the "Outlines" property of the PDF "Catalog" to the first object of your outlines
doc.catalog.set(PDFName.of("Outlines"), outlineRootRef);
const outlineItemRef = [];
const outlineItem = [];
// const outlineRootRef = outlinesDictRef;
const { outlineItems: pseudoOutlineItems, outlineRootCount } = outline_pdf_data_structure_1.outlinePdfDataStructure(inputOutline, pageRefsLength);
for (let i = 0; i < pseudoOutlineItems.length; i++) {
outlineItemRef.push(doc.context.nextRef());
}
for (let i = 0; i < pseudoOutlineItems.length; i++) {
const { Title, Dest, Parent, Count, First, Last, Next, Prev } = pseudoOutlineItems[i];
outlineItem[i] = createOutlineNode(doc, {
Title: PDFHexString.fromText(Title),
Parent: Parent !== -1 ? outlineItemRef[Parent] : outlineRootRef,
...(Prev !== undefined && {
Prev: outlineItemRef[Prev],
}),
...(Next !== undefined && {
Next: outlineItemRef[Next],
}),
...(First !== undefined && { First: outlineItemRef[First] }),
...(Last !== undefined && { Last: outlineItemRef[Last] }),
...(Count !== undefined && { Count: PDFNumber.of(Count) }),
Dest: (() => {
const array = PDFArray.withContext(doc.context);
array.push(pageRefs[Dest]);
array.push(PDFName.of("XYZ"));
array.push(PDFNull);
array.push(PDFNull);
array.push(PDFNull);
return array;
})(),
});
}
const outlinesDict = createOutlineDict(doc, {
First: outlineItemRef[0],
Last: outlineItemRef[pseudoOutlineItems.length - 1],
Count: PDFNumber.of(outlineRootCount),
});
//First 'Outline' object. Refer to table H.3 in Annex H.6 of PDF Specification doc.
doc.context.assign(outlineRootRef, outlinesDict);
//Actual outline items that will be displayed
for (let i = 0; i < pseudoOutlineItems.length; i++) {
doc.context.assign(outlineItemRef[i], outlineItem[i]);
}
return doc;
};
};
exports.outlinePdfFactory = outlinePdfFactory;