Printing a pdf with javascript

Although i really like the IBM Content Navigator & Case Manager suites, its actually embarrassing that they lack a decent – out of the box – printing mechanism. For some reason they’re using a java applet to print jpg rendered documents to a printer, that in turn produce almost unreadable documents.

I’m not sure why they’re using 1. Java applets to print 2. pour quality documents, but it’s quite obvious there are way better mechanism’s to achieve a print.

My personal favorite: Do a back-end render to pdf, add a document.print() to the resulting PDF, and load that PDF in a hidden iframe.

This sample PDF was created using a java backend (using PDFBox) but i’m sure it’s comparable libraries are available for php, .net etc.

Now, as for the sample code:

The backend/java part:

<%@ page trimDirectiveWhitespaces="true" %>
<%@page import=""%>
<%@page import="org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript"%>
<%@page import="org.apache.pdfbox.pdmodel.PDDocument"%>
<%@ page contentType="applicaton/octet-stream" %>

ServletContext context = session.getServletContext();

//load the file on the server, or from some other source (e.g. Filenet Content Engine / accesContentStream)
String pathToPDF = context.getRealPath(request.getParameter("pdf")); 
PDDocument document = PDDocument.load(new File(pathToPDF));

//append a print instruction to execute when the PDF is opened.
PDActionJavaScript javascript = new PDActionJavaScript("this.print();");

//tell the browser to open this pdf inline 
response.setHeader("Content-Disposition", "inline");
response.setHeader("Content-Type", "application/pdf;");;

The web/javascript part:

<!DOCTYPE html>
<title>Printing :)</title>
	function print(filename) {
		//basically create a hidden i-frame showing the pdf.
		var iFrame = document.createElement("iframe"); = "none";
		iFrame.src = "printablePDF.jsp?pdf=" + encodeURIComponent(filename);

	<a href="#" onclick="print('document.pdf')">Print the /document.pdf document now!</a>


edited 28-02-2016 after Jeroen’s comment.



14 thoughts on “Printing a pdf with javascript

    1. Hi Amit,

      That’s a good idea, will do but i’m not sure when i’ll come to it. Will try to post it as a follow-up post on this one!

  1. Thanks a lot Ivo Jonker. That will be really helpful. Will be waiting for update from you on this.

  2. Hi Jonker,

    Did you get any chance to work on Navigator Plugin related to this Print feature?

    1. Hi Amit,

      Did not have any time to spend on this yet. It’s high on my list, but i’m currently overbooked by my clients! Are you on a deadline? If so i could try to make some time for you. Additionally: what repository types are you using? P8?

  3. Hi Jonker,

    No I am not on a deadline but the problem is my end user are used to Chrome. So I am receiving complaints that just for Printing documents from Navigator they have to switch to IE. Even we requested IBM when we can have Print feature without Java dependency but it seems it will still take long time.

    And yes we are using FileNet P8.

    Thanks a lot for responding and hope to hear from you soon

  4. Hi Jonker,

    Tried creating a plugin but faced some issues while converting documents to PDF. Did you get any time to work on this?

    1. Hi Amit,

      Unfortunately i did not have any time yet! When converting documents, i think it’s probably easiest to reuse the out of the box /jaxrs/p8/getDocument. It’s quite a good service and only lacks support for a few key elements when it comes to office documents such as reviews and comments.

  5. Hi Jonker,

    Did u get any time to create a working plugin for printing. ICN newer version has removed dependancy on applet while printing but it seems to be too slow while printing.

Leave a Reply

Your email address will not be published. Required fields are marked *