@pdfme/common 6.1.1-dev.4 → 6.1.1-dev.5
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/dist/helper.d.ts +30 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +126 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/helper.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PDFDocument, PDFPage } from '@pdfme/pdf-lib';
|
|
1
2
|
import { Template, Font, BasePdf, Plugins, BlankPdf } from './types.js';
|
|
2
3
|
export declare const cloneDeep: typeof structuredClone;
|
|
3
4
|
export declare const getFallbackFontName: (font: Font) => string;
|
|
@@ -16,6 +17,35 @@ export declare const getInputFromTemplate: (template: Template) => {
|
|
|
16
17
|
[key: string]: string;
|
|
17
18
|
}[];
|
|
18
19
|
export declare const isUrlSafeToFetch: (urlString: string) => boolean;
|
|
20
|
+
export type PdfLinkAnnotationRect = {
|
|
21
|
+
x: number;
|
|
22
|
+
y: number;
|
|
23
|
+
width: number;
|
|
24
|
+
height: number;
|
|
25
|
+
};
|
|
26
|
+
export declare const normalizeSafeLinkUri: (uri: string) => string | undefined;
|
|
27
|
+
export declare const getInternalLinkTarget: (href: string) => string | undefined;
|
|
28
|
+
export declare const normalizeInternalLinkHref: (href: string) => string | undefined;
|
|
29
|
+
export declare const normalizeLinkHref: (href: string) => string | undefined;
|
|
30
|
+
export declare const resetInternalLinkAnnotations: (_cache: Map<string | number, unknown>) => void;
|
|
31
|
+
export declare const registerInternalLinkAnchor: (arg: {
|
|
32
|
+
_cache: Map<string | number, unknown>;
|
|
33
|
+
name: string;
|
|
34
|
+
page: PDFPage;
|
|
35
|
+
x: number;
|
|
36
|
+
y: number;
|
|
37
|
+
}) => void;
|
|
38
|
+
export declare const registerInternalLinkAnnotation: (arg: {
|
|
39
|
+
_cache: Map<string | number, unknown>;
|
|
40
|
+
page: PDFPage;
|
|
41
|
+
targetName: string;
|
|
42
|
+
rect: PdfLinkAnnotationRect;
|
|
43
|
+
borderWidth?: number;
|
|
44
|
+
}) => void;
|
|
45
|
+
export declare const applyInternalLinkAnnotations: (arg: {
|
|
46
|
+
_cache: Map<string | number, unknown>;
|
|
47
|
+
pdfDoc: PDFDocument;
|
|
48
|
+
}) => void;
|
|
19
49
|
export declare const getB64BasePdf: (customPdf: ArrayBuffer | Uint8Array | string) => Promise<string>;
|
|
20
50
|
export declare const isBlankPdf: (basePdf: BasePdf) => basePdf is BlankPdf;
|
|
21
51
|
export declare const b64toUint8Array: (base64: string) => Uint8Array<ArrayBuffer>;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { PDFME_VERSION } from './version.js';
|
|
2
2
|
import { MM_TO_PT_RATIO, PT_TO_MM_RATIO, PT_TO_PX_RATIO, BLANK_PDF, BLANK_A4_PDF, CUSTOM_A4_PDF, ZOOM, DEFAULT_FONT_NAME } from './constants.js';
|
|
3
3
|
import type { ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, Mode, UIRenderProps, Plugin, Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, DynamicLayoutArgs, DynamicLayoutCallbackResult, DynamicLayoutPatchArgs, DynamicLayoutResult, GetDynamicLayout } from './types.js';
|
|
4
|
-
import {
|
|
4
|
+
import type { PdfLinkAnnotationRect } from './helper.js';
|
|
5
|
+
import { cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, isUrlSafeToFetch, getInternalLinkTarget, normalizeInternalLinkHref, normalizeLinkHref, normalizeSafeLinkUri, registerInternalLinkAnchor, registerInternalLinkAnnotation, resetInternalLinkAnnotations, applyInternalLinkAnnotations } from './helper.js';
|
|
5
6
|
import { getDynamicTemplate } from './dynamicTemplate.js';
|
|
6
7
|
import { replacePlaceholders } from './expression.js';
|
|
7
8
|
import { pluginRegistry } from './pluginRegistry.js';
|
|
8
|
-
export { PDFME_VERSION, MM_TO_PT_RATIO, PT_TO_MM_RATIO, PT_TO_PX_RATIO, BLANK_PDF, BLANK_A4_PDF, CUSTOM_A4_PDF, ZOOM, DEFAULT_FONT_NAME, cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, getDynamicTemplate, replacePlaceholders, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, pluginRegistry, isUrlSafeToFetch, };
|
|
9
|
-
export type { Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugin, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, UIRenderProps, Mode, DynamicLayoutArgs, DynamicLayoutCallbackResult, DynamicLayoutPatchArgs, DynamicLayoutResult, GetDynamicLayout, };
|
|
9
|
+
export { PDFME_VERSION, MM_TO_PT_RATIO, PT_TO_MM_RATIO, PT_TO_PX_RATIO, BLANK_PDF, BLANK_A4_PDF, CUSTOM_A4_PDF, ZOOM, DEFAULT_FONT_NAME, cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, getDynamicTemplate, replacePlaceholders, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, pluginRegistry, isUrlSafeToFetch, getInternalLinkTarget, normalizeInternalLinkHref, normalizeLinkHref, normalizeSafeLinkUri, registerInternalLinkAnchor, registerInternalLinkAnnotation, resetInternalLinkAnnotations, applyInternalLinkAnnotations, };
|
|
10
|
+
export type { Lang, Dict, Size, Schema, SchemaForUI, Font, ColorType, BasePdf, BlankPdf, CustomPdf, Template, CommonOptions, GeneratorOptions, Plugin, Plugins, PluginRegistry, GenerateProps, UIOptions, UIProps, PreviewProps, DesignerProps, ChangeSchemaItem, ChangeSchemas, SchemaPageArray, PropPanel, PropPanelSchema, PropPanelWidgetProps, PDFRenderProps, UIRenderProps, Mode, DynamicLayoutArgs, DynamicLayoutCallbackResult, DynamicLayoutPatchArgs, DynamicLayoutResult, GetDynamicLayout, PdfLinkAnnotationRect, };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { Buffer } from "buffer";
|
|
3
|
+
import { PDFName } from "@pdfme/pdf-lib";
|
|
3
4
|
import * as acorn from "acorn";
|
|
4
5
|
//#region src/version.ts
|
|
5
6
|
var PDFME_VERSION = "6.1.1";
|
|
@@ -365,6 +366,130 @@ var isUrlSafeToFetch = (urlString) => {
|
|
|
365
366
|
}
|
|
366
367
|
return true;
|
|
367
368
|
};
|
|
369
|
+
var SAFE_LINK_URI_PROTOCOLS = new Set([
|
|
370
|
+
"http:",
|
|
371
|
+
"https:",
|
|
372
|
+
"mailto:"
|
|
373
|
+
]);
|
|
374
|
+
var INTERNAL_LINK_CACHE_KEY = "pdfme-internal-link-cache";
|
|
375
|
+
var normalizeSafeLinkUri = (uri) => {
|
|
376
|
+
const trimmed = uri.trim();
|
|
377
|
+
if (!trimmed) return void 0;
|
|
378
|
+
let parsed;
|
|
379
|
+
try {
|
|
380
|
+
parsed = new URL(trimmed);
|
|
381
|
+
} catch {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
return SAFE_LINK_URI_PROTOCOLS.has(parsed.protocol.toLowerCase()) ? trimmed : void 0;
|
|
385
|
+
};
|
|
386
|
+
var getInternalLinkTarget = (href) => {
|
|
387
|
+
const trimmed = href.trim();
|
|
388
|
+
if (!trimmed.startsWith("#") || trimmed.length === 1) return void 0;
|
|
389
|
+
let target = trimmed.slice(1);
|
|
390
|
+
try {
|
|
391
|
+
target = decodeURIComponent(target);
|
|
392
|
+
} catch {}
|
|
393
|
+
target = target.trim();
|
|
394
|
+
if (!target || Array.from(target).some((char) => {
|
|
395
|
+
const code = char.charCodeAt(0);
|
|
396
|
+
return code < 32 || code === 127;
|
|
397
|
+
})) return;
|
|
398
|
+
return target;
|
|
399
|
+
};
|
|
400
|
+
var normalizeInternalLinkHref = (href) => {
|
|
401
|
+
const target = getInternalLinkTarget(href);
|
|
402
|
+
return target ? `#${target}` : void 0;
|
|
403
|
+
};
|
|
404
|
+
var normalizeLinkHref = (href) => normalizeSafeLinkUri(href) ?? normalizeInternalLinkHref(href);
|
|
405
|
+
var getInternalLinkCache = (_cache) => {
|
|
406
|
+
let cache = _cache.get(INTERNAL_LINK_CACHE_KEY);
|
|
407
|
+
if (!cache) {
|
|
408
|
+
cache = {
|
|
409
|
+
anchors: /* @__PURE__ */ new Map(),
|
|
410
|
+
annotations: []
|
|
411
|
+
};
|
|
412
|
+
_cache.set(INTERNAL_LINK_CACHE_KEY, cache);
|
|
413
|
+
}
|
|
414
|
+
return cache;
|
|
415
|
+
};
|
|
416
|
+
var resetInternalLinkAnnotations = (_cache) => {
|
|
417
|
+
_cache.set(INTERNAL_LINK_CACHE_KEY, {
|
|
418
|
+
anchors: /* @__PURE__ */ new Map(),
|
|
419
|
+
annotations: []
|
|
420
|
+
});
|
|
421
|
+
};
|
|
422
|
+
var registerInternalLinkAnchor = (arg) => {
|
|
423
|
+
const { _cache, name, page, x, y } = arg;
|
|
424
|
+
if (!name) return;
|
|
425
|
+
const cache = getInternalLinkCache(_cache);
|
|
426
|
+
const anchors = cache.anchors.get(name) ?? [];
|
|
427
|
+
anchors.push({
|
|
428
|
+
name,
|
|
429
|
+
page,
|
|
430
|
+
x,
|
|
431
|
+
y
|
|
432
|
+
});
|
|
433
|
+
cache.anchors.set(name, anchors);
|
|
434
|
+
};
|
|
435
|
+
var registerInternalLinkAnnotation = (arg) => {
|
|
436
|
+
const { _cache, page, targetName, rect, borderWidth } = arg;
|
|
437
|
+
if (!targetName || rect.width <= 0 || rect.height <= 0) return;
|
|
438
|
+
getInternalLinkCache(_cache).annotations.push({
|
|
439
|
+
page,
|
|
440
|
+
targetName,
|
|
441
|
+
rect,
|
|
442
|
+
borderWidth
|
|
443
|
+
});
|
|
444
|
+
};
|
|
445
|
+
var addGoToLinkAnnotation = (arg) => {
|
|
446
|
+
const { pdfDoc, page, target, rect, borderWidth = 0 } = arg;
|
|
447
|
+
if (rect.width <= 0 || rect.height <= 0) return;
|
|
448
|
+
const annotationRef = pdfDoc.context.register(pdfDoc.context.obj({
|
|
449
|
+
Type: PDFName.of("Annot"),
|
|
450
|
+
Subtype: PDFName.of("Link"),
|
|
451
|
+
Rect: [
|
|
452
|
+
rect.x,
|
|
453
|
+
rect.y,
|
|
454
|
+
rect.x + rect.width,
|
|
455
|
+
rect.y + rect.height
|
|
456
|
+
],
|
|
457
|
+
Border: [
|
|
458
|
+
0,
|
|
459
|
+
0,
|
|
460
|
+
borderWidth
|
|
461
|
+
],
|
|
462
|
+
A: {
|
|
463
|
+
Type: PDFName.of("Action"),
|
|
464
|
+
S: PDFName.of("GoTo"),
|
|
465
|
+
D: [
|
|
466
|
+
target.page.ref,
|
|
467
|
+
PDFName.of("XYZ"),
|
|
468
|
+
target.x,
|
|
469
|
+
target.y,
|
|
470
|
+
null
|
|
471
|
+
]
|
|
472
|
+
}
|
|
473
|
+
}));
|
|
474
|
+
page.node.addAnnot(annotationRef);
|
|
475
|
+
};
|
|
476
|
+
var applyInternalLinkAnnotations = (arg) => {
|
|
477
|
+
const { _cache, pdfDoc } = arg;
|
|
478
|
+
const cache = getInternalLinkCache(_cache);
|
|
479
|
+
cache.annotations.forEach((annotation) => {
|
|
480
|
+
const anchors = cache.anchors.get(annotation.targetName) ?? [];
|
|
481
|
+
if (anchors.length === 0) throw new Error(`[@pdfme/generator] Internal link target "#${annotation.targetName}" was not found.`);
|
|
482
|
+
if (anchors.length > 1) throw new Error(`[@pdfme/generator] Internal link target "#${annotation.targetName}" is ambiguous because multiple schemas use that name.`);
|
|
483
|
+
addGoToLinkAnnotation({
|
|
484
|
+
pdfDoc,
|
|
485
|
+
page: annotation.page,
|
|
486
|
+
target: anchors[0],
|
|
487
|
+
rect: annotation.rect,
|
|
488
|
+
borderWidth: annotation.borderWidth
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
resetInternalLinkAnnotations(_cache);
|
|
492
|
+
};
|
|
368
493
|
var getB64BasePdf = async (customPdf) => {
|
|
369
494
|
if (typeof customPdf === "string" && !customPdf.startsWith("data:application/pdf;") && typeof window !== "undefined") {
|
|
370
495
|
if (!isUrlSafeToFetch(customPdf)) throw Error("[@pdfme/common] Invalid or unsafe URL for basePdf. Only http: and https: URLs pointing to public hosts are allowed.");
|
|
@@ -985,6 +1110,6 @@ var pluginRegistry = (plugins) => {
|
|
|
985
1110
|
};
|
|
986
1111
|
};
|
|
987
1112
|
//#endregion
|
|
988
|
-
export { BLANK_A4_PDF, BLANK_PDF, CUSTOM_A4_PDF, DEFAULT_FONT_NAME, MM_TO_PT_RATIO, PDFME_VERSION, PT_TO_MM_RATIO, PT_TO_PX_RATIO, ZOOM, b64toUint8Array, checkDesignerProps, checkFont, checkGenerateProps, checkInputs, checkPreviewProps, checkTemplate, checkUIOptions, checkUIProps, cloneDeep, getB64BasePdf, getDefaultFont, getDynamicTemplate, getFallbackFontName, getInputFromTemplate, isBlankPdf, isHexValid, isUrlSafeToFetch, mm2pt, pluginRegistry, pt2mm, pt2px, px2mm, replacePlaceholders };
|
|
1113
|
+
export { BLANK_A4_PDF, BLANK_PDF, CUSTOM_A4_PDF, DEFAULT_FONT_NAME, MM_TO_PT_RATIO, PDFME_VERSION, PT_TO_MM_RATIO, PT_TO_PX_RATIO, ZOOM, applyInternalLinkAnnotations, b64toUint8Array, checkDesignerProps, checkFont, checkGenerateProps, checkInputs, checkPreviewProps, checkTemplate, checkUIOptions, checkUIProps, cloneDeep, getB64BasePdf, getDefaultFont, getDynamicTemplate, getFallbackFontName, getInputFromTemplate, getInternalLinkTarget, isBlankPdf, isHexValid, isUrlSafeToFetch, mm2pt, normalizeInternalLinkHref, normalizeLinkHref, normalizeSafeLinkUri, pluginRegistry, pt2mm, pt2px, px2mm, registerInternalLinkAnchor, registerInternalLinkAnnotation, replacePlaceholders, resetInternalLinkAnnotations };
|
|
989
1114
|
|
|
990
1115
|
//# sourceMappingURL=index.js.map
|