How do I create easily a PDF from an SVG with jsPD

2019-01-11 20:46发布

I'm trying to create a pdf but I have some SVG pictures. I found information about this problem, but I just have to use JavaScript, that's to say, no jQuery.

I found jsPDF here : https://github.com/MrRio/jsPDF

There is the plugin jspdf.plugin.sillysvgrenderer.js (in the same folder) and where we can find an exemple of PDF created in the folder test.

But when I try to generate the PDF on my own, it doesn't work and I don't understand why.

Do you know how to do it?

标签: pdf svg jspdf
4条回答
叛逆
2楼-- · 2019-01-11 21:00

There now is svg2pdf.js which uses a fork of jsPDF. It has been created to solve this exact task: Exporting an SVG to a PDF.

Also in the meantime, jsPDF also added a demo that shows how to possibly export SVG using canvg and the jsPDF canvas implementation.

The two solutions have different advantages and disadvantages, so you might want to try both and see if one of them suits your needs.

查看更多
Melony?
3楼-- · 2019-01-11 21:03

Try canvg for that to covert SVG to Canvas. Then convert the canvas to base64 string using .toDataURL().

More detailed answer is here https://stackoverflow.com/a/35788928/2090459

Check the demo here http://jsfiddle.net/Purushoth/hvs91vpq/

Canvg Repo: https://github.com/gabelerner/canvg

查看更多
Fickle 薄情
4楼-- · 2019-01-11 21:12

You can use the canvas plugin that comes with jsPDF to render the SVG on the PDF with canvg. I've had to set a few dummy properties on the jsPDF canvas implementation, and disable the interactive/animation features of canvg for this to work without errors:

var jsPdfDoc = new jsPDF({
    // ... options ...
});

// ... whatever ...

// hack to make the jspdf canvas work with canvg
jsPdfDoc.canvas.childNodes = {}; 
jsPdfDoc.context2d.canvas = jsPdfDoc.canvas;
jsPdfDoc.context2d.font = undefined;

// use the canvg render the SVG onto the 
// PDF via the jsPDF canvas plugin.
canvg(jsPdfDoc.canvas, svgSource, {
    ignoreMouse: true,
    ignoreAnimation: true,
    ignoreDimensions: true,
    ignoreClear: true
});

This seems to me a much better solution than the SVG plugin for jsPDF, as canvg has much better support of SVG features. Note that the width and height properties should be set on the <svg/> element of your SVG for canvg to render it correctly (or at least so it seemed to me).

查看更多
Ridiculous、
5楼-- · 2019-01-11 21:13

I got this plugin working, but only with SVG file from the tests and the I saw in the doc that only PATHs are supported :(

There is already the issue on github https://github.com/MrRio/jsPDF/issues/384

If paths are ok for here is my code (it's more or less the code from the tests):

function demoSvgDocument() {
    var doc = new jsPDF();
    var test = $.get('013_sillysvgrenderer.svg', function(svgText){
        var svgAsText = new XMLSerializer().serializeToString(svgText.documentElement);
        doc.addSVG(svgAsText, 20, 20, doc.internal.pageSize.width - 20*2)

        // Save the PDF
        doc.save('TestSVG.pdf');
    });
}       

Another point to consider, you have to run all examples on a server. Otherwise you won't see any results probably because of the security

查看更多
登录 后发表回答