@campxdev/pdfme 1.2.1 → 1.2.2

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.
Files changed (32) hide show
  1. package/dist/cjs/chunks/{helper-C2o2tpNj.js → helper-BfoMn47R.js} +16955 -4
  2. package/dist/cjs/chunks/{index-dHRmLCnu.js → index-C8qZMUOU.js} +2 -2
  3. package/dist/cjs/chunks/{index-qB7eb2BC.js → index-COKtXyPp.js} +6 -2
  4. package/dist/cjs/chunks/{index-CXm3doOM.js → index-CVqJfcgy.js} +101 -36
  5. package/dist/cjs/chunks/{pluginRegistry-Ba3ANzzx.js → pluginRegistry-C8bMreez.js} +1 -1
  6. package/dist/cjs/common.js +7 -3
  7. package/dist/cjs/converter.js +1 -1
  8. package/dist/cjs/generator.js +3 -3
  9. package/dist/cjs/index.js +9 -5
  10. package/dist/cjs/print-designer-editor.js +122 -140
  11. package/dist/cjs/schemas.js +3 -3
  12. package/dist/cjs/ui.js +83020 -72511
  13. package/dist/esm/chunks/{helper-D7XF1bxK.js → helper-D5PPN6Bv.js} +16952 -5
  14. package/dist/esm/chunks/{index-pDt5vVVj.js → index-C4F7EwBG.js} +2 -2
  15. package/dist/esm/chunks/{index-qTsnfbi7.js → index-C7jr4GIK.js} +102 -37
  16. package/dist/esm/chunks/{index-D1FuD_XZ.js → index-CDhErAtE.js} +3 -3
  17. package/dist/esm/chunks/{pluginRegistry-DEA2P0ud.js → pluginRegistry-B-XSNgmK.js} +1 -1
  18. package/dist/esm/common.js +3 -3
  19. package/dist/esm/converter.js +1 -1
  20. package/dist/esm/generator.js +3 -3
  21. package/dist/esm/index.js +5 -5
  22. package/dist/esm/print-designer-editor.js +121 -142
  23. package/dist/esm/schemas.js +5 -5
  24. package/dist/esm/ui.js +83020 -72511
  25. package/dist/types/_vendors/common/constants.d.ts +1 -2
  26. package/dist/types/_vendors/common/googleFontUrls.d.ts +9 -0
  27. package/dist/types/_vendors/common/googleFonts.d.ts +7 -0
  28. package/dist/types/_vendors/common/helper.d.ts +1 -0
  29. package/dist/types/_vendors/common/index.d.ts +3 -2
  30. package/dist/types/_vendors/print-designer-editor/index.d.ts +1 -1
  31. package/dist/types/_vendors/schemas/text/fontCache.d.ts +10 -0
  32. package/package.json +1 -1
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
- var helper = require('./helper-C2o2tpNj.js');
3
+ var helper = require('./helper-BfoMn47R.js');
4
4
  var SignaturePad = require('signature_pad');
5
5
  var lucide = require('lucide');
6
- var index = require('./index-CXm3doOM.js');
6
+ var index = require('./index-CVqJfcgy.js');
7
7
  var buffer = require('buffer');
8
8
  var colors = require('./colors-BeBcxfhb.js');
9
9
  var uuid = require('uuid');
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var helper = require('./helper-C2o2tpNj.js');
4
- var pluginRegistry = require('./pluginRegistry-Ba3ANzzx.js');
3
+ var helper = require('./helper-BfoMn47R.js');
4
+ var pluginRegistry = require('./pluginRegistry-C8bMreez.js');
5
5
  var expression = require('./expression-CoTzcL7v.js');
6
6
  require('zod');
7
7
  require('buffer');
@@ -28,12 +28,16 @@ exports.checkUIOptions = helper.checkUIOptions;
28
28
  exports.checkUIProps = helper.checkUIProps;
29
29
  exports.cloneDeep = helper.cloneDeep;
30
30
  exports.getAllFonts = helper.getAllFonts;
31
+ exports.getAllFontsWithGoogle = helper.getAllFontsWithGoogle;
31
32
  exports.getB64BasePdf = helper.getB64BasePdf;
32
33
  exports.getBuiltinFontsData = helper.getBuiltinFontsData;
33
34
  exports.getDefaultFont = helper.getDefaultFont;
34
35
  exports.getFallbackFontName = helper.getFallbackFontName;
36
+ exports.getGoogleFontsData = helper.getGoogleFontsData;
35
37
  exports.getInputFromTemplate = helper.getInputFromTemplate;
38
+ exports.googleFonts = helper.googleFonts;
36
39
  exports.isBlankPdf = helper.isBlankPdf;
40
+ exports.isGoogleFont = helper.isGoogleFont;
37
41
  exports.isHexValid = helper.isHexValid;
38
42
  exports.mm2pt = helper.mm2pt;
39
43
  exports.pt2mm = helper.pt2mm;
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var helper = require('./helper-C2o2tpNj.js');
3
+ var helper = require('./helper-BfoMn47R.js');
4
4
  var fontkit = require('fontkit');
5
5
  var buffer = require('buffer');
6
6
  var colors = require('./colors-BeBcxfhb.js');
@@ -121,6 +121,36 @@ const LINE_END_FORBIDDEN_CHARS = [
121
121
  '«',
122
122
  ];
123
123
 
124
+ /**
125
+ * Global font binary cache for URL-based fonts.
126
+ * Fonts are fetched on demand and cached so subsequent uses are instant.
127
+ */
128
+ const _binaryCache = new Map();
129
+ const _inflight = new Map();
130
+ /**
131
+ * Fetch font binary data for a URL, using the global cache.
132
+ * Returns cached ArrayBuffer if available, otherwise fetches and caches.
133
+ * Deduplicates concurrent requests for the same URL.
134
+ */
135
+ const fetchFontBinary = async (url) => {
136
+ const cached = _binaryCache.get(url);
137
+ if (cached)
138
+ return cached;
139
+ // Deduplicate: if a fetch for this URL is already in flight, reuse it
140
+ const inflight = _inflight.get(url);
141
+ if (inflight)
142
+ return inflight;
143
+ const promise = fetch(url)
144
+ .then((res) => res.arrayBuffer())
145
+ .then((buf) => {
146
+ _binaryCache.set(url, buf);
147
+ _inflight.delete(url);
148
+ return buf;
149
+ });
150
+ _inflight.set(url, promise);
151
+ return promise;
152
+ };
153
+
124
154
  const getBrowserVerticalFontAdjustments = (fontKitFont, fontSize, lineHeight, verticalAlignment) => {
125
155
  const { ascent, descent, unitsPerEm } = fontKitFont;
126
156
  // Fonts have a designed line height that the browser renders when using `line-height: normal`
@@ -189,7 +219,7 @@ const getFontKitFont = async (fontName, font, _cache) => {
189
219
  if (typeof fontData === 'string') {
190
220
  const isUrl = fontData.startsWith('http') || fontData.startsWith('/') || fontData.startsWith('./');
191
221
  fontData = isUrl
192
- ? await fetch(fontData).then((res) => res.arrayBuffer())
222
+ ? await fetchFontBinary(fontData)
193
223
  : helper.b64toUint8Array(fontData);
194
224
  }
195
225
  // Convert fontData to Buffer if it's not already a Buffer
@@ -1615,26 +1645,34 @@ const createSvgStr = (icon, attrs) => {
1615
1645
  return `<svg ${svgAttrString}>${elementsString}</svg>`;
1616
1646
  };
1617
1647
 
1618
- const embedAndGetFontObj = async (arg) => {
1619
- const { pdfDoc, font, _cache } = arg;
1620
- if (_cache.has(pdfDoc)) {
1621
- return _cache.get(pdfDoc);
1622
- }
1623
- const fontValues = await Promise.all(Object.values(font).map(async (v) => {
1624
- let fontData = v.data;
1625
- if (typeof fontData === 'string') {
1626
- const isUrl = fontData.startsWith('http') || fontData.startsWith('/') || fontData.startsWith('./');
1627
- fontData = isUrl
1628
- ? await fetch(fontData).then((res) => res.arrayBuffer())
1629
- : helper.b64toUint8Array(fontData);
1630
- }
1631
- return pdfDoc.embedFont(fontData, {
1632
- subset: typeof v.subset === 'undefined' ? true : v.subset,
1633
- });
1634
- }));
1635
- const fontObj = Object.keys(font).reduce((acc, cur, i) => Object.assign(acc, { [cur]: fontValues[i] }), {});
1636
- _cache.set(pdfDoc, fontObj);
1637
- return fontObj;
1648
+ const embedPdfFont = async (pdfDoc, fontName, font, _cache) => {
1649
+ const cacheKey = `embedPdfFont-${fontName}`;
1650
+ if (_cache.has(cacheKey))
1651
+ return _cache.get(cacheKey);
1652
+ const fontEntry = font[fontName];
1653
+ if (!fontEntry)
1654
+ throw new Error(`Font "${fontName}" not found`);
1655
+ let fontData = fontEntry.data;
1656
+ if (typeof fontData === 'string') {
1657
+ const isUrl = fontData.startsWith('http') || fontData.startsWith('/') || fontData.startsWith('./');
1658
+ fontData = isUrl ? await fetchFontBinary(fontData) : helper.b64toUint8Array(fontData);
1659
+ }
1660
+ const useSubset = typeof fontEntry.subset === 'undefined' ? true : fontEntry.subset;
1661
+ let pdfFont;
1662
+ try {
1663
+ pdfFont = await pdfDoc.embedFont(fontData, { subset: useSubset });
1664
+ }
1665
+ catch (err) {
1666
+ // CFF/OpenType fonts fail subsetting (glyph._decode is not a function) — retry without subset
1667
+ if (useSubset) {
1668
+ pdfFont = await pdfDoc.embedFont(fontData, { subset: false });
1669
+ }
1670
+ else {
1671
+ throw err;
1672
+ }
1673
+ }
1674
+ _cache.set(cacheKey, pdfFont);
1675
+ return pdfFont;
1638
1676
  };
1639
1677
  const getFontProp = ({ value, fontKitFont, schema, colorType, }) => {
1640
1678
  const fontSize = schema.dynamicFontSize
@@ -1656,18 +1694,12 @@ const pdfRender$1 = async (arg) => {
1656
1694
  return;
1657
1695
  const { font = helper.getDefaultFont(), colorType } = options;
1658
1696
  const resolvedFontName = resolveFontName(schema.fontName, schema.bold, schema.italic, font);
1659
- const [pdfFontObj, fontKitFont] = await Promise.all([
1660
- embedAndGetFontObj({
1661
- pdfDoc,
1662
- font,
1663
- _cache: _cache,
1664
- }),
1697
+ const [pdfFontValue, fontKitFont] = await Promise.all([
1698
+ embedPdfFont(pdfDoc, resolvedFontName, font, _cache),
1665
1699
  getFontKitFont(resolvedFontName, font, _cache),
1666
1700
  ]);
1667
1701
  const fontProp = getFontProp({ value, fontKitFont, schema, colorType });
1668
1702
  const { fontSize, color, alignment, verticalAlignment, lineHeight, characterSpacing } = fontProp;
1669
- const fontName = resolvedFontName;
1670
- const pdfFontValue = pdfFontObj && pdfFontObj[fontName];
1671
1703
  const pageHeight = page.getHeight();
1672
1704
  const { width, height, rotate, position: { x, y }, opacity, } = convertForPdfLayoutProps({ schema, pageHeight, applyRotateTranslate: false });
1673
1705
  const pivotPoint = { x: x + width / 2, y: pageHeight - helper.mm2pt(schema.position.y) - height / 2 };
@@ -1846,8 +1878,8 @@ const pdfRender = async (arg) => {
1846
1878
  await pdfRender$1(renderArgs);
1847
1879
  };
1848
1880
 
1849
- const TextBoldIcon = createSvgStr(lucide.Bold);
1850
- const TextItalicIcon = createSvgStr(lucide.Italic);
1881
+ createSvgStr(lucide.Bold);
1882
+ createSvgStr(lucide.Italic);
1851
1883
  const TextStrikethroughIcon = createSvgStr(lucide.Strikethrough);
1852
1884
  const TextUnderlineIcon = createSvgStr(lucide.Underline);
1853
1885
  const TextAlignLeftIcon = createSvgStr(lucide.AlignLeft);
@@ -1871,8 +1903,7 @@ exports.Formatter = void 0;
1871
1903
  })(exports.Formatter || (exports.Formatter = {}));
1872
1904
  function getExtraFormatterSchema(i18n) {
1873
1905
  const buttons = [
1874
- { key: exports.Formatter.BOLD, icon: TextBoldIcon, type: 'boolean' },
1875
- { key: exports.Formatter.ITALIC, icon: TextItalicIcon, type: 'boolean' },
1906
+ // TODO: re-enable bold/italic controls when ready
1876
1907
  { key: exports.Formatter.STRIKETHROUGH, icon: TextStrikethroughIcon, type: 'boolean' },
1877
1908
  { key: exports.Formatter.UNDERLINE, icon: TextUnderlineIcon, type: 'boolean' },
1878
1909
  { key: exports.Formatter.ALIGNMENT, icon: TextAlignLeftIcon, type: 'select', value: DEFAULT_ALIGNMENT },
@@ -1943,8 +1974,16 @@ const propPanel$1 = {
1943
1974
  widget: 'select',
1944
1975
  default: fallbackFontName,
1945
1976
  placeholder: fallbackFontName,
1946
- props: { options: fontNames.map((name) => ({ label: name, value: name })) },
1947
- span: 12,
1977
+ props: {
1978
+ options: fontNames.map((name) => ({ label: name, value: name })),
1979
+ showSearch: true,
1980
+ virtual: true,
1981
+ filterOption: (input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
1982
+ popupMatchSelectWidth: false,
1983
+ listHeight: 300,
1984
+ style: { width: '100%' },
1985
+ },
1986
+ span: 24,
1948
1987
  },
1949
1988
  fontSize: {
1950
1989
  title: i18n('schemas.text.size'),
@@ -2307,6 +2346,16 @@ const updateVariablesFromText = (text, variables) => {
2307
2346
  return changed;
2308
2347
  };
2309
2348
 
2349
+ const _loadedGoogleFonts = new Set();
2350
+ const ensureGoogleFontLoaded = (fontName, fontData) => {
2351
+ if (!fontName || _loadedGoogleFonts.has(fontName) || !helper.isGoogleFont(fontName))
2352
+ return;
2353
+ if (!document?.fonts)
2354
+ return;
2355
+ _loadedGoogleFonts.add(fontName);
2356
+ const fontFace = new FontFace(fontName, typeof fontData === 'string' ? `url(${fontData})` : fontData, { display: 'swap' });
2357
+ fontFace.load().then(() => document.fonts.add(fontFace)).catch(() => { });
2358
+ };
2310
2359
  const replaceUnsupportedChars = (text, fontKitFont) => {
2311
2360
  const charSupportCache = {};
2312
2361
  const isCharSupported = (char) => {
@@ -2348,8 +2397,24 @@ const uiRender$1 = async (arg) => {
2348
2397
  };
2349
2398
  const font = options?.font || helper.getDefaultFont();
2350
2399
  const resolvedFontName = resolveFontName(schema.fontName, schema.bold, schema.italic, font);
2400
+ // Lazily load Google Font for CSS rendering if needed
2401
+ if (resolvedFontName && font[resolvedFontName]) {
2402
+ ensureGoogleFontLoaded(resolvedFontName, font[resolvedFontName].data);
2403
+ }
2404
+ // Show a subtle loading state while the font binary is being fetched
2405
+ const isLoading = helper.isGoogleFont(resolvedFontName);
2406
+ if (isLoading) {
2407
+ arg.rootElement.style.opacity = '0.4';
2408
+ arg.rootElement.style.transition = 'opacity 0.15s ease-out';
2409
+ }
2351
2410
  const fontKitFont = await getFontKitFont(resolvedFontName, font, _cache);
2352
2411
  const textBlock = buildStyledTextContainer(arg, fontKitFont, usePlaceholder ? placeholder : value);
2412
+ // Fade in once the font is ready
2413
+ if (isLoading) {
2414
+ // Force a reflow so the transition triggers from the dimmed state
2415
+ void arg.rootElement.offsetHeight;
2416
+ arg.rootElement.style.opacity = '1';
2417
+ }
2353
2418
  const processedText = replaceUnsupportedChars(value, fontKitFont);
2354
2419
  if (!isEditable(mode, schema)) {
2355
2420
  // Read-only mode
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var helper = require('./helper-C2o2tpNj.js');
3
+ var helper = require('./helper-BfoMn47R.js');
4
4
 
5
5
  /** Floating point tolerance for comparisons */
6
6
  const EPSILON = 0.01;
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var helper = require('./chunks/helper-C2o2tpNj.js');
4
- var common = require('./chunks/index-qB7eb2BC.js');
5
- var pluginRegistry = require('./chunks/pluginRegistry-Ba3ANzzx.js');
3
+ var helper = require('./chunks/helper-BfoMn47R.js');
4
+ var common = require('./chunks/index-COKtXyPp.js');
5
+ var pluginRegistry = require('./chunks/pluginRegistry-C8bMreez.js');
6
6
  var expression = require('./chunks/expression-CoTzcL7v.js');
7
7
  require('zod');
8
8
  require('buffer');
@@ -29,12 +29,16 @@ exports.checkUIOptions = helper.checkUIOptions;
29
29
  exports.checkUIProps = helper.checkUIProps;
30
30
  exports.cloneDeep = helper.cloneDeep;
31
31
  exports.getAllFonts = helper.getAllFonts;
32
+ exports.getAllFontsWithGoogle = helper.getAllFontsWithGoogle;
32
33
  exports.getB64BasePdf = helper.getB64BasePdf;
33
34
  exports.getBuiltinFontsData = helper.getBuiltinFontsData;
34
35
  exports.getDefaultFont = helper.getDefaultFont;
35
36
  exports.getFallbackFontName = helper.getFallbackFontName;
37
+ exports.getGoogleFontsData = helper.getGoogleFontsData;
36
38
  exports.getInputFromTemplate = helper.getInputFromTemplate;
39
+ exports.googleFonts = helper.googleFonts;
37
40
  exports.isBlankPdf = helper.isBlankPdf;
41
+ exports.isGoogleFont = helper.isGoogleFont;
38
42
  exports.isHexValid = helper.isHexValid;
39
43
  exports.mm2pt = helper.mm2pt;
40
44
  exports.pt2mm = helper.pt2mm;
@@ -2,7 +2,7 @@
2
2
 
3
3
  var pdfjsLib = require('pdfjs-dist');
4
4
  var PDFJSWorker = require('pdfjs-dist/build/pdf.worker.entry.js');
5
- var helper = require('./chunks/helper-C2o2tpNj.js');
5
+ var helper = require('./chunks/helper-BfoMn47R.js');
6
6
  var PDFButton = require('./chunks/PDFButton-QFYEzldP.js');
7
7
  require('zod');
8
8
  require('buffer');
@@ -2,11 +2,11 @@
2
2
 
3
3
  var PDFButton = require('./chunks/PDFButton-QFYEzldP.js');
4
4
  var colors = require('./chunks/colors-BeBcxfhb.js');
5
- var helper = require('./chunks/helper-C2o2tpNj.js');
6
- var pluginRegistry = require('./chunks/pluginRegistry-Ba3ANzzx.js');
5
+ var helper = require('./chunks/helper-BfoMn47R.js');
6
+ var pluginRegistry = require('./chunks/pluginRegistry-C8bMreez.js');
7
7
  var expression = require('./chunks/expression-CoTzcL7v.js');
8
8
  var fontkit = require('fontkit');
9
- var index = require('./chunks/index-CXm3doOM.js');
9
+ var index = require('./chunks/index-CVqJfcgy.js');
10
10
  require('pako');
11
11
  require('@pdf-lib/standard-fonts');
12
12
  require('@pdf-lib/upng');
package/dist/cjs/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
3
  var ui = require('./ui.js');
4
- var index = require('./chunks/index-CXm3doOM.js');
4
+ var index = require('./chunks/index-CVqJfcgy.js');
5
5
  var converter = require('./converter.js');
6
6
  var manipulator = require('./manipulator.js');
7
7
  var printDesignerEditor = require('./print-designer-editor.js');
8
8
  var schemas = require('./schemas.js');
9
- var index$1 = require('./chunks/index-dHRmLCnu.js');
9
+ var index$1 = require('./chunks/index-C8qZMUOU.js');
10
10
  var generator = require('./generator.js');
11
- var helper = require('./chunks/helper-C2o2tpNj.js');
12
- var common = require('./chunks/index-qB7eb2BC.js');
13
- var pluginRegistry = require('./chunks/pluginRegistry-Ba3ANzzx.js');
11
+ var helper = require('./chunks/helper-BfoMn47R.js');
12
+ var common = require('./chunks/index-COKtXyPp.js');
13
+ var pluginRegistry = require('./chunks/pluginRegistry-C8bMreez.js');
14
14
  var expression = require('./chunks/expression-CoTzcL7v.js');
15
15
  require('fontkit');
16
16
  require('buffer');
@@ -133,12 +133,16 @@ exports.checkUIOptions = helper.checkUIOptions;
133
133
  exports.checkUIProps = helper.checkUIProps;
134
134
  exports.cloneDeep = helper.cloneDeep;
135
135
  exports.getAllFonts = helper.getAllFonts;
136
+ exports.getAllFontsWithGoogle = helper.getAllFontsWithGoogle;
136
137
  exports.getB64BasePdf = helper.getB64BasePdf;
137
138
  exports.getBuiltinFontsData = helper.getBuiltinFontsData;
138
139
  exports.getDefaultFont = helper.getDefaultFont;
139
140
  exports.getFallbackFontName = helper.getFallbackFontName;
141
+ exports.getGoogleFontsData = helper.getGoogleFontsData;
140
142
  exports.getInputFromTemplate = helper.getInputFromTemplate;
143
+ exports.googleFonts = helper.googleFonts;
141
144
  exports.isBlankPdf = helper.isBlankPdf;
145
+ exports.isGoogleFont = helper.isGoogleFont;
142
146
  exports.isHexValid = helper.isHexValid;
143
147
  exports.mm2pt = helper.mm2pt;
144
148
  exports.pt2mm = helper.pt2mm;