* fix(deps): resolve Dependabot security alerts * fix(reader): scope PDF.js 6 integration * fix(reader): address PDF.js compatibility gaps
517 lines
18 KiB
JavaScript
517 lines
18 KiB
JavaScript
const pdfjsPath = path => `/vendor/pdfjs/${path}`
|
|
|
|
import '@pdfjs/pdf.min.mjs'
|
|
const pdfjsLib = globalThis.pdfjsLib
|
|
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsPath('pdf.worker.min.mjs')
|
|
|
|
const fetchText = async url => await (await fetch(url)).text()
|
|
|
|
let pdfLayerCSS = null
|
|
|
|
// Track active render tasks per iframe document to cancel superseded renders
|
|
const activeRenderTasks = new WeakMap()
|
|
// Generation counter per document to detect stale renders after async gaps
|
|
const renderGenerations = new WeakMap()
|
|
|
|
// Set up panning and selection event handlers once per iframe document
|
|
const setupPanningEvents = (doc) => {
|
|
if (doc._readestEventsInitialized) return
|
|
doc._readestEventsInitialized = true
|
|
|
|
const container = doc.querySelector('.textLayer')
|
|
if (!container) return
|
|
|
|
let isPanning = false
|
|
let startX = 0
|
|
let startY = 0
|
|
let scrollLeft = 0
|
|
let scrollTop = 0
|
|
let scrollParent = null
|
|
|
|
const findScrollableParent = (element) => {
|
|
let current = element
|
|
while (current) {
|
|
if (current !== document.body && current.nodeType === 1) {
|
|
const style = window.getComputedStyle(current)
|
|
const overflow = style.overflow + style.overflowY + style.overflowX
|
|
if (/(auto|scroll)/.test(overflow)) {
|
|
if (current.scrollHeight > current.clientHeight ||
|
|
current.scrollWidth > current.clientWidth) {
|
|
return current
|
|
}
|
|
}
|
|
}
|
|
if (current.parentElement) {
|
|
current = current.parentElement
|
|
} else if (current.parentNode && current.parentNode.host) {
|
|
current = current.parentNode.host
|
|
} else {
|
|
break
|
|
}
|
|
}
|
|
return window
|
|
}
|
|
|
|
container.onpointerdown = (e) => {
|
|
const selection = doc.getSelection()
|
|
const hasTextSelection = selection && selection.toString().length > 0
|
|
|
|
const elementUnderCursor = doc.elementFromPoint(e.clientX, e.clientY)
|
|
const hasTextUnderneath = elementUnderCursor &&
|
|
(elementUnderCursor.tagName === 'SPAN' || elementUnderCursor.tagName === 'P') &&
|
|
elementUnderCursor.textContent.trim().length > 0
|
|
|
|
if (!hasTextUnderneath && !hasTextSelection) {
|
|
isPanning = true
|
|
startX = e.screenX
|
|
startY = e.screenY
|
|
|
|
const iframe = doc.defaultView?.frameElement
|
|
if (iframe) {
|
|
scrollParent = findScrollableParent(iframe)
|
|
if (scrollParent === window) {
|
|
scrollLeft = window.scrollX || window.pageXOffset
|
|
scrollTop = window.scrollY || window.pageYOffset
|
|
} else {
|
|
scrollLeft = scrollParent.scrollLeft
|
|
scrollTop = scrollParent.scrollTop
|
|
}
|
|
container.style.cursor = 'grabbing'
|
|
}
|
|
} else {
|
|
container.classList.add('selecting')
|
|
}
|
|
}
|
|
|
|
container.onpointermove = (e) => {
|
|
if (isPanning && scrollParent) {
|
|
e.preventDefault()
|
|
|
|
const dx = e.screenX - startX
|
|
const dy = e.screenY - startY
|
|
|
|
if (scrollParent === window) {
|
|
window.scrollTo(scrollLeft - dx, scrollTop - dy)
|
|
} else {
|
|
scrollParent.scrollLeft = scrollLeft - dx
|
|
scrollParent.scrollTop = scrollTop - dy
|
|
}
|
|
}
|
|
}
|
|
|
|
container.onpointerup = () => {
|
|
if (isPanning) {
|
|
isPanning = false
|
|
scrollParent = null
|
|
container.style.cursor = 'grab'
|
|
} else {
|
|
container.classList.remove('selecting')
|
|
}
|
|
}
|
|
|
|
container.onpointerleave = () => {
|
|
if (isPanning) {
|
|
isPanning = false
|
|
scrollParent = null
|
|
container.style.cursor = 'grab'
|
|
}
|
|
}
|
|
|
|
doc.addEventListener('selectionchange', () => {
|
|
const selection = doc.getSelection()
|
|
if (selection && selection.toString().length > 0) {
|
|
container.style.cursor = 'text'
|
|
} else if (!isPanning) {
|
|
container.style.cursor = 'grab'
|
|
}
|
|
})
|
|
|
|
container.style.cursor = 'grab'
|
|
}
|
|
|
|
const render = async (page, doc, zoom, pageColors, getAttachmentContent) => {
|
|
if (!doc) return
|
|
|
|
// Increment generation to invalidate any in-progress render for this doc
|
|
const generation = (renderGenerations.get(doc) || 0) + 1
|
|
renderGenerations.set(doc, generation)
|
|
|
|
// Cancel any in-progress render task for this document
|
|
const existingTask = activeRenderTasks.get(doc)
|
|
if (existingTask) {
|
|
existingTask.cancel()
|
|
activeRenderTasks.delete(doc)
|
|
}
|
|
|
|
const scale = zoom * devicePixelRatio
|
|
doc.documentElement.style.transform = `scale(${1 / devicePixelRatio})`
|
|
doc.documentElement.style.transformOrigin = 'top left'
|
|
doc.documentElement.style.setProperty('--total-scale-factor', scale)
|
|
doc.documentElement.style.setProperty('--user-unit', '1')
|
|
doc.documentElement.style.setProperty('--scale-round-x', '1px')
|
|
doc.documentElement.style.setProperty('--scale-round-y', '1px')
|
|
const viewport = page.getViewport({ scale })
|
|
|
|
// the canvas must be in the `PDFDocument`'s `ownerDocument`
|
|
// (`globalThis.document` by default); that's where the fonts are loaded
|
|
const canvas = document.createElement('canvas')
|
|
canvas.height = viewport.height
|
|
canvas.width = viewport.width
|
|
const renderTask = page.render({ canvas, viewport, pageColors })
|
|
activeRenderTasks.set(doc, renderTask)
|
|
|
|
try {
|
|
await renderTask.promise
|
|
} catch {
|
|
// Render was cancelled or failed — release canvas bitmap memory
|
|
canvas.width = 0
|
|
canvas.height = 0
|
|
return
|
|
} finally {
|
|
if (activeRenderTasks.get(doc) === renderTask) {
|
|
activeRenderTasks.delete(doc)
|
|
}
|
|
}
|
|
|
|
// Bail out if a newer render has started or iframe was removed
|
|
if (renderGenerations.get(doc) !== generation || !doc.defaultView) {
|
|
canvas.width = 0
|
|
canvas.height = 0
|
|
return
|
|
}
|
|
|
|
const canvasElement = doc.querySelector('#canvas')
|
|
if (!canvasElement) {
|
|
canvas.width = 0
|
|
canvas.height = 0
|
|
return
|
|
}
|
|
|
|
// Release old canvas bitmap memory before replacing
|
|
const oldCanvas = canvasElement.querySelector('canvas')
|
|
if (oldCanvas) {
|
|
oldCanvas.width = 0
|
|
oldCanvas.height = 0
|
|
}
|
|
canvasElement.replaceChildren(doc.adoptNode(canvas))
|
|
|
|
// Clear text layer before re-rendering to prevent DOM accumulation
|
|
const container = doc.querySelector('.textLayer')
|
|
container.replaceChildren()
|
|
const textLayer = new pdfjsLib.TextLayer({
|
|
textContentSource: await page.streamTextContent(),
|
|
container, viewport,
|
|
})
|
|
await textLayer.render()
|
|
|
|
// Bail out if superseded after async text layer render
|
|
if (renderGenerations.get(doc) !== generation) return
|
|
|
|
// hide "offscreen" canvases appended to document when rendering text layer
|
|
// https://github.com/mozilla/pdf.js/blob/642b9a5ae67ef642b9a8808fd9efd447e8c350e2/web/pdf_viewer.css#L51-L58
|
|
for (const hiddenCanvas of document.querySelectorAll('.hiddenCanvasElement'))
|
|
Object.assign(hiddenCanvas.style, {
|
|
position: 'absolute',
|
|
top: '0',
|
|
left: '0',
|
|
width: '0',
|
|
height: '0',
|
|
display: 'none',
|
|
})
|
|
|
|
// fix text selection
|
|
// https://github.com/mozilla/pdf.js/blob/642b9a5ae67ef642b9a8808fd9efd447e8c350e2/web/text_layer_builder.js#L105-L107
|
|
const endOfContent = document.createElement('div')
|
|
endOfContent.className = 'endOfContent'
|
|
container.append(endOfContent)
|
|
|
|
// Set up panning/selection event handlers once per document
|
|
setupPanningEvents(doc)
|
|
|
|
// Clear annotation layer before re-rendering to prevent DOM accumulation
|
|
const div = doc.querySelector('.annotationLayer')
|
|
div.replaceChildren()
|
|
const linkService = {
|
|
goToDestination: () => {},
|
|
getDestinationHash: dest => JSON.stringify(dest),
|
|
addLinkAttributes: (link, url) => link.href = url,
|
|
getAttachmentContent,
|
|
}
|
|
await new pdfjsLib.AnnotationLayer({ page, viewport, div, linkService }).render({
|
|
annotations: await page.getAnnotations(),
|
|
})
|
|
}
|
|
|
|
const renderPage = async (page, getImageBlob, getAttachmentContent) => {
|
|
const viewport = page.getViewport({ scale: 1 })
|
|
if (getImageBlob) {
|
|
const canvas = document.createElement('canvas')
|
|
canvas.height = viewport.height
|
|
canvas.width = viewport.width
|
|
await page.render({ canvas, viewport }).promise
|
|
return new Promise(resolve => canvas.toBlob(blob => {
|
|
// Release canvas bitmap memory after extracting the blob
|
|
canvas.width = 0
|
|
canvas.height = 0
|
|
resolve(blob)
|
|
}))
|
|
}
|
|
if (pdfLayerCSS == null) {
|
|
pdfLayerCSS = await fetchText(pdfjsPath('pdf_layers.css'))
|
|
}
|
|
const data = `
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=${viewport.width}, height=${viewport.height}">
|
|
<style>
|
|
html, body {
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
${pdfLayerCSS}
|
|
</style>
|
|
<div id="canvas"></div>
|
|
<div class="textLayer"></div>
|
|
<div class="annotationLayer"></div>
|
|
`
|
|
const src = URL.createObjectURL(new Blob([data], { type: 'text/html' }))
|
|
const onZoom = ({ doc, scale, pageColors }) =>
|
|
render(page, doc, scale, pageColors, getAttachmentContent)
|
|
return { src, data, onZoom }
|
|
}
|
|
|
|
const makeTOCItem = async (item, pdf) => {
|
|
let pageIndex = undefined
|
|
|
|
if (item.dest) {
|
|
try {
|
|
const dest = typeof item.dest === 'string'
|
|
? await pdf.getDestination(item.dest)
|
|
: item.dest
|
|
if (dest?.[0]) {
|
|
pageIndex = await pdf.getPageIndex(dest[0])
|
|
}
|
|
} catch (e) {
|
|
console.warn('Failed to get page index for TOC item:', item.title, e)
|
|
}
|
|
}
|
|
|
|
return {
|
|
label: item.title,
|
|
href: item.dest ? JSON.stringify(item.dest) : '',
|
|
index: pageIndex,
|
|
subitems: item.items?.length
|
|
? await Promise.all(item.items.map(i => makeTOCItem(i, pdf)))
|
|
: null,
|
|
}
|
|
}
|
|
|
|
const MAX_CACHED_PAGES = 8
|
|
|
|
const CALIBRE_NS = 'http://calibre-ebook.com/xmp-namespace'
|
|
const CALIBRE_SI_NS = 'http://calibre-ebook.com/xmp-namespace-series-index'
|
|
const RDF_NS = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
|
|
|
|
// Calibre writes series metadata into the XMP packet as
|
|
// <calibre:series rdf:parseType="Resource">
|
|
// <rdf:value>Name</rdf:value>
|
|
// <calibreSI:series_index>1.00</calibreSI:series_index>
|
|
// </calibre:series>
|
|
const parseCalibreSeriesFromXMP = raw => {
|
|
if (!raw || typeof raw !== 'string') return null
|
|
let doc
|
|
try {
|
|
doc = new DOMParser().parseFromString(raw, 'application/xml')
|
|
} catch {
|
|
return null
|
|
}
|
|
if (!doc || doc.getElementsByTagName('parsererror').length) return null
|
|
const seriesEls = doc.getElementsByTagNameNS(CALIBRE_NS, 'series')
|
|
const seriesEl = seriesEls.item(0)
|
|
if (!seriesEl) return null
|
|
const valueEl = seriesEl.getElementsByTagNameNS(RDF_NS, 'value').item(0)
|
|
const name = valueEl?.textContent?.trim()
|
|
if (!name) return null
|
|
const indexEl = seriesEl.getElementsByTagNameNS(CALIBRE_SI_NS, 'series_index').item(0)
|
|
const position = indexEl?.textContent?.trim()
|
|
return position ? { name, position } : { name }
|
|
}
|
|
|
|
export const makePDF = async file => {
|
|
const transport = new pdfjsLib.PDFDataRangeTransport(file.size, [])
|
|
transport.requestDataRange = (begin, end) => {
|
|
file.slice(begin, end).arrayBuffer().then(chunk => {
|
|
transport.onDataRange(begin, chunk)
|
|
})
|
|
}
|
|
const loadingTask = pdfjsLib.getDocument({
|
|
range: transport,
|
|
wasmUrl: pdfjsPath(''),
|
|
cMapUrl: pdfjsPath('cmaps/'),
|
|
standardFontDataUrl: pdfjsPath('standard_fonts/'),
|
|
enableScripting: false,
|
|
isEvalSupported: false,
|
|
})
|
|
const pdf = await loadingTask.promise
|
|
const getAttachmentContent = async id => {
|
|
try {
|
|
return await pdf.getAttachmentContent(id)
|
|
} catch (error) {
|
|
console.warn(`Unable to load PDF attachment content: ${error}`)
|
|
return null
|
|
}
|
|
}
|
|
|
|
// Get viewport dimensions from first page for fixed-layout rendering
|
|
const firstPage = await pdf.getPage(1)
|
|
const firstViewport = firstPage.getViewport({ scale: 1 })
|
|
const book = { rendition: {
|
|
layout: 'pre-paginated',
|
|
viewport: { width: firstViewport.width, height: firstViewport.height },
|
|
} }
|
|
|
|
const { metadata, info } = await pdf.getMetadata() ?? {}
|
|
// TODO: for better results, parse `metadata.getRaw()`
|
|
book.metadata = {
|
|
title: metadata?.get('dc:title') ?? info?.Title,
|
|
author: metadata?.get('dc:creator') ?? info?.Author,
|
|
contributor: metadata?.get('dc:contributor'),
|
|
description: metadata?.get('dc:description') ?? info?.Subject,
|
|
language: metadata?.get('dc:language'),
|
|
publisher: metadata?.get('dc:publisher'),
|
|
subject: metadata?.get('dc:subject'),
|
|
identifier: metadata?.get('dc:identifier'),
|
|
source: metadata?.get('dc:source'),
|
|
rights: metadata?.get('dc:rights'),
|
|
}
|
|
|
|
const calibreSeries = parseCalibreSeriesFromXMP(metadata?.getRaw?.())
|
|
if (calibreSeries) book.metadata.belongsTo = { series: calibreSeries }
|
|
|
|
const outline = await pdf.getOutline()
|
|
book.toc = outline ? await Promise.all(outline.map(item => makeTOCItem(item, pdf))) : null
|
|
|
|
const cache = new Map()
|
|
const pageCache = new Map()
|
|
const getPage = async (i) => {
|
|
const cached = pageCache.get(i)
|
|
if (cached) {
|
|
// Move to end for LRU ordering
|
|
pageCache.delete(i)
|
|
pageCache.set(i, cached)
|
|
return cached
|
|
}
|
|
const page = await pdf.getPage(i + 1)
|
|
pageCache.set(i, page)
|
|
|
|
// Evict oldest pages when over limit, freeing internal page data
|
|
while (pageCache.size > MAX_CACHED_PAGES) {
|
|
const oldestKey = pageCache.keys().next().value
|
|
const oldPage = pageCache.get(oldestKey)
|
|
pageCache.delete(oldestKey)
|
|
oldPage?.cleanup()
|
|
}
|
|
|
|
return page
|
|
}
|
|
book.sections = Array.from({ length: pdf.numPages }).map((_, i) => ({
|
|
id: i,
|
|
load: async () => {
|
|
const cached = cache.get(i)
|
|
if (cached) {
|
|
// Move to end for LRU ordering
|
|
cache.delete(i)
|
|
cache.set(i, cached)
|
|
return cached
|
|
}
|
|
const url = await renderPage(await getPage(i), false, getAttachmentContent)
|
|
cache.set(i, url)
|
|
|
|
// Evict oldest render results when over limit
|
|
while (cache.size > MAX_CACHED_PAGES) {
|
|
const oldestKey = cache.keys().next().value
|
|
const oldEntry = cache.get(oldestKey)
|
|
cache.delete(oldestKey)
|
|
if (oldEntry?.src) URL.revokeObjectURL(oldEntry.src)
|
|
}
|
|
|
|
return url
|
|
},
|
|
createDocument: async () => {
|
|
const page = await getPage(i)
|
|
const doc = document.implementation.createHTMLDocument('')
|
|
|
|
const canvas = doc.createElement('div')
|
|
canvas.id = 'canvas'
|
|
doc.body.appendChild(canvas)
|
|
|
|
const textLayer = doc.createElement('div')
|
|
textLayer.className = 'textLayer'
|
|
doc.body.appendChild(textLayer)
|
|
|
|
const annotationLayer = doc.createElement('div')
|
|
annotationLayer.className = 'annotationLayer'
|
|
doc.body.appendChild(annotationLayer)
|
|
|
|
// TextLayer requires canvas 2d context for font metrics;
|
|
// fall back to manual span construction when unavailable
|
|
const probe = doc.createElement('canvas')
|
|
if (probe.getContext?.('2d')) {
|
|
const textLayerInstance = new pdfjsLib.TextLayer({
|
|
textContentSource: await page.streamTextContent(),
|
|
container: textLayer, viewport: page.getViewport({ scale: 1 }),
|
|
})
|
|
await textLayerInstance.render()
|
|
} else {
|
|
const content = await page.getTextContent()
|
|
for (const item of content.items) {
|
|
if (item.str) {
|
|
const span = doc.createElement('span')
|
|
span.textContent = item.str
|
|
textLayer.appendChild(span)
|
|
}
|
|
}
|
|
}
|
|
return doc
|
|
},
|
|
size: 1000,
|
|
}))
|
|
book.isExternal = uri => /^\w+:/i.test(uri)
|
|
book.resolveHref = async href => {
|
|
const parsed = JSON.parse(href)
|
|
const dest = typeof parsed === 'string'
|
|
? await pdf.getDestination(parsed) : parsed
|
|
const index = await pdf.getPageIndex(dest[0])
|
|
return { index }
|
|
}
|
|
book.splitTOCHref = async href => {
|
|
if (!href) return [null, null]
|
|
const parsed = JSON.parse(href)
|
|
const dest = typeof parsed === 'string'
|
|
? await pdf.getDestination(parsed) : parsed
|
|
try {
|
|
const index = await pdf.getPageIndex(dest[0])
|
|
return [index, null]
|
|
} catch (e) {
|
|
console.warn('Error getting page index for href', href, e)
|
|
return [null, null]
|
|
}
|
|
}
|
|
book.getTOCFragment = doc => doc.documentElement
|
|
book.getCover = async () => renderPage(await pdf.getPage(1), true)
|
|
book.destroy = () => {
|
|
// Clean up all cached canvases and revoke blob URLs
|
|
for (const [, entry] of cache) {
|
|
if (entry?.src) URL.revokeObjectURL(entry.src)
|
|
}
|
|
cache.clear()
|
|
for (const [, page] of pageCache) {
|
|
page?.cleanup()
|
|
}
|
|
pageCache.clear()
|
|
return loadingTask.destroy()
|
|
}
|
|
return book
|
|
}
|