【发布时间】:2017-05-05 19:20:06
【问题描述】:
我正在尝试使用 jsPDF 将 html div 转换为 pdf。在我的 div 中,我有一个带有背景图像的 svg 文件,用户可以在其中绘制矩形、线条、文本等。我正在使用 d3.js 进行绘图。现在我想将我的 div 与所有绘图保存为 pdf,但它只将我的文本转换为 pdf。我的js代码是
function htmlToPdf() {
console.log("--------------- with in demoFromHTML");
var pdf = new jsPDF('p', 'pt', 'letter');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('svg.plancontainer')[0];
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, { // y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
// pdf.autoPrint();
pdf.output('dataurlnewwindow');
}, margins
);
}
而cdn是<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>
它打印PRINT AREA 而不是我的image 和没有svg 绘图的文本。
这是我要转换为 pdf 的示例 div 预览
我没有得到任何具体的信息来说明可以在哪里使用jsPDF。
现在我的问题是
是否可以使用
jsPDF或任何其他js库?如果可以的话,请给我推荐一下?
感谢任何形式的帮助。谢谢。
【问题讨论】:
-
你找到解决方案了吗?
标签: javascript image d3.js svg jspdf