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
649 stars 98 forks source link

Exported PDF content is empty / SVG not exported in PDF file #169

Closed facuescobar closed 3 years ago

facuescobar commented 3 years ago

Describe the bug The SVG content is not exported in PDF file

I'm able to generate the SVG content and display it as an HTML element in DOM. But When I export the PDF file, the SVG content is not included, the PDF content is empty.

Code

const doc = new jsPDF() // eslint-disable-line

// Convert SVG string to HTML element
const svgWrapperElement = document.getElementById('svg-wrapper')
svgWrapperElement.innerHTML = window.toolSVG // SVG string located in window 
const svgElement = svgWrapperElement.firstChild

const svgOptions = {
    x: 0,
    y: 0,
    width: Number(svgElement.getAttribute('width')),
    height: Number(svgElement.getAttribute('height')),
}

doc.svg(svgElement, svgOptions).then(() => {
    doc.save('svg.pdf')
})

Svg http://raw.githack.com/yWorks/svg2pdf.js/master/?svg=...

Screenshots SVG Element in DOM

Screen Shot 2021-03-16 at 12 17 22 PM

Exported PDF

Screen Shot 2021-03-16 at 12 18 03 PM

Desktop (please complete the following information):

yGuy commented 3 years ago

Please post the SVG here: http://raw.githack.com/yWorks/svg2pdf.js/master/ as described in the issue template. That's almost definitely a problem with the SVG and if you don't post it, we cannot help. I could send you a PDF that has your shape, but that would not be helpful, either, or would it? ;-)

facuescobar commented 3 years ago

Thanks for your quick response @yGuy, sorry I've missed that part

Here's the SVG

yGuy commented 3 years ago

Huh? But it does work in the link, doesn't it? image

Please check how your code differs from our demo.

facuescobar commented 3 years ago

Yes, as you can see, the SVG works in the demo viewer, but it doesn't work in my code.

Do you think it could be a problem related to svgOptions or something? I've tried exporting it without svgOptions but it doesn't work either.

facuescobar commented 3 years ago

Thanks for your suggestion @yGuy

I've updated my code following the demo version, and the SVG is being exported now

// Convert SVG string to HTML element
const svgWrapperElement = document.getElementById('svg-wrapper')
svgWrapperElement.innerHTML = window.svg
const svgElement = svgWrapperElement.firstChild

// Force layout calculation
svgElement.getBoundingClientRect() 
const width = svgElement.width.baseVal.value
const height = svgElement.height.baseVal.value

// eslint-disable-next-line
const pdf = new jsPDF(width > height ? 'l' : 'p', 'pt', [
    width,
    height,
])

const svgOptions = { width, height }

pdf.svg(svgElement, svgOptions).then(() => {
    pdf.save('svg.pdf')
})