I need to output the resulted PDF within the browser. This is what I have so far
public void displayWordAsPDF(InputStream inputStream,HttpServletResponse response) throws Exception {
PhysicalFonts.setRegex(".*(calibri|camb|cour|arial|symb|times|Times|zapf).*");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(inputStream);
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font = PhysicalFonts.get("Arial Unicode MS");
// make sure this is in your regex (if any)!!!
if (font != null) {
fontMapper.put("Times New Roman", font);
fontMapper.put("Arial", font);
}
fontMapper.put("Libian SC Regular", PhysicalFonts.get("SimSun"));
Docx4J.toPDF(wordMLPackage, response.getOutputStream());
}
The above code converts the docx file into PDF file (however the file name is still the same e.g something.docx
, thus the file will be downloaded locally). If I rename the downloaded file extension into .pdf then I am only able to open it up in my Adobe Reader.
My question is how I can specify the file name as well as the extension during the conversion ?
This is less a docx4j issue, and more one with updating your HttpServletResponse
object appropriately. Something like this should do the job:
response.setContentType("application/pdf");
response.setHeader("content-disposition", "attachment; filename=\"something.pdf\"");