word的xml格式转为普通的doc或者docx格式怎么转,请大神指教!!

孤独的搬砖人 孤独的搬砖人 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
使用java将freemarker根据ftl模板生成的word文件转化为PDF
freemarker转成的word是xml格式,转成PDF时显示的是xml内容
将freemarker生成的文件另存成doc文件,再使用openoffice转化可以正常转化
请大神指教,怎么可以将freemarker生成的word文件转为PDF文件
//freemarker生成word文件
public static void printToPdf(String templatePath, String templateName, Map<String, Object> dataMap,
String tmpUrl) {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
configuration.setServletContextForTemplateLoading(ServletContextUtils.getServletContext(), templatePath);
Template t = null;
try {
t = configuration.getTemplate(templateName);
File outFile = new File(tmpUrl);
Writer out = null;
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
t.process(dataMap, out);
IOUtils.closeQuietly(out);
String source = tmpUrl;
String target = tmpUrl.substring(0, tmpUrl.lastIndexOf('.')) + ".pdf";
word2pdf(source, target);
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
}
}

//转PDF
private static void word2pdf(String source, String target) {
String OpenOffice_HOME = "G:/soft/openoffice/";//openoffice安装目录
if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
OpenOffice_HOME += "/";
}
Process pro = null;
try {
File inputFile = new File(source);
File outputFile = new File(target);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
String command = OpenOffice_HOME
+ "program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;StarOffice.ServiceManager\" -nofirststartwizard";
pro = Runtime.getRuntime().exec(command);
OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
pro.destroy();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
pro.destroy();
}
}

...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
孤独的搬砖人
Web 开发 81,108 成员 341,721 内容