Create PDF to print from html dom

I have a web page that dynamically creates enough content (jquery ajax, etc.) and there is a requirement to submit its print version. A.

I am facing all the usual re html / printing problems that I can get around (in terms of time), but it made me wonder if there is a way to take the DOM and create a PDF from it using javascript. This is probably a little stupid question - it sounds a bit complicated, and I'm not sure even if I could create a PDF file using javascript, as I then present it to the user.

What do people think?

+6
source share
2 answers
var doc = new jsPDF(); doc.text(20, 20, 'Hello world!'); doc.text(20, 30, 'This is client-side Javascript, pumping out a PDF.'); doc.addPage(); doc.text(20, 20, 'Do you like that?'); // Output as Data URI doc.output('datauri'); 

https://parall.ax/products/jspdf , I think this will help you

+2
source

This question is a question that I asked a few days ago regarding a similar type of site / problem.

My solution was: (1) in Javascript, to set a cookie, and then call the PHP script using location.href = ...; (not AJAX), and then (2) provide the PHP script with access to the cookie to determine the desired type of report, and then an echo form that asks the user to download the file using the correct headers. PHP was something like this:

 header("Content-type: application/vnd.ms-word"); header("Content-Disposition: attachment;Filename=test.doc"); echo "<html>"; echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">"; echo "<body>"; echo "Testing-2-3!"; echo "</body>"; echo "</html>"; 

Failed to get what I wanted to use AJAX, because AJAX never allows you to request a user.

You can use this method to do something similar, but in your case you will create a PDF file, not a .doc file (or download the one that is pre-prepared).

One of the advantages of this method, of course, is that it does not require page reloading.

0
source

Source: https://habr.com/ru/post/910976/


All Articles