word转pdf实现总结

之前项目有需求,要求java代码实现word转pdf功能.调研了很多技术都不是很成熟.特别是中文乱码的问题.大部分框架都是在中文环节出了问题.(我很奢望我能出生在一个英语国度).最终的技术选型是利用jacob实现word转pdf功能.但是服务器部署在linux服务器上.所以又产生了新的问题就是:linux和windows交互文件.这个问题还是比较容易解决的.windows上用的是pscp.exe向linux回传文件.linux使用ftp向window发送文件.

1.linux向windows发送文件,使用如下脚本:

Java代码 复制代码 收藏代码
  1. #!/bin/bash
  2. ftp -v -n xxx.xxx.xxx.xxx << EOF
  3. user 用户名 密码
  4. binary
  5. hash
  6. lcd /user/locale/ (发送文件所在的文件夹)
  7. prompt
  8. mput $1(传进来的第一个参数 代表发送的文件名)
  9. bye
  10. EOF
#!/bin/bash
ftp -v -n xxx.xxx.xxx.xxx << EOF
user 用户名 密码
binary
hash
lcd /user/locale/ (发送文件所在的文件夹)
prompt
mput $1(传进来的第一个参数 代表发送的文件名)
bye
EOF

java调用linux脚本的方式如下:

Java代码 复制代码 收藏代码
  1. Process process = Runtime.getRuntime().exec(command)
Process process = Runtime.getRuntime().exec(command)

文件成功发送到windows服务器之后,会利用spring的httpinvoker技术远程通知windows服务器有新文件到了.关于httpinvoker的使用可以看这篇文件:http://blog.csdn.net/qq413041153/article/details/7971194

2.windows需要装office2007sp1以上版本就可以使用jacob命令调用word接口生成pdf了.windows上java利用jacob word转pdf的代码如下:

Java代码 复制代码 收藏代码
  1. private synchronized void word2PDF(String docfile, String toFile) {
  2. logger.info("WTopTools word2PDF begin");
  3. ActiveXComponent app = new ActiveXComponent("Word.Application");
  4. try {
  5. app.setProperty("Visible", new Variant(false));
  6. Dispatch docs = app.getProperty("Documents").toDispatch();
  7. Dispatch doc = Dispatch.invoke(
  8. docs,
  9. "Open",
  10. Dispatch.Method,
  11. new Object[] { docfile, new Variant(false),
  12. new Variant(true) }, new int[1]).toDispatch();
  13. File file1 = new File(toFile);
  14. File file2 = new File(toFile + ".pdf");
  15. if (file1.exists()) {
  16. file1.delete();
  17. }
  18. if (file2.exists()) {
  19. file2.delete();
  20. }
  21. Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
  22. toFile, new Variant(17) }, new int[1]);
  23. Variant f = new Variant(false);
  24. Dispatch.call(doc, "Close", f);
  25. logger.info("转换成功:" + toFile);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. logger.info("转换失败:" + docfile + ", " + e.getMessage());
  29. } finally {
  30. app.invoke("Quit", new Variant[] {});
  31. }
  32. try {
  33. Thread.sleep(3000);
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. logger.info("WTopTools word2PDF end");
  38. }
private  synchronized void word2PDF(String docfile, String toFile) {
		logger.info("WTopTools word2PDF begin");
		ActiveXComponent app = new ActiveXComponent("Word.Application");
		try {
			app.setProperty("Visible", new Variant(false));
			Dispatch docs = app.getProperty("Documents").toDispatch();
			Dispatch doc = Dispatch.invoke(
					docs,
					"Open",
					Dispatch.Method,
					new Object[] { docfile, new Variant(false),
							new Variant(true) }, new int[1]).toDispatch();
			File file1 = new File(toFile);
			File file2 = new File(toFile + ".pdf");
			if (file1.exists()) {
				file1.delete();
			}
			if (file2.exists()) {
				file2.delete();
			}

			Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
					toFile, new Variant(17) }, new int[1]);
			Variant f = new Variant(false);
			Dispatch.call(doc, "Close", f);
			logger.info("转换成功:" + toFile);
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("转换失败:" + docfile + ",  " + e.getMessage());
		} finally {
			app.invoke("Quit", new Variant[] {});
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		logger.info("WTopTools word2PDF end");
	}

3.window使用pscp.exe向linux发送文件.同时利用httpinvoker通知linux服务器文件转换完成.在第一次使用pscp.exe的使用会有提示操作.如果你直接用java代码调用的话会直接推出.所有第一次使用请用cmd手动执行一次,并且根据选项操作一下就可以了. window发送文件到linux的bat脚本代码如下:

Java代码 复制代码 收藏代码
  1. pscp.exe -q -pw 密码 -P 端口 d:\file\%1 wap@xxx.xxx.xxx.xxx:/usr/local/
pscp.exe -q -pw 密码 -P 端口 d:\file\%1  wap@xxx.xxx.xxx.xxx:/usr/local/

其中-pw参数后面是linux密码. -p后面是端口号, 在后面是window本地要发送的文件.%1表示调用bat脚本时传进来的第一个参数.wap@xxx.xxx.xxx.xxx:/usr/local/表示用wap用户链接xxx.xxx.xxx.xxxlinux服务器,并且文件发送到/user/local文件夹下.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值