jsPDF从.html创建PDF并在新窗口中打开

jsPDF create PDF from .html and open in new window

我正在尝试使用jsPDF将.html文件转换为PDF并在新窗口中打开它。

我正在像这样处理de PDF:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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文件,但我找不到这样做的方法。

问候,


据我所知,除了直接查看代码和现有示例外,没有太多关于API的文档。

通过在doc.fromHTML(..)之后添加对output API的调用,并指定dataurlnewwindow参数,我可以在新窗口中打开PDF文件:

1
doc.output('dataurlnewwindow');

至于为什么doc.fromHTML('ticket.html')不起作用,再次参考代码,source参数需要为:

either a HTML-formatted string, or a reference to an actual DOM element.

因此,您可能必须使用jquery $.get()方法加载ticket.html的内容。 本质上,看起来像:

1
2
3
4
$.get('ticket.html', function(content){
    doc.fromHTML(content), 15, 15, {'width': 170, 'elementHandlers': specialElementHandlers});
    doc.output('dataurlnewwindow');
}, 'html');