如何使用jsPDF来创建输出的pdf,正如window.print()命令所给出的那样?
我必须创建一个pdf的'div‘元素,我尝试使用jsPDF。但是我得到的输出有很多额外的项目符号和混乱的编号。但我在屏幕上看到的是格式良好的'div‘,我可以通过使用window.print()将其完全打印出来。
我唯一的抱怨是,我想要另一个选项,将此输出保存为pdf格式,以供无法将打印选项更改为'save to pdf‘的用户使用。
var pdf = new jsPDF('p', 'pt', 'letter');
var source = $('#content')[0]; //content has the survey with lot of linked list elements and various formatted text
//I tried all kind of formatting on 'source' to make it right... but its not possible to keep the form as it is what it looks on screen
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
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.save('Disclosure' + '@DateTime.Now' + '.pdf');
}
, margins);
那么,有没有一种方法可以将输出保存为window.print()格式呢?
发布于 2014-09-18 08:23:11
试一试:
var pdf = new jsPDF('p', 'pt', 'letter');
var source = $('#content')[0];
var options = { background: '#fff' };
pdf.addHTML(source, options, function()
{
pdf.save('Disclosure' + '@DateTime.Now' + '.pdf');
});
(如果您处理的是单个元素,如前面提到的div,则需要使用background选项,否则将获得透明/黑色背景)
另请注意,您需要使用html2canvas才能使其正常工作。
https://stackoverflow.com/questions/25828502
复制