The code provided bellow kinda works, the problemn i'm trying to solve is that i'm already able to save my docx localy. When im trying to covert it to pdf, i get blank pages or pages with only the docx header. But when i open the file manualy with Libre office or free office and save it without doing any alteration, and run the conversion again, the file converts perfectly just how i needed it. Anyone know why this happens and how could i replicate that behavior without having to actually opeing and saving the file, since i need to do all this processes programaticaly?
Im trying to covert a DOCX to PDF using:
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
<version>1.0.6</version>
</dependency>
So far my code is:
public byte[] converterXmlToDocx(Long idModeloDocumento) throws Docx4JException {
try {
byte[] gzipXml = buscarModeloDocumento(idModeloDocumento);
byte[] xmlData = descompactarGzip(gzipXml);
WordprocessingMLPackage pkg = WordprocessingMLPackage.load(new ByteArrayInputStream(xmlData));
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
pkg.save(outputStream);
//saves file localy
String outputFilePath = "src/main/resources/test1.docx";
File outputDocxFile = new File(outputFilePath);
pkg.save(outputDocxFile);
return outputStream.toByteArray();
} catch (Docx4JException | IOException e) {
e.printStackTrace();
throw new Docx4JException("Erro converting xml to docx", e);
}
}
public void convertLOCAL(String docxFilePath, String pdfFilePath) {
try {
FileInputStream docxFile = new FileInputStream(docxFilePath);
XWPFDocument document = new XWPFDocument(docxFile);
FileOutputStream pdfFile = new FileOutputStream(pdfFilePath);
PdfOptions options = PdfOptions.create();
PdfConverter.getInstance().convert(document, pdfFile, options);
docxFile.close();
pdfFile.close();
System.out.println("Sucess");
} catch (IOException ex) {
System.out.println(ex.getMessage());
throw new RuntimeException("Conversion Error", ex);
}
}
What i do above is generating the docx file with the first method them converting it using the second method in my controller, passing input docx path and output pdf path.