{"id":340,"date":"2016-01-31T21:02:04","date_gmt":"2016-01-31T20:02:04","guid":{"rendered":"http:\/\/h2916922.stratoserver.net:8084\/?p=340"},"modified":"2017-09-01T11:15:47","modified_gmt":"2017-09-01T09:15:47","slug":"printing-a-pdf-with-javascript","status":"publish","type":"post","link":"https:\/\/www.ivojonker.nl\/?p=340","title":{"rendered":"Printing a pdf with javascript"},"content":{"rendered":"<p>Although i really like the IBM Content Navigator &amp; Case Manager suites, its actually embarrassing that they lack a decent &#8211; out of the box &#8211; printing mechanism. For some reason they&#8217;re using a java applet to print jpg rendered documents to a printer, that in turn produce almost unreadable documents.<\/p>\n<p>I&#8217;m not sure why they&#8217;re using 1. Java applets to print 2. pour quality documents, but it&#8217;s quite obvious there are way better mechanism&#8217;s to achieve a print.<\/p>\n<p>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.<br \/>\n<script>\nfunction sampleprint340(){ \nvar iframe=document.createElement(\"iframe\"); \niframe.src=\"http:\/\/h2916922.stratoserver.net:8084\/wp-content\/uploads\/2016\/02\/PrintMe.pdf\"; iframe.style.display=\"none\"; \ndocument.body.appendChild(iframe) ;\n}\n<\/script><\/p>\n<ul>\n<li>The result:\u00a0Click <a href=\"#\" onclick=\"sampleprint340()\">here<\/a> to print a\u00a0sample document.<\/li>\n<li>The source: <a href=\"http:\/\/h2916922.stratoserver.net:8084\/wp-content\/uploads\/2016\/01\/PrintPDFExample.zip\">source \/ eclipse project<\/a><\/li>\n<\/ul>\n<p>This sample PDF was created using a java backend (using <a href=\"https:\/\/pdfbox.apache.org\/\">PDFBox<\/a>) but i&#8217;m sure it&#8217;s comparable libraries are available for php, .net etc.<\/p>\n<p>Now, as for the sample code:<\/p>\n<p>The backend\/java part:<\/p>\n<pre class=\"lang:java decode:true\" title=\"printablePDF.jsp\">&lt;%@ page trimDirectiveWhitespaces=\"true\" %&gt;\r\n&lt;%@page import=\"java.io.File\"%&gt;\r\n&lt;%@page import=\"org.apache.pdfbox.pdmodel.interactive.action.PDActionJavaScript\"%&gt;\r\n&lt;%@page import=\"org.apache.pdfbox.pdmodel.PDDocument\"%&gt;\r\n&lt;%@ page contentType=\"applicaton\/octet-stream\" %&gt;\r\n\r\n&lt;%\r\nServletContext context = session.getServletContext();\r\n\r\n\/\/load the file on the server, or from some other source (e.g. Filenet Content Engine \/ accesContentStream)\r\nString pathToPDF = context.getRealPath(request.getParameter(\"pdf\")); \r\nPDDocument document = PDDocument.load(new File(pathToPDF));\r\n\r\n\/\/append a print instruction to execute when the PDF is opened.\r\nPDActionJavaScript javascript = new PDActionJavaScript(\"this.print();\");\r\ndocument.getDocumentCatalog().setOpenAction(javascript);\r\n\r\n\/\/tell the browser to open this pdf inline \r\nresponse.setHeader(\"Content-Disposition\", \"inline\");\r\nresponse.setHeader(\"Content-Type\", \"application\/pdf;\");\r\n\r\ndocument.save(response.getOutputStream());\r\n%&gt;<\/pre>\n<p>The web\/javascript part:<\/p>\n<pre class=\"lang:js decode:true\" title=\"sample.html\">&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Printing :)&lt;\/title&gt;\r\n&lt;script&gt;\r\n\tfunction print(filename) {\r\n\t\t\/\/basically create a hidden i-frame showing the pdf.\r\n\t\tvar iFrame = document.createElement(\"iframe\");\r\n\t\tiFrame.style.display = \"none\";\r\n\t\tiFrame.src = \"printablePDF.jsp?pdf=\" + encodeURIComponent(filename);\r\n\t\tdocument.body.appendChild(iFrame);\r\n\t}\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n\t&lt;a href=\"#\" onclick=\"print('document.pdf')\"&gt;Print the \/document.pdf document now!&lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><em>edited 28-02-2016 after Jeroen&#8217;s comment.<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although i really like the IBM Content Navigator &amp; Case Manager suites, its actually embarrassing that they lack a decent &#8211; out of the box &#8211; printing mechanism. For some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-340","post","type-post","status-publish","format-standard","hentry","category-geen-categorie"],"_links":{"self":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340"}],"version-history":[{"count":15,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/340\/revisions\/606"}],"wp:attachment":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}