yWorks/svg2pdf.js

Having issue in converting SVG to PDF --invalid argument to jsPDF.scale

im-hamza-dev opened this issue · 6 comments

trying to add svg to pdf using jsPDF and svg2pdf but somehow got this exception:
image

let canvasWrapper = document.getElementById("svg-wrapper");
canvasWrapper.innerHTML = jsonFile;

  canvasWrapper = canvasWrapper.firstChild;
  // canvasWrapper.getBoundingClientRect();
  const width = canvasWrapper.width?.baseVal?.value ?? 1200;
  const height = canvasWrapper.height?.baseVal?.value ?? 1000;

  console.log(canvasWrapper, canvasWrapper.width, canvasWrapper.height);
  // Generate PDF
  const pdf = new jsPDF(width > height ? "l" : "p", "pt", "a0");

  const svgOptions = { x: 0, y: 0, width, height };
  pdf.svg(canvasWrapper, svgOptions).then(() => {
    pdf.save("sv3432g.pdf");
  });

Need some suggestions on urgent basis!
Thanks

Your code looks good, but the bug is probably caused by your SVG. Please provide a small SVG that reproduces the issue. Optimally, the issue can be reproduced in the online playground.

That file is huge. Please try to reduce it to something manageable, optimally less than 10-20 tags/elements.

yGuy commented

I don't see that "none" is a valid value for the stroke-width attribute. Can you point me to the spec that tells what "none" should be? I guess you should simply be using "0" instead if you don't want a stroke or set the stroke itself to none?

https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-width

I have no glue why it's there. In my case it was generated by http://paperjs.org/reference/project/#exportsvg.
But "none" seems to be interpreted as "1", probably because it's not valid, like you said, and so it's ignored: https://jsfiddle.net/grynvkbu/

yGuy commented

We are not going to implement a lenient parser that will try to make the best out of invalid or broken svg. Please fix the SVG or the tool that created the broken SVG.