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
667 stars 104 forks source link

Cannot read property 'toLowerCase' of undefined #21

Closed psamim closed 7 years ago

psamim commented 7 years ago

Hi,

I get the following error:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
    at l (bundle.js:493)
    at k (bundle.js:493)
    at U (bundle.js:493)
    at Q (bundle.js:493)
    at Object.<anonymous> (bundle.js:58)
    at __webpack_require__ (bundle.js:20)
    at bundle.js:40
    at bundle.js:43

I have my files at the repo below.

https://github.com/psamim/svg2pdf.js-test

(npm install , ./node_modules/.bin/webpack ./entry.js bundle.js, then open index.html)

Thanks, Samim

Vasilcenko commented 7 years ago

In your entry.js you are loading the contents of your svg file and then set the innerHTML of a div to those contents. After that, you are trying to get the svg element with const svgElemnt = div.firstChild;. This returns a comment element, not the actual svg element. That is because the svg file starts with a XML declaration (<?xml version="1.0"...), which is converted to a comment when inserted into the DOM via div.innerHTML = ....

So instead, retrieve the actual svg element (e.g. with const svgElement = div.querySelector('svg');), and everything should work.