JP.
JP.

Reputation: 5606

Javascript - Programmatically batch print HTML documents

tl;dr I'm looking for a good way of batch printing database-stored HTML documents from javascript

Our users generate rich text content via an open source WYSIWYG javascript-based text editor (CKEditor). The HTML content is saved to our database and can be printed directly from the editor via its inbuilt print functionality (basically just window.print()) . This is great and works perfectly.

Now, we have a need to batch print saved documents and I am looking for workable solutions. There are various options that I can see, but all have large tradeoffs:

  1. User selects documents to print. JS code loops through documents and calls print one-by-one. The issue here is that the user will see a bunch of print dialogs. Which is painful. (Aside: we are using Chrome but I do not have the option of setting it to kiosk mode)

  2. User selects documents to print. JS code combines all of these in a single (hidden) container and they are all printed as one 'document'. These can be fairly big documents with tables, images, etc. I worry about the performance involved here as we could be adding a significant amount to the DOM.

  3. Similar to #2 above, but at some point the documents are converted and saved to a single PDF. This would be OK, but there don't seem to be many good/cost-effective options for converting HTML to PDF.

  4. Generate some kind of a 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 very much appreciated!

Edit As per @Paul, I need to clarify a few points:

The content is that which is created in your standard online text editor. In my case:

Now, were I to print straight from the editor a print stylesheet would be applied, so this may complicate things a bit.

Upvotes: 6

Views: 1888

Answers (2)

Ethan
Ethan

Reputation: 2087

I completely agree with the answer above, PhantomJS would probably be the best option. The only problem with this is in terms of reliability PhantomJS has been pretty touch and go over the last few versions. If the size of the documents become too large it may become too much for Phantom to handle (remember it was originally designed for web testing purposes, and morphed into web automation). When writing the script for this, I would suggest following the outline below (to break up the processes into more manageable steps)

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

Again, it's not a perfect option overall, but it is the best one we have to work with for now. If you have any questions feel free to reach out, I'm working on web automation myself so this will all be fresh in my mind.

Download for PhantomJS Here

Upvotes: 0

Janne
Janne

Reputation: 1725

Since content could be potentially large and consume a lot of memory I would do this on server side. Select docs on client and request server to render those to PDFs e.g. utilising PhantomJS. This would then allow you to even use mobile clients to fetch PDFs.

Upvotes: 7

Related Questions