@imggion/html2realpdf 0.1.13 → 0.1.14-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/libhtml2realpdf.wasm +0 -0
- package/dist/snapshot.js +40 -6
- package/dist/snapshot.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,6 +82,12 @@ HTML strings are supported too:
|
|
|
82
82
|
const pdf = await renderPdf("<h1>Hello from a real PDF</h1>");
|
|
83
83
|
```
|
|
84
84
|
|
|
85
|
+
PDF link annotations keep absolute `http`, `https`, `mailto`, `tel`, and `ftp`
|
|
86
|
+
URLs. Browser snapshots resolve relative links against `baseUrl` and canonicalize
|
|
87
|
+
international URLs; direct native/WASM input must already use canonical ASCII.
|
|
88
|
+
Unresolved, active, or local values such as `javascript`, `data`, and `file` are
|
|
89
|
+
removed. Set `enableLinks: false` to remove every link annotation.
|
|
90
|
+
|
|
85
91
|
## React
|
|
86
92
|
|
|
87
93
|
Pass a ref to a mounted element. The package understands React-shaped refs
|
|
Binary file
|
package/dist/snapshot.js
CHANGED
|
@@ -256,6 +256,8 @@ const SUPPORTED_DISPLAY = new Set([
|
|
|
256
256
|
"table-column-group",
|
|
257
257
|
]);
|
|
258
258
|
const ACTIVE_ELEMENTS = "script,iframe,object,embed";
|
|
259
|
+
const PDF_LINK_PROTOCOLS = new Set(["http:", "https:", "mailto:", "tel:", "ftp:"]);
|
|
260
|
+
const PDF_LINK_HOST_PROTOCOLS = new Set(["http:", "https:", "ftp:"]);
|
|
259
261
|
const AUTHORED_PAGE_MIRROR = "--html2realpdf-authored-page";
|
|
260
262
|
const AUTHORED_FLOW_DIMENSION_MIRRORS = {
|
|
261
263
|
width: "--html2realpdf-authored-width",
|
|
@@ -1316,9 +1318,7 @@ async function snapshotElement(element, options, canvasOrigins) {
|
|
|
1316
1318
|
for (const active of clone.matches(ACTIVE_ELEMENTS) ? [clone] : clone.querySelectorAll(ACTIVE_ELEMENTS)) {
|
|
1317
1319
|
active.remove();
|
|
1318
1320
|
}
|
|
1319
|
-
|
|
1320
|
-
for (const anchor of clone.querySelectorAll("a[href]"))
|
|
1321
|
-
anchor.removeAttribute("href");
|
|
1321
|
+
sanitizePdfLinks(clone, options.enableLinks, options.baseUrl ?? element.ownerDocument.baseURI);
|
|
1322
1322
|
const materializedInlineSvgs = await materializeInlineSvgs(clone, options, diagnostics);
|
|
1323
1323
|
await materializeBackgroundImages(clone, options, diagnostics);
|
|
1324
1324
|
await materializeImages(clone, options, diagnostics, materializedInlineSvgs);
|
|
@@ -1361,9 +1361,7 @@ async function snapshotHtmlString(source, options) {
|
|
|
1361
1361
|
refresh.remove();
|
|
1362
1362
|
for (const element of template.content.querySelectorAll("*"))
|
|
1363
1363
|
removeEventHandlers(element);
|
|
1364
|
-
|
|
1365
|
-
for (const anchor of template.content.querySelectorAll("a[href]"))
|
|
1366
|
-
anchor.removeAttribute("href");
|
|
1364
|
+
sanitizePdfLinks(template.content, options.enableLinks, options.baseUrl ?? document.baseURI);
|
|
1367
1365
|
await materializeExternalStylesheets(template.content, options, diagnostics);
|
|
1368
1366
|
const viewport = options.viewport ?? { width: 1280, height: 720 };
|
|
1369
1367
|
const iframe = createSnapshotFrame(viewport);
|
|
@@ -2966,6 +2964,42 @@ function removeEventHandlers(element) {
|
|
|
2966
2964
|
element.removeAttribute(attribute.name);
|
|
2967
2965
|
}
|
|
2968
2966
|
}
|
|
2967
|
+
/** Keeps only URI schemes that PDF viewers can open without active or local content. */
|
|
2968
|
+
function sanitizePdfLinks(root, enableLinks, baseUrl) {
|
|
2969
|
+
const rootElement = typeof root.matches === "function" ? root : null;
|
|
2970
|
+
const anchors = rootElement?.matches("a[href]")
|
|
2971
|
+
? [rootElement, ...root.querySelectorAll("a[href]")]
|
|
2972
|
+
: [...root.querySelectorAll("a[href]")];
|
|
2973
|
+
for (const anchor of anchors) {
|
|
2974
|
+
const href = anchor.getAttribute("href");
|
|
2975
|
+
const safeUrl = enableLinks === false || href === null ? null : safePdfLink(href, baseUrl);
|
|
2976
|
+
if (safeUrl === null)
|
|
2977
|
+
anchor.removeAttribute("href");
|
|
2978
|
+
else
|
|
2979
|
+
anchor.setAttribute("href", safeUrl);
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
function safePdfLink(href, baseUrl) {
|
|
2983
|
+
try {
|
|
2984
|
+
const resolved = new URL(href, baseUrl);
|
|
2985
|
+
if (!PDF_LINK_PROTOCOLS.has(resolved.protocol))
|
|
2986
|
+
return null;
|
|
2987
|
+
if (PDF_LINK_HOST_PROTOCOLS.has(resolved.protocol) && !isValidPdfLinkHost(resolved.hostname))
|
|
2988
|
+
return null;
|
|
2989
|
+
return resolved.href;
|
|
2990
|
+
}
|
|
2991
|
+
catch {
|
|
2992
|
+
return null;
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
function isValidPdfLinkHost(hostname) {
|
|
2996
|
+
if (hostname.startsWith("[") && hostname.endsWith("]"))
|
|
2997
|
+
return true;
|
|
2998
|
+
const host = hostname.endsWith(".") ? hostname.slice(0, -1) : hostname;
|
|
2999
|
+
if (host.length === 0 || host.length > 255)
|
|
3000
|
+
return false;
|
|
3001
|
+
return host.split(".").every((label) => label.length >= 1 && label.length <= 63 && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i.test(label));
|
|
3002
|
+
}
|
|
2969
3003
|
function isSvgElement(element) {
|
|
2970
3004
|
return element.namespaceURI === "http://www.w3.org/2000/svg";
|
|
2971
3005
|
}
|