{"id":493,"date":"2020-03-16T10:34:33","date_gmt":"2020-03-16T00:34:33","guid":{"rendered":"https:\/\/www.docx4java.org\/blog\/?p=493"},"modified":"2020-03-16T10:34:33","modified_gmt":"2020-03-16T00:34:33","slug":"documents4j-for-pdf-output","status":"publish","type":"post","link":"https:\/\/www.docx4java.org\/blog\/2020\/03\/documents4j-for-pdf-output\/","title":{"rendered":"documents4j for PDF output"},"content":{"rendered":"<p>Generating high fidelity PDF output from Office documents has always been a challenge, given the &#8220;long tail&#8221; of features which are possible in docx\/pptx\/xlsx files.<\/p>\n<p>For Word documents, it is easy enough to output paragraphs of text, tables and images. But add in VML, DrawingML, equations, SmartArt, and fidelity becomes a challenge.<\/p>\n<p>If your documents are constrained, you may be able to find a suitable conversion tool.  Plutext&#8217;s PDF Converter was a good example of this.  It worked well on a growing range of documents.<\/p>\n<p>But ultimately, if you want great fidelity on a unconstrained set of files, you need to be using Microsoft&#8217;s own Office layout engine.<\/p>\n<p>There are various ways to do that, for example https:\/\/developer.microsoft.com\/en-us\/graph\/examples\/document-conversion<\/p>\n<p>For Java developers, a good solution is documents4j.<\/p>\n<p>It uses Office and the Microsoft Scripting Host for VBS on the conversion machine, so that machine must Microsoft Windows.  <\/p>\n<p>Documents4j can run either a &#8220;LocalConverter&#8221; or a &#8220;RemoteConverter&#8221;.  <\/p>\n<p>Using a LocalConverter is as simple as:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.io.File;\r\nimport java.util.concurrent.Future;\r\nimport java.util.concurrent.TimeUnit;\r\n\r\nimport com.documents4j.api.DocumentType;\r\nimport com.documents4j.api.IConverter;\r\nimport com.documents4j.job.LocalConverter;\r\n\r\npublic class ToPDF {\r\n\r\n\tpublic static void main(String&#x5B;] args) {\r\n\r\n\t\tFile wordFile = new File( System.getProperty(&quot;user.dir&quot;)+&quot;\/input.docx&quot; ); \r\n\t\tFile target = new File( System.getProperty(&quot;user.dir&quot;)+&quot;\/output.pdf&quot; );\r\n\t\t\r\n\t\tIConverter converter = LocalConverter.builder()\r\n                .baseFolder(new File(&quot;C:\\\\temp&quot;))\r\n                .workerPool(20, 25, 2, TimeUnit.SECONDS)\r\n                .processTimeout(30, TimeUnit.SECONDS)\r\n                .build();\t\t\r\n                \r\n       Future&lt;Boolean&gt; conversion = converter\r\n                                .convert(wordFile).as(DocumentType.MS_WORD)\r\n                                .to(target).as(DocumentType.PDF)\r\n                                .prioritizeWith(1000) \/\/ optional\r\n                                .schedule();\r\n               \r\n\t}\r\n\r\n}\r\n<\/pre>\n<p>From Maven, you just need these dependencies:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;com.documents4j&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;documents4j-local&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;1.1.1&lt;\/version&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;com.documents4j&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;documents4j-transformer-msoffice-word&lt;\/artifactId&gt;\r\n\t\t\t&lt;version&gt;1.1.1&lt;\/version&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\r\n\t\t&lt;dependency&gt;\r\n\t\t\t&lt;groupId&gt;org.slf4j&lt;\/groupId&gt;\r\n\t\t\t&lt;artifactId&gt;slf4j-simple&lt;\/artifactId&gt;\r\n\t\t&lt;\/dependency&gt;\r\n\r\n<\/pre>\n<p>For a successful conversion, your logs will contain:<\/p>\n<pre>\r\n[main] INFO com.documents4j.conversion.msoffice.MicrosoftWordBridge - From-Microsoft-Word-Converter was started successfully\r\n[main] INFO com.documents4j.job.LocalConverter - The documents4j local converter has started successfully\r\n[pool-1-thread-1] INFO com.documents4j.conversion.msoffice.MicrosoftWordBridge - Requested conversion from input.docx (application\/vnd.com.documents4j.any-msword) to output.pdf (application\/pdf)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Generating high fidelity PDF output from Office documents has always been a challenge, given the &#8220;long tail&#8221; of features which are possible in docx\/pptx\/xlsx files. For Word documents, it is easy enough to output paragraphs of text, tables and images. But add in VML, DrawingML, equations, SmartArt, and fidelity becomes a challenge. If your documents [&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-493","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":2,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"predecessor-version":[{"id":496,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/posts\/493\/revisions\/496"}],"wp:attachment":[{"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.docx4java.org\/blog\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}