9

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).

Charles
  • 355
  • 1
  • 2
  • 12
  • 2
    You will most likely not be able to print it directly. You'll have to exploit the browser's print dialogue, and even that might not work: Embed your document as an iframe or popup, then execute JS' ``print()`` function on that. But as I said, even that is a risky game: Browsers might not allow it, or interpret the print command as pertaining to the (empty) page instead of the embedded content. I'd consider showing the PDF or allowing to download it (like the jsPDF website does) and leave printing up to the user. – Antares42 Feb 03 '16 at 20:00
  • Sending it through the browser's print dialogue would not be a problem, but downloading the pdf is something I am trying to avoid at all costs – Charles Feb 03 '16 at 20:23
  • 1
    Have a look at this answer http://stackoverflow.com/q/33900689/2446808 or this http://stackoverflow.com/q/32280613/2446808 . Automatically opening the print dialog does seem to be fraught with problems, so I'd probably go with the "show it in a frame / popup and let the user click print" option. – Antares42 Feb 03 '16 at 20:33
  • The entire purpose of the exercise is to have the user hit a button that causes the pdf to be created (as above), and then have said pdf print without the user having to press any more buttons nor download anything to print it – Charles Feb 03 '16 at 22:45
  • 1
    This will most likely never work. If you find any browser that wouldn't disallow that, then they should. Web security and stuff. Think what horrible exploits one could write if that were possible, to activate printing from javascript without user interaction. – Antares42 Feb 04 '16 at 12:28
  • the user hits a button that says "print". In order to print the necessary information without the header and footer that browsers add, I'm making a pdf. I want this button to trigger the creation and printing of the pdf without further interaction. How is that a security problem? – Charles Feb 04 '16 at 14:54
  • 1
    With very few exceptions, if you can do it with a button triggering a function, you can do it directly from the script without user interaction, which means you could write a website that prints a thousand black pages on your default printer once you open it. Not good. For example, no modern browser allows you to "remote control" the file upload dialog via JS. That's why widgets like this https://blueimp.github.io/jQuery-File-Upload/index.html need to resort to trickery with invisible floating divs and whatnot. – Antares42 Feb 04 '16 at 20:56

5 Answers5

15

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');
Mitesh Agrawal
  • 238
  • 3
  • 9
8

Try this:

window.open(doc.output('bloburl'), '_blank');

This can have problems with adblock sometimes.

Yevhenii Bahmutskyi
  • 1,561
  • 3
  • 20
  • 38
5

So, in conclusion, for Chrome and Safari, use

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

but for IE and Firefox, use

doc.save();

These will both allow you to open the pdf in a new window, from which it can be printed. For those who have taken the time to figure out what is necessary on other browsers, feel free to add your research here...

Charles
  • 355
  • 1
  • 2
  • 12
2

All you need is to add this

doc.save('Test.pdf');

(I suppose you already have all the above written code inside the button trigger which user clicks to get pdf)

Arpit Goyal
  • 1,017
  • 12
  • 25
0

You better implement "printjs" from npm, convert jspdf document to blob and print it like this

 const data = PDF.output('blob')
 const blobUrl = URL.createObjectURL(data);
 printJS(blobUrl);