@pixldocs/canvas-renderer 0.3.21 → 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.d.ts CHANGED
@@ -211,6 +211,14 @@ export declare class PixldocsRenderer {
211
211
  private paintPageBackground;
212
212
  private renderPageViaPreviewCanvas;
213
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;
214
222
  /**
215
223
  * Find the Fabric.Canvas instance that belongs to a given container element,
216
224
  * using the global __fabricCanvasRegistry (set by PageCanvas).
package/dist/index.js CHANGED
@@ -11075,7 +11075,8 @@ class PixldocsRenderer {
11075
11075
  const prevSvgVPT = fabricInstance.svgViewportTransformation;
11076
11076
  fabricInstance.viewportTransform = [1, 0, 0, 1, 0, 0];
11077
11077
  fabricInstance.svgViewportTransformation = false;
11078
- const svgString = fabricInstance.toSVG();
11078
+ let svgString = fabricInstance.toSVG();
11079
+ svgString = this.normalizeSvgDimensions(svgString, canvasWidth, canvasHeight);
11079
11080
  if (prevVPT) fabricInstance.viewportTransform = prevVPT;
11080
11081
  fabricInstance.svgViewportTransformation = prevSvgVPT;
11081
11082
  const page = config.pages[pageIndex];
@@ -11108,6 +11109,33 @@ class PixldocsRenderer {
11108
11109
  );
11109
11110
  });
11110
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
+ }
11111
11139
  /**
11112
11140
  * Find the Fabric.Canvas instance that belongs to a given container element,
11113
11141
  * using the global __fabricCanvasRegistry (set by PageCanvas).