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
659 stars 101 forks source link

PDF from svg string #205

Closed sandeep-kr-kgp closed 2 years ago

sandeep-kr-kgp commented 2 years ago

const svg = someSVGString; const doc = new jsPDF(); doc.svg(svg, { x: 0, y: 0, width: 1200, height: 800, }).then(() => { // save the created pdf doc.save('myPDF.pdf'); });

This throws error that querySelectorAll is not a funciton because that's not any dom element. Is there any way to use existing svg string to generate pdf ?

sandeep-kr-kgp commented 2 years ago

nvm..I figured it out. The trick is to convert that string to DOM element using DOM Parser..Working code:

var parser = new DOMParser(); var svgDOM = parser.parseFromString(svgString, 'image/svg+xml'); const doc = new jsPDF(); doc.svg(svgDOM.documentElement, { x: 0, y: 0, width: 1200, height: 800, }).then(() => { // save the created pdf doc.save('myPDF.pdf'); });