现将html页面转为word文档在转为PDF(预览)或PDF文件下载
Word转PDF
-
-
-
-
-
-
-
- public static void convertDocxToPDF(String docxPath, String pdfPath) throws Exception {
- OutputStream os = null;
- try {
- WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(new File(docxPath));
- Mapper fontMapper = new IdentityPlusMapper();
- fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
- fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
- fontMapper.put("隶书", PhysicalFonts.get("LiSu"));
- mlPackage.setFontMapper(fontMapper);
-
- os = new java.io.FileOutputStream(pdfPath);
-
- FOSettings foSettings = Docx4J.createFOSettings();
- foSettings.setWmlPackage(mlPackage);
- Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
-
- }catch(Exception ex){
- ex.printStackTrace();
- }finally {
- IOUtils.closeQuietly(os);
- }
- }
-
- public void pdf(WordprocessingMLPackage wordMLPackage,OutputStream os)
- throws Exception {
-
- String regex = null;
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
// FO exporter setup (required)
// .. the FOSettings object
FOSettings foSettings = Docx4J.createFOSettings();
foSettings.setWmlPackage(wordMLPackage);
// Document format:
// The default implementation of the FORenderer that uses Apache Fop will output
// a PDF document if nothing is passed via
// foSettings.setApacheFopMime(apacheFopMime)
// apacheFopMime can be any of the output formats defined in org.apache.fop.apps.MimeConstants eg org.apache.fop.apps.MimeConstants.MIME_FOP_IF or
// FOSettings.INTERNAL_FO_MIME if you want the fo document as the result.
//foSettings.setApacheFopMime(FOSettings.INTERNAL_FO_MIME);
// Specify whether PDF export uses XSLT or not to create the FO
// (XSLT takes longer, but is more complete).
// Don't care what type of exporter you use
Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that uses a xsl transformation
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);
// Prefer the exporter, that doesn't use a xsl transformation (= uses a visitor)
// .. faster, but not yet at feature parity
// Docx4J.toFO(foSettings, os, Docx4J.FLAG_EXPORT_PREFER_NONXSL);
// Clean up, so any ObfuscatedFontPart temp files can be deleted
// if (wordMLPackage.getMainDocumentPart().getFontTablePart()!=null) {
// wordMLPackage.getMainDocumentPart().getFontTablePart().deleteEmbeddedFontTempFiles();
// }
// This would also do it, via finalize() methods
updater = null;
foSettings = null;
wordMLPackage = null;
}
下载PDF
- public void pdfFile(WordprocessingMLPackage wordMLPackage,OutputStream os)
throws Exception {
// Font regex (optional)
// Set regex if you want to restrict to some defined subset of fonts
// Here we have to do this before calling createContent,
// since that discovers fonts
String regex = null;
/* // Refresh the values of DOCPROPERTY fields
FieldUpdater updater = new FieldUpdater(wordMLPackage);
updater.update(true);*/
// Set up font mapper (optional)
Mapper fontMapper = new IdentityPlusMapper();
wordMLPackage.setFontMapper(fontMapper);
// .. example of mapping font Times New Roman which doesn't have certain Arabic glyphs
// eg Glyph "ي" (0x64a, afii57450) not available in font "TimesNewRomanPS-ItalicMT".
// eg Glyph "ج" (0x62c, afii57420) not available in font "TimesNewRomanPS-ItalicMT".
// to a font which does
PhysicalFont font
= PhysicalFonts.get("Arial Unicode MS");
String directory = "D:\\upload\\pdf";
String fileName = "OUT_ConvertInXHTMLURL.pdf";
File f = new File(directory,fileName);
if(f.exists()) {
// 文件已经存在,输出文件的相关信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
} else {
// 先创建文件所在的目录
f.getParentFile().mkdirs();
}
File file = new File(directory+"/"+fileName);
OutputStream os34 = new java.io.FileOutputStream(file);
Docx4J.toPDF(wordMLPackage, os34);
os.flush();
os.close();
/* updater = null;*/
wordMLPackage = null;
}