How to converted SVG Files into PDF in JSPDF using javascript

强颜欢笑 提交于 2019-12-04 10:56:05

What worked for me was to:

  1. Use svgCrowbar to serialize the SVG

    var uri = svgCrowbar(eGraph);
    
  2. Write the serialized SVG to a PNG dataURI using canvas

    // Create image object which will enable the graph to be saved (via canvas)
    var image = new Image();
    image.onload = function () {
        // Create canvas 
        var canvas = document.createElement('canvas');
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext('2d');
        context.drawImage(image, 0, 0);
    
        // Save DataURI for later use
        window.sitescriptdata.dataURI[id] = canvas.toDataURL('image/png');
        window.sitescriptdata.numDataURIsLoaded++;        
    
        ...
    
    };
    image.src = uri;
    
  3. Use jsPDF.addImage() to embed that PNG into the PDF

Using this method I was able to embed multiple dynamically generated SVGs into a single PDF. I'm not 100% happy with the outcome as the images on the PDF look distorted, but it might be that I need to do more tweaking of the jsPDF settings to get everything to look crisp.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!