yWorks/svg2pdf.js

Empty path def crashes jspdf

edemaine opened this issue · 1 comments

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)?

  • jspdf@2.5.1
  • svg2pdf.js@2.2.1

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):

  • OS: Windows 11
  • Browser: Chrome
  • Version: 119

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.

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:

protected getBoundingBoxCore(context: Context): Rect {
const path = this.getCachedPath(context)
if (!path) {
return [0, 0, 0, 0]
}
let minX = Number.POSITIVE_INFINITY

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