P粉5369091862023-08-17 13:45:59
In my experience, pdf generators like mPdf or DOmPDF tend to have difficulty handling relative, percentage-based values.
As long as your scaling calculations work as expected, you can directly change the svg's width and height properties:
let svg = document.getElementById('svg-number')
scaleSVG(svg, 0.05);
function scaleSVG(svg, scale=1){
let width = svg.width.baseVal.value;
let height = svg.height.baseVal.value;
/**
* alternative
let width = parseFloat(svg.getAttribute('width'));
let height = parseFloat(svg.getAttribute('height'));
*/
svg.setAttribute('width', width*scale)
svg.setAttribute('height', height*scale)
}
svg{
border: 1px solid red;
}
<svg xmlns="http://www.w3.org/2000/svg" id="svg-number" width="686.280029296875px" height="540.8900146484375px" viewBox="62.619998931884766 245 686.280029296875 540.8900146484375" >
<path id="path-svg-number" d="m277.9 252.7 0 533.2-106.9 0 0-438.7-108.4 0 0-75.5 215.3-19zm471 451.2 0 82-364 0 0-70 172.5-184.9q37.3-42.8 53.1-72.3 15.7-29.5 15.7-54.8l0 0q0-33.7-18.1-55.1-18.1-21.4-51.5-21.4l0 0q-37 0-56.2 25.1-19.2 25.1-19.2 66.8l0 0-104 0-.7-2.2q-1.9-72.1 47.4-122.1 49.2-50 132.7-50l0 0q82.4 0 129.5 42.8 47 42.9 47 114.3l0 0q0 48.3-26.5 89.2-26.6 40.8-87.7 107.1l0 0-94.9 103.6.8 1.9 224.1 0z" />
</svg>
To obtain a computable value, you can use one of the following two methods:
let width = svg.width.baseVal.value;
or
let width = parseFloat(svg.getAttribute('width'));
The first method returns a number by default and converts the percentage value to an absolute value (based on svg user units).
The latter will strip off all units and return a purely numeric value.