@polotno/pdf-export 0.1.37 → 0.1.39

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 (52) hide show
  1. package/README.md +61 -8
  2. package/lib/index.d.ts +66 -8
  3. package/lib/index.js +25 -145
  4. package/package.json +17 -18
  5. package/lib/browser-entry.d.ts +0 -7
  6. package/lib/browser-entry.js +0 -11
  7. package/lib/compare-render.d.ts +0 -1
  8. package/lib/compare-render.js +0 -185
  9. package/lib/core/index.d.ts +0 -26
  10. package/lib/core/index.js +0 -87
  11. package/lib/figure.d.ts +0 -10
  12. package/lib/figure.js +0 -54
  13. package/lib/filters.d.ts +0 -2
  14. package/lib/filters.js +0 -163
  15. package/lib/ghostscript.d.ts +0 -21
  16. package/lib/ghostscript.js +0 -132
  17. package/lib/group.d.ts +0 -5
  18. package/lib/group.js +0 -5
  19. package/lib/image.d.ts +0 -38
  20. package/lib/image.js +0 -279
  21. package/lib/line.d.ts +0 -10
  22. package/lib/line.js +0 -66
  23. package/lib/platform/adapter.d.ts +0 -37
  24. package/lib/platform/adapter.js +0 -13
  25. package/lib/platform/browser-polyfill.d.ts +0 -1
  26. package/lib/platform/browser-polyfill.js +0 -5
  27. package/lib/platform/browser.d.ts +0 -7
  28. package/lib/platform/browser.js +0 -145
  29. package/lib/platform/node.d.ts +0 -7
  30. package/lib/platform/node.js +0 -142
  31. package/lib/spot-colors.d.ts +0 -38
  32. package/lib/spot-colors.js +0 -141
  33. package/lib/svg-render.d.ts +0 -9
  34. package/lib/svg-render.js +0 -63
  35. package/lib/svg.d.ts +0 -12
  36. package/lib/svg.js +0 -224
  37. package/lib/text/fonts.d.ts +0 -16
  38. package/lib/text/fonts.js +0 -82
  39. package/lib/text/index.d.ts +0 -8
  40. package/lib/text/index.js +0 -42
  41. package/lib/text/layout.d.ts +0 -22
  42. package/lib/text/layout.js +0 -522
  43. package/lib/text/parser.d.ts +0 -46
  44. package/lib/text/parser.js +0 -415
  45. package/lib/text/render.d.ts +0 -8
  46. package/lib/text/render.js +0 -237
  47. package/lib/text/types.d.ts +0 -91
  48. package/lib/text/types.js +0 -1
  49. package/lib/text.d.ts +0 -49
  50. package/lib/text.js +0 -1277
  51. package/lib/utils.d.ts +0 -16
  52. package/lib/utils.js +0 -124
package/lib/utils.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import parseColor from 'parse-color';
2
- export declare const DPI = 75;
3
- export declare const PIXEL_RATIO = 2;
4
- export declare function fetchWithTimeout(url: string, timeout?: number, retries?: number): Promise<any>;
5
- export declare function pxToPt(px: number): number;
6
- export interface ImageCache {
7
- images: Map<string, any>;
8
- buffers: Map<string, any>;
9
- processedImages: Map<string, string>;
10
- imageFiles: Map<string, string>;
11
- tempDir: string | null;
12
- }
13
- export declare function loadImage(src: string, cache?: ImageCache | null): Promise<any>;
14
- export declare function srcToBase64(src: string, cache?: ImageCache | null): Promise<string>;
15
- export declare function srcToBuffer(src: string, cache?: ImageCache | null): Promise<Buffer>;
16
- export { parseColor };
package/lib/utils.js DELETED
@@ -1,124 +0,0 @@
1
- import parseColor from 'parse-color';
2
- import fetch from 'node-fetch';
3
- import Canvas from 'canvas';
4
- import sharp from 'sharp';
5
- export const DPI = 75;
6
- export const PIXEL_RATIO = 2;
7
- // Fetch with timeout and retry logic
8
- export async function fetchWithTimeout(url, timeout = 30000, retries = 3) {
9
- for (let attempt = 1; attempt <= retries; attempt++) {
10
- try {
11
- const controller = new AbortController();
12
- const timeoutId = setTimeout(() => controller.abort(), timeout);
13
- const response = await fetch(url, {
14
- signal: controller.signal,
15
- });
16
- clearTimeout(timeoutId);
17
- if (!response.ok) {
18
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
19
- }
20
- return response;
21
- }
22
- catch (error) {
23
- const isLastAttempt = attempt === retries;
24
- const isAbortError = error.name === 'AbortError';
25
- const isTimeoutError = error.code === 'ETIMEDOUT' || error.type === 'request-timeout';
26
- if (isLastAttempt) {
27
- throw new Error(`Failed to fetch ${url} after ${retries} attempts: ${error.message}`);
28
- }
29
- // Only retry on network/timeout errors, not on 4xx client errors
30
- if (isAbortError || isTimeoutError || error.code?.startsWith('E')) {
31
- console.warn(`Fetch attempt ${attempt}/${retries} failed for ${url}, retrying...`);
32
- // Exponential backoff: 1s, 2s, 4s, etc.
33
- await new Promise((resolve) => setTimeout(resolve, Math.pow(2, attempt - 1) * 1000));
34
- continue;
35
- }
36
- // Don't retry on other errors (e.g., 404, 403)
37
- throw error;
38
- }
39
- }
40
- throw new Error(`Failed to fetch ${url}`);
41
- }
42
- export function pxToPt(px) {
43
- return (px * DPI) / 100;
44
- }
45
- export async function loadImage(src, cache = null) {
46
- // Check cache first
47
- if (cache && cache.images.has(src)) {
48
- return cache.images.get(src);
49
- }
50
- let buffer;
51
- let mime = 'unknown';
52
- if (src.startsWith('data:')) {
53
- const matches = src.match(/^data:(.+);base64,(.*)$/);
54
- if (matches) {
55
- mime = matches[1];
56
- buffer = Buffer.from(matches[2], 'base64');
57
- }
58
- else {
59
- throw new Error('Invalid data URL');
60
- }
61
- }
62
- else {
63
- try {
64
- const response = await fetchWithTimeout(src);
65
- buffer = Buffer.from(await response.arrayBuffer());
66
- const { fileTypeFromBuffer } = await import('file-type');
67
- const typeData = await fileTypeFromBuffer(buffer);
68
- if (typeData) {
69
- mime = typeData.mime;
70
- }
71
- }
72
- catch (error) {
73
- console.log(error);
74
- throw new Error(`Failed to process image from ${src}: ${error.message}`);
75
- }
76
- }
77
- let imageBuffer = buffer;
78
- if (mime !== 'image/png' && mime !== 'image/jpeg') {
79
- imageBuffer = await sharp(buffer).toFormat('png').toBuffer();
80
- mime = 'image/png';
81
- }
82
- const image = await Canvas.loadImage(imageBuffer);
83
- // Store in cache
84
- if (cache) {
85
- cache.images.set(src, image);
86
- }
87
- return image;
88
- }
89
- export async function srcToBase64(src, cache = null) {
90
- // For base64 caching, we use a different cache key to avoid collision with buffer cache
91
- const base64CacheKey = `base64:${src}`;
92
- // Check cache first
93
- if (cache && cache.buffers.has(base64CacheKey)) {
94
- return cache.buffers.get(base64CacheKey);
95
- }
96
- let base64;
97
- if (src.indexOf('base64') >= 0) {
98
- base64 = src.split('base64,')[1];
99
- }
100
- else {
101
- const res = await fetchWithTimeout(src);
102
- const data = Buffer.from(await res.arrayBuffer());
103
- base64 = data.toString('base64');
104
- }
105
- // Store in cache
106
- if (cache) {
107
- cache.buffers.set(base64CacheKey, base64);
108
- }
109
- return base64;
110
- }
111
- export async function srcToBuffer(src, cache = null) {
112
- // Check if we have a cached Buffer for this source
113
- if (cache && cache.buffers.has(src)) {
114
- return cache.buffers.get(src);
115
- }
116
- const base64 = await srcToBase64(src, cache);
117
- const buffer = Buffer.from(base64, 'base64');
118
- // Cache the actual Buffer object so PDFKit can reuse it
119
- if (cache) {
120
- cache.buffers.set(src, buffer);
121
- }
122
- return buffer;
123
- }
124
- export { parseColor };