Javascript - Batch printing of HTML documents

tl; dr I'm looking for a good way to batch print HTML documents stored in a database from javascript

Our users create rich text content through the open source text editor WYSIWYG (CKEditor). The HTML content is stored in our database and can be printed directly from the editor using the built-in print functions (basically, just window.print ()). This is great and works great.

Now we have a need for batch printing of saved documents, and I'm looking for workable solutions. There are various options that I see, but everyone has big tradeoffs:

  • The user selects documents to print. The JS code cycles through the documents and prints one after another. The problem here is that the user will see a bunch of print dialogs. This is painful. (In addition, we use Chrome, but I have no way to install it in kiosk mode)

  • The user selects documents to print. JS code combines all of this in one (hidden) container, and they all print as a single "document". These can be quite large documents with tables, images, etc. I am worried about the performance associated with this, as we could add a significant amount to the DOM.

  • Similar to # 2 above, but at some point, documents are converted and saved to a single PDF file. That would be nice, but there don't seem to be many good / economical options for converting HTML to PDF.

  • Create some kind of report that can handle HTML content. I took a look at SQL Server Reporting Services, but it supports a very limited set of HTML tags and CSS properties.

Is there a better way to batch print HTML content from javascript? Any help is much appreciated!

Change According to @Paul, I need to clarify a few points:

Content is what is created in a standard text editor. In my case:

  • Without iframe
  • No animations
  • No dynamic content

Now, if I were to print directly from the editor, the print stylesheet would be applied, so that might complicate things a bit.

+5
source share
3 answers

Since content can be potentially large and consume a lot of memory, I would do it on the server side. Select documents on the client and request server to display them in PDF files, for example. using PhantomJS. This will even allow you to use mobile clients to extract PDF files.

+7
source

Not enough comments for comments.

I would do it server side. Create HTML documents or PDF files from your internal unit and make them downloadable to users.

Disable the download button the first time a user clicks and enable it when creating documents created by the server. You can use xmlhttprequest to check the percentage of completion of a task by setting up a global variable in your internal contingent and displaying it on the client side.

If the server needs to generate a long time, ask him to check it later. As the saying goes, documents will be ready for download until xxxx-xx-xx

+1
source

I totally agree with the answer above, PhantomJS is probably the best option. The only problem with this is in terms of reliability. PhantomJS was quite moved and moved to the latest versions. If the size of the documents becomes too large, it may become too large to process Phantom (remember that it was originally designed for web testing and converted to web automation). When writing a script for this, I suggest following the diagram below (to break processes down into more manageable steps)

var steps = [ function() { // step 1 }, function() { // step 2 } ] 

Again, this is not an ideal option as a whole, but it is the best we should work with. If you have any questions, you can freely contact, I am working on website automation, so all this will be fresh in my mind.

Download for PhantomJS Here

0
source

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


All Articles