word转换pdf

大部分参考:https://www.jianshu.com/p/86716c7122ef

1.利用POI把word文档转换为html
    现在的POI对于word文档处理是挺强大的,对于一些简单文档的处理问题不大,但是对于一些复杂文档的处理就有点心累了,而且用户上传的文档是相对比较复杂的文档,除了复杂的表格外还有很多图片,所以放弃这种方式。

2.利用微软或者Google的在线文档预览服务

    这个方法也是不可行的,使用微软和Google的在线文档预览服务需要向他们提供word文档的链接,而客户的文件是需要保密,不能对外泄露的。

3.使用Apache OpenOffice的将word转换为PDF

    Apache OpenOffice支持多种环境,而且是开源的,我没有使用这种方式,想了解的话可以参考:https://blog.csdn.net/a1786223749/article/details/79461070

4.使用apose将word转换为PDF

    apose是一个很强大的office文档处理软件,可以完美的实现word文档转换为pdf文件,缺点就是软件是需要付费的。但是网上可以找到很多的破解版jar包,测试效果还是很不错的,而且使用简单,项目中使用的就是这种方式。

package cn.boxiao.bxn.recruitment.common.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @ClassName: Word2PdfUtil
 * @Description: word转pdf
 * @author: admin
 * @date: 2019年12月30日 下午4:32:24
 * @version v1.0.0
 */
public class Word2PdfUtil {
	private static Logger logger = LoggerFactory.getLogger(Word2PdfUtil.class);

	/**
	 * @date: 2019年12月30日 上午9:50:43
	 * @Description: 获取license
	 * @return
	 */
	private static boolean getLicense() {
		boolean result = false;
		try {
			// 凭证
			String licenseStr = "<License>\n" + "  <Data>\n" + "    <Products>\n"
					+ "      <Product>Aspose.Total for Java</Product>\n"
					+ "      <Product>Aspose.Words for Java</Product>\n" + "    </Products>\n"
					+ "    <EditionType>Enterprise</EditionType>\n"
					+ "    <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
					+ "    <LicenseExpiry>20991231</LicenseExpiry>\n"
					+ "    <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n" + "  </Data>\n"
					+ "  <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n"
					+ "</License>";
			InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
			License asposeLic = new License();
			asposeLic.setLicense(license);
			result = true;
		} catch (Exception e) {
			logger.error("failed to get license", e);
		}
		return result;
	}

	/**
	 * @date: 2019年12月30日 上午10:05:09
	 * @Description: word 转为 pdf
	 * @param filePath 源文件路径
	 * @param pdfFilePath 转为pdf存放路径
	 */
	public static String word2Pdf(String filePath, String pdfFilePath) {
		FileOutputStream fileOS = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(filePath);
			// 验证License
			if (!getLicense()) {
				logger.error("failed to validate License");
			}
			Document doc = new Document(fileInputStream);
			fileOS = new FileOutputStream(new File(pdfFilePath));
			// 保存转换的pdf文件
			doc.save(fileOS, SaveFormat.PDF);
			return pdfFilePath;
		} catch (Exception e) {
			logger.error("word to pdf error", e);
		} finally {
			try {
				if (fileOS != null) {
					fileOS.close();
				}
			} catch (IOException e) {
				logger.error("FileOutputStream close error", e);
			}
		}
		return null;
	}

}

    4.1所需apose的Jar包和maven-pom.xml

    4.1.1.链接:https://pan.baidu.com/s/19clf3JPKMkr_O9uUFF8C0Q 密码:bbu0

    4.1.2.上面不行尝试这个:https://download.csdn.net/download/qq_37725650/12065909

5.WORD转PDF功能在windows下转换正常,Linux下中文乱码问题

    因为转换时需要系统里有word文档里使用的字体,当找不到该字体的时候就会乱码。Linux下默认只有很少的中文字体,所以很可能会出现中文乱码问题。解决办法也很简单,只需在Linux系统中添加常用的字体即可。

    5.1查看系统支持的字体

fc-list

    5.2查看系统支持的中文字体

fc-list :lang=zh

     5.3创建字体目录(需要root权限,linux系统默认的字体存放路径为/usr/share/fonts,没有可自行创建)

cd /usr/share/fonts
mkdir windows

    5.4cp字体文件(将需要新增的字体文件拷贝到指定目录中)

          将windows中的字体(路径:C:\Windows\Fonts)(或者自己需要的字体)拷贝到/usr/share/fonts/windows目录

cd windows
cp ~/fonts/* ./

    5.5 目录和字体文件设置为所有用户可见

chmod 755 ../windows
chmod 755 ./*

    5.6 应用字体

mkfontscale (如果提示 mkfontscale: command not found,需自行安装 # yum install mkfontscale )
mkfontdir
fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )

    5.7再次查看系统支持的字体 

          如果不起作用:sudo shutdown -r now尝试重启

fc-list
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

低调D树苗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值