Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function addOutline (pdfDoc, doc) {
const depth = doc.getAttribute('toclevels') || 2
const context = pdfDoc.context
const outlineRef = context.nextRef()
const outline = getOutline(doc, depth)
if (outline.length === 0) {
return pdfDoc
}
generateWarningsAboutMissingDestinations(outline, pdfDoc)
setRefsForOutlineItems(outline, context, outlineRef)
buildPdfObjectsForOutline(outline, context)
const outlineObject = PDFDict.fromMapWithContext(new Map([
[PDFName.of('First'), outline[0].ref],
[PDFName.of('Last'), outline[outline.length - 1].ref],
[PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(outline))]
]), context)
context.assign(outlineRef, outlineObject)
pdfDoc.catalog.set(PDFName.of('Outlines'), outlineRef)
return pdfDoc
}
const next = layer[i + 1]
const pdfObject = new Map([
[PDFName.of('Title'), PDFHexString.fromText(item.title)],
[PDFName.of('Dest'), PDFName.of(item.destination)],
[PDFName.of('Parent'), item.parentRef]
])
if (prev) {
pdfObject.set(PDFName.of('Prev'), prev.ref)
}
if (next) {
pdfObject.set(PDFName.of('Next'), next.ref)
}
if (item.children.length > 0) {
pdfObject.set(PDFName.of('First'), item.children[0].ref)
pdfObject.set(PDFName.of('Last'), item.children[item.children.length - 1].ref)
pdfObject.set(PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(item.children)))
}
context.assign(item.ref, PDFDict.fromMapWithContext(pdfObject, context))
buildPdfObjectsForOutline(item.children, context)
}
}
function buildPdfObjectsForOutline (layer, context) {
for (const [i, item] of layer.entries()) {
const prev = layer[i - 1]
const next = layer[i + 1]
const pdfObject = new Map([
[PDFName.of('Title'), PDFHexString.fromText(item.title)],
[PDFName.of('Dest'), PDFName.of(item.destination)],
[PDFName.of('Parent'), item.parentRef]
])
if (prev) {
pdfObject.set(PDFName.of('Prev'), prev.ref)
}
if (next) {
pdfObject.set(PDFName.of('Next'), next.ref)
}
if (item.children.length > 0) {
pdfObject.set(PDFName.of('First'), item.children[0].ref)
pdfObject.set(PDFName.of('Last'), item.children[item.children.length - 1].ref)
pdfObject.set(PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(item.children)))
}
context.assign(item.ref, PDFDict.fromMapWithContext(pdfObject, context))
buildPdfObjectsForOutline(item.children, context)
async function addOutline (pdfDoc, doc) {
const depth = doc.getAttribute('toclevels') || 2
const context = pdfDoc.context
const outlineRef = context.nextRef()
const outline = getOutline(doc, depth)
if (outline.length === 0) {
return pdfDoc
}
generateWarningsAboutMissingDestinations(outline, pdfDoc)
setRefsForOutlineItems(outline, context, outlineRef)
buildPdfObjectsForOutline(outline, context)
const outlineObject = PDFDict.fromMapWithContext(new Map([
[PDFName.of('First'), outline[0].ref],
[PDFName.of('Last'), outline[outline.length - 1].ref],
[PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(outline))]
]), context)
context.assign(outlineRef, outlineObject)
pdfDoc.catalog.set(PDFName.of('Outlines'), outlineRef)
return pdfDoc
}
function buildPdfObjectsForOutline (layer, context) {
for (const [i, item] of layer.entries()) {
const prev = layer[i - 1]
const next = layer[i + 1]
const pdfObject = new Map([
[PDFName.of('Title'), PDFHexString.fromText(item.title)],
[PDFName.of('Dest'), PDFName.of(item.destination)],
[PDFName.of('Parent'), item.parentRef]
])
if (prev) {
pdfObject.set(PDFName.of('Prev'), prev.ref)
}
if (next) {
pdfObject.set(PDFName.of('Next'), next.ref)
}
if (item.children.length > 0) {
pdfObject.set(PDFName.of('First'), item.children[0].ref)
pdfObject.set(PDFName.of('Last'), item.children[item.children.length - 1].ref)
pdfObject.set(PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(item.children)))
}
context.assign(item.ref, PDFDict.fromMapWithContext(pdfObject, context))
const outline = getOutline(doc, depth)
if (outline.length === 0) {
return pdfDoc
}
generateWarningsAboutMissingDestinations(outline, pdfDoc)
setRefsForOutlineItems(outline, context, outlineRef)
buildPdfObjectsForOutline(outline, context)
const outlineObject = PDFDict.fromMapWithContext(new Map([
[PDFName.of('First'), outline[0].ref],
[PDFName.of('Last'), outline[outline.length - 1].ref],
[PDFName.of('Count'), PDFNumber.of(countChildrenOfOutline(outline))]
]), context)
context.assign(outlineRef, outlineObject)
pdfDoc.catalog.set(PDFName.of('Outlines'), outlineRef)
return pdfDoc
}