fix(pdf): link URIs as ASCII hex (readable by poppler), PNG budget walks the chunk list (second IHDR and APNG refused)
This commit is contained in:
1 parent
dfd04e0bd7
commit
3a26c61119
4 files changed
+215
-49
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import { PDFName, PDFHexString, type PDFPage } from 'pdf-lib'
|
||||
import { ArchiveExportError } from '../errors'
|
||||
|
||||
export interface LinkRect {
|
||||
x: number
|
||||
@@ -8,6 +9,29 @@ export interface LinkRect {
|
||||
url: string
|
||||
}
|
||||
|
||||
/** Printable ASCII without the space: what the branding URL schema allows. */
|
||||
const ASCII_MIN = 0x21
|
||||
const ASCII_MAX = 0x7e
|
||||
|
||||
/**
|
||||
* The URL as the hex form of its ASCII bytes, one byte per character.
|
||||
* `PDFHexString.fromText` would write UTF-16BE with a BOM, which readers
|
||||
* that take `/URI` as raw bytes (poppler) turn into a dead link. A code
|
||||
* point outside printable ASCII cannot reach the sink: the schema refuses
|
||||
* it upstream, so it means the branding bypassed validation.
|
||||
*/
|
||||
function asciiHexOf(url: string): string {
|
||||
let hex = ''
|
||||
for (const char of url) {
|
||||
const code = char.codePointAt(0) ?? 0
|
||||
if (code < ASCII_MIN || code > ASCII_MAX) {
|
||||
throw new ArchiveExportError('branding_invalid', 'url')
|
||||
}
|
||||
hex += code.toString(16).padStart(2, '0')
|
||||
}
|
||||
return hex
|
||||
}
|
||||
|
||||
/**
|
||||
* A clickable `/Link` annotation opening `url`, appended to the page's
|
||||
* Annots. The URI is written as a hex string: pdf-lib does not escape `(`,
|
||||
@@ -20,7 +44,7 @@ export function addLinkAnnotation(page: PDFPage, rect: LinkRect): void {
|
||||
const actionDict = context.obj({
|
||||
Type: 'Action',
|
||||
S: 'URI',
|
||||
URI: PDFHexString.fromText(rect.url),
|
||||
URI: PDFHexString.of(asciiHexOf(rect.url)),
|
||||
})
|
||||
|
||||
const annotDict = context.obj({
|
||||
|
||||
Reference in new issue
Block a user