@pdfme/common 6.1.1-dev.4 → 6.1.1-dev.7

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 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,12 @@
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 { cloneDeep, getFallbackFontName, getDefaultFont, getB64BasePdf, b64toUint8Array, checkFont, checkInputs, checkUIOptions, checkTemplate, checkUIProps, checkPreviewProps, checkDesignerProps, checkGenerateProps, mm2pt, pt2mm, pt2px, px2mm, isHexValid, getInputFromTemplate, isBlankPdf, isUrlSafeToFetch } from './helper.js';
4
+ import type { PdfLinkAnnotationRect } from './helper.js';
5
+ import type { PageOrientation, PageSize, PageSizePreset } from './pageSize.js';
6
+ 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';
7
+ import { PAGE_SIZE_PRESETS, detectPaperSize, resolvePageSize } from './pageSize.js';
5
8
  import { getDynamicTemplate } from './dynamicTemplate.js';
6
9
  import { replacePlaceholders } from './expression.js';
7
10
  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, };
11
+ 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, PAGE_SIZE_PRESETS, detectPaperSize, resolvePageSize, };
12
+ 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, PageOrientation, PageSize, PageSizePreset, };
package/dist/index.js CHANGED
@@ -1,9 +1,74 @@
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";
6
7
  //#endregion
8
+ //#region src/pageSize.ts
9
+ var PAGE_SIZE_PRESETS = {
10
+ A3: {
11
+ width: 297,
12
+ height: 420
13
+ },
14
+ A4: {
15
+ width: 210,
16
+ height: 297
17
+ },
18
+ A5: {
19
+ width: 148,
20
+ height: 210
21
+ },
22
+ A6: {
23
+ width: 105,
24
+ height: 148
25
+ },
26
+ B4: {
27
+ width: 250,
28
+ height: 353
29
+ },
30
+ B5: {
31
+ width: 176,
32
+ height: 250
33
+ },
34
+ B6: {
35
+ width: 125,
36
+ height: 176
37
+ },
38
+ Letter: {
39
+ width: 215.9,
40
+ height: 279.4
41
+ },
42
+ Legal: {
43
+ width: 215.9,
44
+ height: 355.6
45
+ },
46
+ Tabloid: {
47
+ width: 279.4,
48
+ height: 431.8
49
+ }
50
+ };
51
+ var resolvePageSize = (size = "A4", orientation = "portrait") => {
52
+ const resolved = typeof size === "string" ? PAGE_SIZE_PRESETS[size] : {
53
+ width: size.width,
54
+ height: size.height
55
+ };
56
+ if (!resolved) throw new Error(`@pdfme/common: unknown page size preset "${String(size)}"`);
57
+ if (orientation === "landscape") return {
58
+ width: resolved.height,
59
+ height: resolved.width
60
+ };
61
+ return {
62
+ width: resolved.width,
63
+ height: resolved.height
64
+ };
65
+ };
66
+ var detectPaperSize = (width, height, tolerance = 2) => {
67
+ const entries = Object.entries(PAGE_SIZE_PRESETS);
68
+ for (const [name, size] of entries) if (Math.abs(width - size.width) <= tolerance && Math.abs(height - size.height) <= tolerance || Math.abs(width - size.height) <= tolerance && Math.abs(height - size.width) <= tolerance) return `${name} ${width < height ? "portrait" : "landscape"}`;
69
+ return null;
70
+ };
71
+ //#endregion
7
72
  //#region src/constants.ts
8
73
  var PT_TO_PX_RATIO = 1.333;
9
74
  var PT_TO_MM_RATIO = .3528;
@@ -13,8 +78,7 @@ var ZOOM = 3.7795275591;
13
78
  * Blank A4 PDF size.
14
79
  */
15
80
  var BLANK_A4_PDF = {
16
- width: 210,
17
- height: 297,
81
+ ...PAGE_SIZE_PRESETS.A4,
18
82
  padding: [
19
83
  10,
20
84
  10,
@@ -365,6 +429,130 @@ var isUrlSafeToFetch = (urlString) => {
365
429
  }
366
430
  return true;
367
431
  };
432
+ var SAFE_LINK_URI_PROTOCOLS = new Set([
433
+ "http:",
434
+ "https:",
435
+ "mailto:"
436
+ ]);
437
+ var INTERNAL_LINK_CACHE_KEY = "pdfme-internal-link-cache";
438
+ var normalizeSafeLinkUri = (uri) => {
439
+ const trimmed = uri.trim();
440
+ if (!trimmed) return void 0;
441
+ let parsed;
442
+ try {
443
+ parsed = new URL(trimmed);
444
+ } catch {
445
+ return;
446
+ }
447
+ return SAFE_LINK_URI_PROTOCOLS.has(parsed.protocol.toLowerCase()) ? trimmed : void 0;
448
+ };
449
+ var getInternalLinkTarget = (href) => {
450
+ const trimmed = href.trim();
451
+ if (!trimmed.startsWith("#") || trimmed.length === 1) return void 0;
452
+ let target = trimmed.slice(1);
453
+ try {
454
+ target = decodeURIComponent(target);
455
+ } catch {}
456
+ target = target.trim();
457
+ if (!target || Array.from(target).some((char) => {
458
+ const code = char.charCodeAt(0);
459
+ return code < 32 || code === 127;
460
+ })) return;
461
+ return target;
462
+ };
463
+ var normalizeInternalLinkHref = (href) => {
464
+ const target = getInternalLinkTarget(href);
465
+ return target ? `#${target}` : void 0;
466
+ };
467
+ var normalizeLinkHref = (href) => normalizeSafeLinkUri(href) ?? normalizeInternalLinkHref(href);
468
+ var getInternalLinkCache = (_cache) => {
469
+ let cache = _cache.get(INTERNAL_LINK_CACHE_KEY);
470
+ if (!cache) {
471
+ cache = {
472
+ anchors: /* @__PURE__ */ new Map(),
473
+ annotations: []
474
+ };
475
+ _cache.set(INTERNAL_LINK_CACHE_KEY, cache);
476
+ }
477
+ return cache;
478
+ };
479
+ var resetInternalLinkAnnotations = (_cache) => {
480
+ _cache.set(INTERNAL_LINK_CACHE_KEY, {
481
+ anchors: /* @__PURE__ */ new Map(),
482
+ annotations: []
483
+ });
484
+ };
485
+ var registerInternalLinkAnchor = (arg) => {
486
+ const { _cache, name, page, x, y } = arg;
487
+ if (!name) return;
488
+ const cache = getInternalLinkCache(_cache);
489
+ const anchors = cache.anchors.get(name) ?? [];
490
+ anchors.push({
491
+ name,
492
+ page,
493
+ x,
494
+ y
495
+ });
496
+ cache.anchors.set(name, anchors);
497
+ };
498
+ var registerInternalLinkAnnotation = (arg) => {
499
+ const { _cache, page, targetName, rect, borderWidth } = arg;
500
+ if (!targetName || rect.width <= 0 || rect.height <= 0) return;
501
+ getInternalLinkCache(_cache).annotations.push({
502
+ page,
503
+ targetName,
504
+ rect,
505
+ borderWidth
506
+ });
507
+ };
508
+ var addGoToLinkAnnotation = (arg) => {
509
+ const { pdfDoc, page, target, rect, borderWidth = 0 } = arg;
510
+ if (rect.width <= 0 || rect.height <= 0) return;
511
+ const annotationRef = pdfDoc.context.register(pdfDoc.context.obj({
512
+ Type: PDFName.of("Annot"),
513
+ Subtype: PDFName.of("Link"),
514
+ Rect: [
515
+ rect.x,
516
+ rect.y,
517
+ rect.x + rect.width,
518
+ rect.y + rect.height
519
+ ],
520
+ Border: [
521
+ 0,
522
+ 0,
523
+ borderWidth
524
+ ],
525
+ A: {
526
+ Type: PDFName.of("Action"),
527
+ S: PDFName.of("GoTo"),
528
+ D: [
529
+ target.page.ref,
530
+ PDFName.of("XYZ"),
531
+ target.x,
532
+ target.y,
533
+ null
534
+ ]
535
+ }
536
+ }));
537
+ page.node.addAnnot(annotationRef);
538
+ };
539
+ var applyInternalLinkAnnotations = (arg) => {
540
+ const { _cache, pdfDoc } = arg;
541
+ const cache = getInternalLinkCache(_cache);
542
+ cache.annotations.forEach((annotation) => {
543
+ const anchors = cache.anchors.get(annotation.targetName) ?? [];
544
+ if (anchors.length === 0) throw new Error(`[@pdfme/generator] Internal link target "#${annotation.targetName}" was not found.`);
545
+ if (anchors.length > 1) throw new Error(`[@pdfme/generator] Internal link target "#${annotation.targetName}" is ambiguous because multiple schemas use that name.`);
546
+ addGoToLinkAnnotation({
547
+ pdfDoc,
548
+ page: annotation.page,
549
+ target: anchors[0],
550
+ rect: annotation.rect,
551
+ borderWidth: annotation.borderWidth
552
+ });
553
+ });
554
+ resetInternalLinkAnnotations(_cache);
555
+ };
368
556
  var getB64BasePdf = async (customPdf) => {
369
557
  if (typeof customPdf === "string" && !customPdf.startsWith("data:application/pdf;") && typeof window !== "undefined") {
370
558
  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 +1173,6 @@ var pluginRegistry = (plugins) => {
985
1173
  };
986
1174
  };
987
1175
  //#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 };
1176
+ export { BLANK_A4_PDF, BLANK_PDF, CUSTOM_A4_PDF, DEFAULT_FONT_NAME, MM_TO_PT_RATIO, PAGE_SIZE_PRESETS, PDFME_VERSION, PT_TO_MM_RATIO, PT_TO_PX_RATIO, ZOOM, applyInternalLinkAnnotations, b64toUint8Array, checkDesignerProps, checkFont, checkGenerateProps, checkInputs, checkPreviewProps, checkTemplate, checkUIOptions, checkUIProps, cloneDeep, detectPaperSize, getB64BasePdf, getDefaultFont, getDynamicTemplate, getFallbackFontName, getInputFromTemplate, getInternalLinkTarget, isBlankPdf, isHexValid, isUrlSafeToFetch, mm2pt, normalizeInternalLinkHref, normalizeLinkHref, normalizeSafeLinkUri, pluginRegistry, pt2mm, pt2px, px2mm, registerInternalLinkAnchor, registerInternalLinkAnnotation, replacePlaceholders, resetInternalLinkAnnotations, resolvePageSize };
989
1177
 
990
1178
  //# sourceMappingURL=index.js.map