【发布时间】:2015-03-07 13:03:45
【问题描述】:
我很难使用 JSPDF 将 SVG 文件转换为 PDF。在这里,我在以下代码中工作。
var doc = new jsPDF();
var test = $.get('amChart.svg', function(svgText){
// console.log(svgText);
var svgAsText = new XMLSerializer().serializeToString(svgText.documentElement);
console.log(svgAsText);
doc.addSVG(svgAsText, 20, 20, doc.internal.pageSize.width - 20*2)
//console.log(doc);
// Save the PDF
doc.output('datauri');
});
我从这个SO Answer得到这个脚本。它的结果只有空白PDF。当我console.log(doc)在输出之前它会显示结果。但它不会导致PDF强>...
我也在这个 GITHUB URL 中使用 SVGELEMENTOPDF 函数,我也在这个代码中工作。
// I recommend to keep the svg visible as a preview
var svg = $('#container > svg').get(0);
// you should set the format dynamically, write [width, height] instead of 'a4'
var pdf = new jsPDF('p', 'pt', 'a4');
svgElementToPdf(svg, pdf, {
scale: 72/96, // this is the ratio of px to pt units
removeInvalid: true // this removes elements that could not be translated to pdf from the source svg
});
pdf.output('datauri'); // use output() to get the jsPDF buffer
但我不能实现它..请告诉我...如何在 JSPDF 中解决这个问题
【问题讨论】:
-
如果您可以像使用 wkhtmltopdf 之类的工具一样访问该命令 - wkhtmltopdf.org 有很多文档可以帮助您进行操作。请记住,为了使其正确呈现,您需要在操作系统上安装字体包。
-
由于生成PDF主要是服务器端工作,可能是它更好的废弃完整网页并使用phantomjs将整页转换为PDF
标签: javascript html svg pdf-generation jspdf