81,122
社区成员




//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();
}
}