在web项目中,尤其是在带有工作流的应用中,通常需要把doc文件转成pdf文件供领导审核。我的设计思路是在服务器上运行一个job,每隔一定的时间,去扫描指定的文件夹,发现有doc文件,则调用开源的"PDFCreator”软件进行转换,转换完成,删除该doc文件。具体实现如下:
- 下载安装"PDFCreator"。
- 配制"PDFCreator": 在选项中,选中自动保存,再选择一个目录做为自动保存的目录。
- 程序如下:
public class PdfCreater {
private static final String CLASSNAME = PdfCreater.class.getName();
public void docToPdf() {
String strIn = "";
String strOut = "";
try {
String docFolder = Resources.getProperties("docFolder");
String errorFolder = Resources.getProperties("errorFolder");
String strProcess = Resources.getProperties("pdfCreateProcess");
File fileDirectory = new File(docFolder);
if (!fileDirectory.isDirectory()) {
throw new Exception("not a directory: " + fileDirectory.getName());
}
// Getting all files and directories in the current directory
File[] entries = fileDirectory.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return pathname.getName().endsWith("doc");
}
});
for (int i = 0; i < entries.length; ++i) {
String strParameter = "/"" + entries[i].getAbsolutePath() + "/"";
Process process = Runtime.getRuntime().exec(strProcess + strParameter);
int c = process.waitFor();
Thread.sleep(Integer.valueOf(Resources.getProperties("millisecond")));
strIn = entries[i].getAbsolutePath();
strOut = errorFolder + entries[i].getAbsolutePath().substring(entries[i].getAbsolutePath().lastIndexOf("//"));
// fail
if (c != 0) {
String errFileName = entries[i].getAbsolutePath().substring(entries[i].getAbsolutePath().lastIndexOf("//"));
LogRecorder.error(CLASSNAME, "covert pdf error, file name is :" + strParameter);
FileUtil.CopyFile(entries[i].getAbsolutePath(), errorFolder + errFileName);
FileUtil.deleteFile(entries[i].getAbsolutePath());
} else {
FileUtil.deleteFile(entries[i].getAbsolutePath());
}
}
} catch (Exception e) {
LogRecorder.error(CLASSNAME, "covert pdf error, file name is :" + strIn);
try {
FileUtil.CopyFile(strIn, strOut);
FileUtil.deleteFile(strIn);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
上述代码是转换的核心代码,Process process = Runtime.getRuntime().exec(strProcess + strParameter); 这行代码开始调用开源软件;Thread.sleep(Integer.valueOf(Resources.getProperties("millisecond"))) 这行代码的意思是每转完一个DOC文件,要让程序sleep一段时间,以便让doc的内存彻底释放。
public class DocToPdfMain {
public static void main(String[] arg) {
Timer timer = new Timer();
try {
int startDelay = Integer.valueOf(Resources.getProperties("startDelay"));
int space = Integer.valueOf(Resources.getProperties("space"));
timer.schedule(new RunJob(), startDelay, space);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class RunJob extends TimerTask {
public void run() {
try {
new PdfCreater().docToPdf();
} catch (Exception e) {
e.printStackTrace();
LogRecorder.error(RunJob.class.getName(), "error", e);
}
}
}
以上两例是自定义的job程序。
pdfCreateProcess=C://Program Files//PDFCreator//PDFCreator.exe /PF
#this folder place doc
docFolder=E://doc2pdf//docFolder
#this folder place pdf
pdfFolder=E://doc2pdf//pdfFolder
#this folder place doc converted error
errorFolder=E://doc2pdf//errorFolder
#sleep millisecond
millisecond=5000
#start action after porgram run
startDelay = 10000
#how ofter action
space=60000
以上是配制文件信息。
set PATH=%PATH%
set classpath=%classpath%;
java -Xms64M -Xmx512M xxx.DocToPdfMain
以上是批处理信息。