Nw.js: Printing PDF on Windows?

Created on 15 Mar 2015  ·  6Comments  ·  Source: nwjs/nw.js

Hello!

I am needing to print a PDF on Windows via nwjs. I am using node-printer for this on Mac to print via CUPS, however Windows is tricky as it doesn't have native PDF support.

I saw a mention by @rogerwang here saying that pdf.dll could be exposed via nwjs in the future.

Are there any plans for that? The idea is to be able to print PDF on Windows without the need of Adobe Reader or other custom software. The idea is to print silently, without user interaction—to print by giving it a printer name and desired paper dimensions.

Thanks!

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

Most helpful comment

This is now supported. See #5002

All 6 comments

Will support this no later than 0.13. Thanks.

@rogerwang Thanks for your hard work. Looking forward to native support for this sort of thing.

@alexeyst While this is being worked on, you might be interested in the following:

I was in a similar situation where I had to support PDF printing on Windows. In my setup, PDFs were generated on a server and sent to client (NW) instances for printing. I found three reasonable solutions to my problem, but ended up with solution № 3 in production:

  1. Rasterize the PDF (via e.g., ImageMagick) according to the target printer's DPI and print the result via either shimgvw.dll's ImageView_PrintTo function or mspaint /pt (seriously). This works OK but is slower than it should be and there are too many steps where something can break.
  2. Inject a bit of JavaScript (basically doc.print() as per Acrobat's API) into the PDFs before sending them to clients. Clients are then responsible for creating a hidden iframe and loading the incoming PDFs in it which results in the execution of the print call(s). This of course assumes that clients have a PDF reader that has a registered NPAPI plugin _and_ is capable of executing JS (e.g., Acrobat, Foxit) _and_ JS evaluation hasn't been turned off by the user/group policy. This works OK but is relatively slow and like with solution № 1, there are lots of points of failure.
  3. Ship the portable version of Sumatra PDF (based on MuPDF) with your binaries and let it do all the heavy lifting. It's a single ~6MB executable and has pretty good command-line support. I've had great results with it in production.

A basic demo showing how solution № 3 works is available in this gist:

git clone https://gist.github.com/rdtsc/4a64c8c2b8183f6aee78 nw.js-win-print-test
  • Make sure you have a copy of the portable version of SumatraPDF.exe in the gist's working directory.
  • Tested with NW.js v0.12.0-win-ia32 on Windows 7.

Thank you very much @rogerwang!

@rdtsc, this is a great and detailed answer, thank you so much. I actually ended up using SumatraPDF just as you have shown in your example, and it works really well. I have been delivering the app to end users in this way for about a month now and it worked for them too; however as I noticed the pdf.dll coming along with NW I thought that it should be a better way.

Many thanks again for detailed overview, and for everyone who's looking for a workaround at this point, I by all means recommend using the solution 3 by @rdtsc from above.

+1

btw Windows 10 has native PDF print support

This is now supported. See #5002

Was this page helpful?
0 / 5 - 0 ratings