WordToPdfRenditionProvider implements RenditionProvider { @Override public InputStream convert(InputStream fromInputSource, String toMimeType) { try { WordprocessingMLPackage pkg = WordprocessingMLPackage.load(fromInputSource); FieldUpdater updater = new FieldUpdater(pkg); updater.update(true); File tmpFile = Files.createTempFile(null, null, new FileAttribute<?>[]{}).toFile(); OutputStream os = new java.io.FileOutputStream(tmpFile); Docx4J.toPDF(pkg, os); return new ObservableInputStream(new FileInputStream(tmpFile), new FileRemover(tmpFile)); } catch (Exception e) { logger.warn(format("%s rendition failed", toMimeType), e); } return null; }  @Override String consumes(); @Override String[] produces(); @Override InputStream convert(InputStream fromInputSource, String toMimeType);  }
@Test public void testConvert() throws Exception { InputStream converted = service.convert(this.getClass().getResourceAsStream("/sample-docx2.docx"),"application/pdf"); assertThat(converted.available(), is(greaterThan(0))); assertThat(((ObservableInputStream)converted).getObservers(), hasItem(is(instanceOf(FileRemover.class)))); String content = pdfToText(converted); assertThat(content, is("This is the Document Title\n \nand this is the document body.\n \n \n")); }