@pixldocs/canvas-renderer 0.3.21 → 0.3.23

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
@@ -11073,9 +11073,19 @@ class PixldocsRenderer {
11073
11073
  }
11074
11074
  const prevVPT = fabricInstance.viewportTransform ? [...fabricInstance.viewportTransform] : void 0;
11075
11075
  const prevSvgVPT = fabricInstance.svgViewportTransformation;
11076
+ const prevCanvasWidth = fabricInstance.width;
11077
+ const prevCanvasHeight = fabricInstance.height;
11078
+ fabricInstance.setDimensions(
11079
+ { width: canvasWidth, height: canvasHeight },
11080
+ { cssOnly: false, backstoreOnly: false }
11081
+ );
11076
11082
  fabricInstance.viewportTransform = [1, 0, 0, 1, 0, 0];
11077
11083
  fabricInstance.svgViewportTransformation = false;
11078
11084
  const svgString = fabricInstance.toSVG();
11085
+ fabricInstance.setDimensions(
11086
+ { width: prevCanvasWidth, height: prevCanvasHeight },
11087
+ { cssOnly: false, backstoreOnly: false }
11088
+ );
11079
11089
  if (prevVPT) fabricInstance.viewportTransform = prevVPT;
11080
11090
  fabricInstance.svgViewportTransformation = prevSvgVPT;
11081
11091
  const page = config.pages[pageIndex];
@@ -11108,6 +11118,33 @@ class PixldocsRenderer {
11108
11118
  );
11109
11119
  });
11110
11120
  }
11121
+ /**
11122
+ * Normalize the SVG's width/height/viewBox to match the logical page dimensions.
11123
+ * Fabric's toSVG() may output dimensions scaled by the canvas element's actual
11124
+ * pixel size (e.g., 2x due to devicePixelRatio), causing svg2pdf to render
11125
+ * content at the wrong scale. This rewrites the root <svg> attributes to ensure
11126
+ * the SVG coordinate system matches the intended page size exactly.
11127
+ */
11128
+ normalizeSvgDimensions(svg, targetWidth, targetHeight) {
11129
+ const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
11130
+ const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
11131
+ const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
11132
+ const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
11133
+ if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
11134
+ return svg;
11135
+ }
11136
+ console.log(
11137
+ `[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
11138
+ );
11139
+ let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
11140
+ const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
11141
+ if (/viewBox="[^"]*"/.test(normalized)) {
11142
+ normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
11143
+ } else {
11144
+ normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
11145
+ }
11146
+ return normalized;
11147
+ }
11111
11148
  /**
11112
11149
  * Find the Fabric.Canvas instance that belongs to a given container element,
11113
11150
  * using the global __fabricCanvasRegistry (set by PageCanvas).