问题描述
我正在尝试使用jsPDF将.html文件转换为PDF并在新窗口中打开.
I am trying to use jsPDF to convert a .html file to PDF and open it in a new window.
我正在像这样处理PDF:
I am gnerating de PDF like this:
function PDFFromHTML() {
var doc = new jsPDF();
// We'll make our own renderer to skip this editor
var specialElementHandlers = {
'#editor': function(element, renderer){
return true;
}
};
// All units are in the set measurement for the document
// This can be changed to "pt" (points), "mm" (Default), "cm", "in"
doc.fromHTML('ticket.html', 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
}
我需要像window.open这样的东西才能在新窗口中打开此pdf文件,但我找不到这样做的方法.
I need something like window.open to open this pdf file in a new window but i can't find the way of doing it.
此致
推荐答案
From what I can tell, there isn't much documentation on the APIs other than looking directly at the codes and existing examples.
通过在doc.fromHTML(..)
之后添加对output
API的调用,并指定dataurlnewwindow
参数,我能够在新窗口中打开PDF文件:
I am able to get the PDF file to open in a new window by adding a call to the output
API after doc.fromHTML(..)
, specifying the dataurlnewwindow
argument like this:
doc.output('dataurlnewwindow');
关于为什么doc.fromHTML('ticket.html')
不起作用的原因,请再次参考代码,source
参数必须为:
As to why doc.fromHTML('ticket.html')
doesn't work, again referring to the code, the source
argument needs to be:
因此,您可能必须使用jquery $.get()
方法加载内容ticket.html
.本质上,看起来像这样:
So you may have to use the jquery $.get()
method to load in the content of ticket.html
. Essentially, something that looks like:
$.get('ticket.html', function(content){
doc.fromHTML(content), 15, 15, {'width': 170, 'elementHandlers': specialElementHandlers});
doc.output('dataurlnewwindow');
}, 'html');
这篇关于jsPDF从.html创建PDF并在新窗口中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!