yWorks / svg2pdf.js

A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
MIT License
643 stars 96 forks source link

Empty path def crashes jspdf #278

Closed edemaine closed 6 months ago

edemaine commented 7 months ago

Describe the bug It seems that a <path> with no d argument, inside a <defs>, crashes when <use>d.

What version are you using (exact version of svg2pdf.js and jspdf)?

To Reproduce Minimal SVG input:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 150">
  <defs>
    <path id="MJX-34-TEX-N-2061" d=""/>
  </defs>
  <use href="#MJX-34-TEX-N-2061" />
</svg>

Playground link

Expected behavior I would have expected a blank PDF. Chrome has no issues rendering this.

Actual behavior Exception: Error in function M.y.hpf.y.__private__.hpf (https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js:86:5369): Invalid argument passed to jsPDF.hpf

Desktop (please complete the following information):

Additional context This SVG is admittedly a little silly, but it's automatically generated from MathJax's SVG output. I could filter out dummy symbols like this, but it would be nicer if jspdf didn't crash in this case.

HackbrettXXX commented 7 months ago

Thank you for the bug report. I could reproduce it.

I assume the issue is that the bounding box calculated for the use is infinite for the empty path, which it shouldn't. To fix it, I think we need to extend this if to also check for empty paths, like already done in renderCore:

https://github.com/yWorks/svg2pdf.js/blob/53061ff60c1c9e804d92afb839ad43c3b9899de3/src/nodes/geometrynode.ts#L81-L86

Could you prepare a PR? I will happily merge it.