Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a pdf created with jsPDF in all browsers

So, I have dynamically created a pdf, and now i want to print it:

var doc = new jsPDF();
var name = "Doe, John"
doc.setFontType("normal");
doc.setFontSize(12);
doc.text(20,20,'Name: '+ name);

//do something that prints the pdf...

So, how do I take this doc variable and print it. Everywhere else I found uses the url of the pdf. Do I need to create a url for it first?

So, the solution I am currently going with is to show the pdf in a new tab/window, from which the pdf can be printed.

window.open(doc.output('datauristring'));

Unfortunately, this only works in Chrome. Anyone know how to get it working in IE, Firefox, Safari, etc.?

I still wonder if there is a way to skip this step (of opening the pdf and then requiring another button to be pushed).

like image 641
Charles Avatar asked Feb 03 '16 19:02

Charles


1 Answers

There is a method autoPrint() provided by jsPDF library. You can use that as shown below

var doc = new jsPDF();
var name = "Doe, John"
doc.setFontType("normal");
doc.setFontSize(12);
doc.text(20,20,'Name: '+ name);
doc.autoPrint();
//This is a key for printing
doc.output('dataurlnewwindow');
like image 132
MITESH AGRAWAL Avatar answered Oct 04 '22 09:10

MITESH AGRAWAL