UNPKG

printmaker

Version:

Generate PDF documents and from JavaScript objects

27 lines 1.37 kB
import { PDFName, PDFString } from 'pdf-lib'; export function createLinkAnnotation(page, box, uri) { const annots = getOrCreate(page.node, 'Annots', () => []); const annot = page.doc.context.obj(Object.assign(Object.assign({ Type: 'Annot', Subtype: 'Link', Rect: [box.x, box.y, box.x + box.width, box.y + box.height] }, (uri.startsWith('#') ? { A: { Type: 'Action', S: 'GoTo', D: PDFString.of(uri.slice(1)) } } : { A: { Type: 'Action', S: 'URI', URI: PDFString.of(uri) } })), { C: page.doc.context.obj([]), F: 0 })); annots.push(page.doc.context.register(annot)); } export function createNamedDest(page, name, pos) { const names = getOrCreate(page.doc.catalog, 'Names', () => ({})); const dests = getOrCreate(names, 'Dests', () => ({})); const destNames = getOrCreate(dests, 'Names', () => []); for (let i = 0; i < destNames.size(); i += 2) { if (destNames.get(i).asString() === name) { throw new Error(`Duplicate id ${name}`); } } destNames.push(PDFString.of(name)); destNames.push(page.doc.context.obj([page.ref, 'XYZ', pos.x, pos.y, null])); } function getOrCreate(dict, name, creatorFn) { if (!dict.has(PDFName.of(name))) { dict.set(PDFName.of(name), dict.context.obj(creatorFn())); } return dict.get(PDFName.of(name)); } //# sourceMappingURL=pdf-annotations.js.map