jsPDF create PDF from .html and open in new window

时光总嘲笑我的痴心妄想 提交于 2019-12-11 10:15:00

问题


I am trying to use jsPDF to convert a .html file to PDF and open it in a new window.

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
    });
}

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.

Regards,


回答1:


From what I can tell, there isn't much documentation on the APIs other than looking directly at the codes and existing examples.

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');

As to why doc.fromHTML('ticket.html') doesn't work, again referring to the code, the source argument needs to be:

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

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');


来源:https://stackoverflow.com/questions/26221041/jspdf-create-pdf-from-html-and-open-in-new-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!