SVG for print with scale

旧街凉风 提交于 2019-12-02 10:39:06

You can't rely on the fact that something that is X pixels on screen is going to be X pixels when printed. You need to print some specimens and measure the printed SVG to calculate the scale. And it will differ between browsers and printers.

First set your SVG to be rendered at 1:1 on screen. To do that, set your width="496" and height="388" to match the viewBox.

Print that and measure a known dimension. Such as one wall.

So let's say your 2m wall on your plan is 53mm when printed. Then your real length to printed length scale factor is

2m = 53mm printed
worldToPrinterScaleFactor = 53 / 2000

To print a 1:1 scale copy of your room, you would need to scale (multiply) your SVG by 1 / worldToPrinterScaleFactor.

To print a half-scale copy of your room, you would need to scale (multiply) your SVG by 0.5 / worldToPrinterScaleFactor.

But if you wanted to print at 1/50...

printScale = 1 / 50

then you would need to scale your SVG by:

printScale / worldToPrinterScaleFactor

So your JS should be something like:

var printScale = 1 / 50;

var worldToPrinterScaleFactor = 53 / 2000;
var svgScale = printScale / worldToPrinterScaleFactor;

var svgNode = document.getElementById("plan");
var viewBox = svgNode.viewBox.baseVal;

svgNode.width.baseVal.value = svgScale * viewBox.width;
svgNode.height.baseVal.value = svgScale * viewBox.height;

You should set your width and height parameters in print size, like millimeters for example; and then also you should define viewbox with exactly the same width, height parameters to get exact size on the print output.

In your case (with roughly 1/50 conversion from pixels to cms):

<svg xmlns:cc="http://creativecommons.org/ns#" xmlns:svg="http://www.w3.org/2000/svg"
     xmlns="http://www.w3.org/2000/svg" version="1.1" width="100mm"   height="80mm"
     preserveAspectRatio="xMinYMin slice" id="plan" xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:svgjs="http://svgjs.com/svgjs" viewBox="0 0 100 80">
     .....
</svg>

This will provide desired printed output in scale; provided that the printer is set up for 100% scale (and not for "Fit To Page") with correct paper size.

Scaling can be added as an additional coefficient for different requirements as explained by Paul.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!