@pixldocs/canvas-renderer 0.3.20 → 0.3.22
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/index.cjs +41 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +12 -0
- package/dist/index.js +41 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -201,12 +201,24 @@ export declare class PixldocsRenderer {
|
|
|
201
201
|
* Convenience: fetch by ID with simple flat data and render.
|
|
202
202
|
*/
|
|
203
203
|
renderById(templateId: string, formData?: Record<string, any>, options?: RenderOptions): Promise<RenderResult>;
|
|
204
|
+
/**
|
|
205
|
+
* Convenience: fetch by ID with flat data and render ALL pages.
|
|
206
|
+
*/
|
|
207
|
+
renderAllById(templateId: string, formData?: Record<string, any>, options?: Omit<RenderOptions, 'pageIndex'>): Promise<RenderResult[]>;
|
|
204
208
|
private getExpectedImageCount;
|
|
205
209
|
private waitForCanvasImages;
|
|
206
210
|
private getNormalizedGradientStops;
|
|
207
211
|
private paintPageBackground;
|
|
208
212
|
private renderPageViaPreviewCanvas;
|
|
209
213
|
private captureSvgViaPreviewCanvas;
|
|
214
|
+
/**
|
|
215
|
+
* Normalize the SVG's width/height/viewBox to match the logical page dimensions.
|
|
216
|
+
* Fabric's toSVG() may output dimensions scaled by the canvas element's actual
|
|
217
|
+
* pixel size (e.g., 2x due to devicePixelRatio), causing svg2pdf to render
|
|
218
|
+
* content at the wrong scale. This rewrites the root <svg> attributes to ensure
|
|
219
|
+
* the SVG coordinate system matches the intended page size exactly.
|
|
220
|
+
*/
|
|
221
|
+
private normalizeSvgDimensions;
|
|
210
222
|
/**
|
|
211
223
|
* Find the Fabric.Canvas instance that belongs to a given container element,
|
|
212
224
|
* using the global __fabricCanvasRegistry (set by PageCanvas).
|
package/dist/index.js
CHANGED
|
@@ -10758,6 +10758,18 @@ class PixldocsRenderer {
|
|
|
10758
10758
|
});
|
|
10759
10759
|
return this.render(resolved.config, options);
|
|
10760
10760
|
}
|
|
10761
|
+
/**
|
|
10762
|
+
* Convenience: fetch by ID with flat data and render ALL pages.
|
|
10763
|
+
*/
|
|
10764
|
+
async renderAllById(templateId, formData, options) {
|
|
10765
|
+
const resolved = await resolveTemplateData({
|
|
10766
|
+
templateId,
|
|
10767
|
+
formData,
|
|
10768
|
+
supabaseUrl: this.config.supabaseUrl,
|
|
10769
|
+
supabaseAnonKey: this.config.supabaseAnonKey
|
|
10770
|
+
});
|
|
10771
|
+
return this.renderAllPages(resolved.config, options);
|
|
10772
|
+
}
|
|
10761
10773
|
// ─── Internal: render a page using the full PreviewCanvas engine ───
|
|
10762
10774
|
getExpectedImageCount(config, pageIndex) {
|
|
10763
10775
|
const page = config.pages[pageIndex];
|
|
@@ -11063,7 +11075,8 @@ class PixldocsRenderer {
|
|
|
11063
11075
|
const prevSvgVPT = fabricInstance.svgViewportTransformation;
|
|
11064
11076
|
fabricInstance.viewportTransform = [1, 0, 0, 1, 0, 0];
|
|
11065
11077
|
fabricInstance.svgViewportTransformation = false;
|
|
11066
|
-
|
|
11078
|
+
let svgString = fabricInstance.toSVG();
|
|
11079
|
+
svgString = this.normalizeSvgDimensions(svgString, canvasWidth, canvasHeight);
|
|
11067
11080
|
if (prevVPT) fabricInstance.viewportTransform = prevVPT;
|
|
11068
11081
|
fabricInstance.svgViewportTransformation = prevSvgVPT;
|
|
11069
11082
|
const page = config.pages[pageIndex];
|
|
@@ -11096,6 +11109,33 @@ class PixldocsRenderer {
|
|
|
11096
11109
|
);
|
|
11097
11110
|
});
|
|
11098
11111
|
}
|
|
11112
|
+
/**
|
|
11113
|
+
* Normalize the SVG's width/height/viewBox to match the logical page dimensions.
|
|
11114
|
+
* Fabric's toSVG() may output dimensions scaled by the canvas element's actual
|
|
11115
|
+
* pixel size (e.g., 2x due to devicePixelRatio), causing svg2pdf to render
|
|
11116
|
+
* content at the wrong scale. This rewrites the root <svg> attributes to ensure
|
|
11117
|
+
* the SVG coordinate system matches the intended page size exactly.
|
|
11118
|
+
*/
|
|
11119
|
+
normalizeSvgDimensions(svg, targetWidth, targetHeight) {
|
|
11120
|
+
const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
|
|
11121
|
+
const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
|
|
11122
|
+
const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
|
|
11123
|
+
const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
|
|
11124
|
+
if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
|
|
11125
|
+
return svg;
|
|
11126
|
+
}
|
|
11127
|
+
console.log(
|
|
11128
|
+
`[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
|
|
11129
|
+
);
|
|
11130
|
+
let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
|
|
11131
|
+
const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
|
|
11132
|
+
if (/viewBox="[^"]*"/.test(normalized)) {
|
|
11133
|
+
normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
|
|
11134
|
+
} else {
|
|
11135
|
+
normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
|
|
11136
|
+
}
|
|
11137
|
+
return normalized;
|
|
11138
|
+
}
|
|
11099
11139
|
/**
|
|
11100
11140
|
* Find the Fabric.Canvas instance that belongs to a given container element,
|
|
11101
11141
|
* using the global __fabricCanvasRegistry (set by PageCanvas).
|