我正在尝试使用jsPDF将一个应用程序导出为PDF文件。在浏览了一些网页并抓取了一些代码段之后,我终于让它运行起来了... 某种程度上。
它在Firefox和Safari中可以工作,但在Chrome中无法正常工作。
使用的JS文件(来自jsPDF)。也许有点过多了。还有Jquery。
<script type="text/javascript" src="PDF/standard_fonts_metrics.js"></script>
<script type="text/javascript" src="PDF/split_text_to_size.js"></script>
<script type="text/javascript" src="PDF/from_html.js"></script>
<script type="text/javascript" src="PDF/addhtml.js"></script>
<script type="text/javascript" src="PDF/addimage.js"></script>
我使用的代码是这样的:
<script type="text/javascript">
function demoFromHTML() {
$('#listAreaPDF').css("display", "block");
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 = $('#listAreaPDF')[0];
pdf.setFontSize(24);
pdf.text(35, 40, "PDF Title here");
pdf.setFontSize(10);
pdf.text(500, 40, "Company Name AB");
// 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) {
html2canvas($("#presentationArea"), {
onrendered: function (canvas) {
var imgData = canvas.toDataURL(
'image/png');
pdf.addImage(imgData, 'PNG', 20, 300);
pdf.save('Test.pdf');
}
});
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
// pdf.save('Test.pdf');
},
margins
);
$('#listAreaPDF').css("display", "none");
}
</script>
参考此处的代码,感谢原作者。我对代码进行了少量更改以适应我的应用程序,并添加了与html2canvas的连接,将图像提取到PDF中。在Safari和Firefox中,它实际上运行良好。
但是,在Chrome中点击并激活此功能时,我甚至没有收到一个空白的PDF,什么也没有。甚至没有弹出窗口或生成页面。
为什么Firefox和Safari可以工作,但Chrome不行?我还没有尝试Internet Explorer,但我并不抱有希望。 如果您知道一种方法可以使其工作,我非常愿意尝试。
感谢您所提供的任何帮助或建议。