问题
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