@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.cjs CHANGED
@@ -11092,9 +11092,19 @@ class PixldocsRenderer {
11092
11092
  }
11093
11093
  const prevVPT = fabricInstance.viewportTransform ? [...fabricInstance.viewportTransform] : void 0;
11094
11094
  const prevSvgVPT = fabricInstance.svgViewportTransformation;
11095
+ const prevCanvasWidth = fabricInstance.width;
11096
+ const prevCanvasHeight = fabricInstance.height;
11097
+ fabricInstance.setDimensions(
11098
+ { width: canvasWidth, height: canvasHeight },
11099
+ { cssOnly: false, backstoreOnly: false }
11100
+ );
11095
11101
  fabricInstance.viewportTransform = [1, 0, 0, 1, 0, 0];
11096
11102
  fabricInstance.svgViewportTransformation = false;
11097
11103
  const svgString = fabricInstance.toSVG();
11104
+ fabricInstance.setDimensions(
11105
+ { width: prevCanvasWidth, height: prevCanvasHeight },
11106
+ { cssOnly: false, backstoreOnly: false }
11107
+ );
11098
11108
  if (prevVPT) fabricInstance.viewportTransform = prevVPT;
11099
11109
  fabricInstance.svgViewportTransformation = prevSvgVPT;
11100
11110
  const page = config.pages[pageIndex];
@@ -11127,6 +11137,33 @@ class PixldocsRenderer {
11127
11137
  );
11128
11138
  });
11129
11139
  }
11140
+ /**
11141
+ * Normalize the SVG's width/height/viewBox to match the logical page dimensions.
11142
+ * Fabric's toSVG() may output dimensions scaled by the canvas element's actual
11143
+ * pixel size (e.g., 2x due to devicePixelRatio), causing svg2pdf to render
11144
+ * content at the wrong scale. This rewrites the root <svg> attributes to ensure
11145
+ * the SVG coordinate system matches the intended page size exactly.
11146
+ */
11147
+ normalizeSvgDimensions(svg, targetWidth, targetHeight) {
11148
+ const widthMatch = svg.match(/<svg[^>]*\bwidth="([^"]+)"/);
11149
+ const heightMatch = svg.match(/<svg[^>]*\bheight="([^"]+)"/);
11150
+ const svgWidth = widthMatch ? parseFloat(widthMatch[1]) : targetWidth;
11151
+ const svgHeight = heightMatch ? parseFloat(heightMatch[1]) : targetHeight;
11152
+ if (Math.abs(svgWidth - targetWidth) < 1 && Math.abs(svgHeight - targetHeight) < 1) {
11153
+ return svg;
11154
+ }
11155
+ console.log(
11156
+ `[canvas-renderer][svg-normalize] SVG dimensions ${svgWidth}x${svgHeight} → ${targetWidth}x${targetHeight}`
11157
+ );
11158
+ let normalized = svg.replace(/(<svg[^>]*\b)width="[^"]*"/, `$1width="${targetWidth}"`).replace(/(<svg[^>]*\b)height="[^"]*"/, `$1height="${targetHeight}"`);
11159
+ const viewBox = `0 0 ${svgWidth} ${svgHeight}`;
11160
+ if (/viewBox="[^"]*"/.test(normalized)) {
11161
+ normalized = normalized.replace(/viewBox="[^"]*"/, `viewBox="${viewBox}"`);
11162
+ } else {
11163
+ normalized = normalized.replace(/<svg\b/, `<svg viewBox="${viewBox}"`);
11164
+ }
11165
+ return normalized;
11166
+ }
11130
11167
  /**
11131
11168
  * Find the Fabric.Canvas instance that belongs to a given container element,
11132
11169
  * using the global __fabricCanvasRegistry (set by PageCanvas).