Aspose实现word、excel、ppt转pdf

1. 工具类 AsposeUtil

public class AsposeUtil {

    private static final String[] WORD = {"doc", "docx", "wps", "wpt", "txt"};
    private static final String[] EXCEL = {"xls", "xlsx", "et", "xlsm"};
    private static final String[] PPT = {"ppt", "pptx"};
    private static final String[] PDF = {"pdf"};
    private static final String[] IMG = {"bmp", "jpg", "png", "tif", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "WMF", "webp", "avif", "apng"};

    private static final String TYPE_UNSUPPORT = "不支持的格式";
    private static final String TYPE_WORD = "TYPE_WORD";
    private static final String TYPE_EXCEL = "TYPE_EXCEL";
    private static final String TYPE_PPT = "TYPE_PPT";
    private static final String TYPE_PDF = "TYPE_PDF";
    private static final String TYPE_IMG = "TYPE_IMG";

    @Autowired
    OssUtil ossUtil;

    private boolean judgeLicense() {
        boolean result = false;
        try {
//			InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
            String license =
                    "\n" +
                            "  \n" +
                            "    \n" +
                            "      Aspose.Total for Java\n" +
                            "      Aspose.Words for Java\n" +
                            "    \n" +
                            "    Enterprise\n" +
                            "    20991231\n" +
                            "    20991231\n" +
                            "    8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7\n" +
                            "  \n" +
                            "  sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n" +
                            "";
            InputStream is = new ByteArrayInputStream(license.getBytes("UTF-8"));
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            log.error("Aspose License Error,error:{}",e);
        }
        return result;
    }

    /**
     * 根据文件名判断文件类型
     *
     * @return
     */
    private String getType(String fileName) {
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1); // 后缀
        if (CollUtil.contains(Arrays.asList(WORD), suffix)) {
            return TYPE_WORD;
        } else if (CollUtil.contains(Arrays.asList(EXCEL), suffix)) {
            return TYPE_EXCEL;
        } else if (CollUtil.contains(Arrays.asList(PPT), suffix)) {
            return TYPE_PPT;
        } else if (CollUtil.contains(Arrays.asList(PDF), suffix)) {
            return TYPE_PDF;
        } else if (CollUtil.contains(Arrays.asList(IMG), suffix)) {
            return TYPE_IMG;
        } else {
            return TYPE_UNSUPPORT;
        }
    }

    /**
     * 文件转化pdf
     *
     * @param fileName 传入的文件名称
     * @param addr     传入的文件地址
     * @return 转换后的pdf地址 或 格式不支持预览
     */
    public String toPdf(String fileName, String addr) throws Exception {
        if (!judgeLicense()) {
            throw new BizException(BuErrorEnum.ASPOSE_LISENSE_PROBLEM.getErrCode(), BuErrorEnum.ASPOSE_LISENSE_PROBLEM.getErrMsg());
        }

        String type = getType(fileName);
        switch (type) {
            case TYPE_UNSUPPORT:
                return TYPE_UNSUPPORT;
            case TYPE_PDF:
                return addr;
            case TYPE_IMG:
                return addr;
            default:
                break;
        }

        InputStream in = URLUtil.getStream(URLUtil.url(addr)); // 下载文件
        ByteArrayOutputStream tmp = new ByteArrayOutputStream();
        switch (type) {
            case TYPE_WORD:
                tmp = wordToPdfStream(in);
                break;
            case TYPE_EXCEL:
                tmp = excelToPdfStream(in);
                break;
            case TYPE_PPT:
                tmp = pptToPdfStream(in);
                break;
            default:
                break;
        }

        return ossUtil.uploadFile2OSS(new ByteArrayInputStream(tmp.toByteArray()), fileName);
    }

    private ByteArrayOutputStream wordToPdfStream(InputStream in) throws Exception {
        Document doc = new Document(in);
        ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
        //解决乱码
        //如果是windows执行,不需要加这个
        //TODO 如果是linux执行,需要添加这个*****
        //FontSettings.setFontsFolder("/usr/share/fonts",true);
        //全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
        doc.save(dstStream, com.aspose.words.SaveFormat.PDF);
        return dstStream;
    }

    private ByteArrayOutputStream excelToPdfStream(InputStream in) throws Exception {

        Workbook excel = new Workbook (in);
        ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
        excel.save(dstStream, com.aspose.cells.SaveFormat.PDF);
        return dstStream;
    }

    private ByteArrayOutputStream pptToPdfStream(InputStream in) throws Exception {
        Presentation ppt = new Presentation (in);
        ByteArrayOutputStream dstStream = new ByteArrayOutputStream();
        ppt.save(dstStream, SaveFormat.Pdf);
        return dstStream;
    }
}

2. license文件

<License>
    <Data>
        <Products>
            <Product>Aspose.Total for JavaProduct>
            <Product>Aspose.Words for JavaProduct>
        Products>
        <EditionType>EnterpriseEditionType>
        <SubscriptionExpiry>20991231SubscriptionExpiry>
        <LicenseExpiry>20991231LicenseExpiry>
        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7SerialNumber>
    Data>
    <Signature>
    sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=
    Signature>
License>

3. jar包

4. 本地pom引入


        
            com.aspose
            slides
            15.9.0
            system
            ${project.basedir}/lib/aspose.slides-15.9.0.jar
        

        
            aspose
            cells
            8.5.2
            system
            ${project.basedir}/lib/aspose-cells-8.5.2.jar
        

        
            aspose
            words
            16.8.0
            system
            ${project.basedir}/lib/aspose-words-16.8.0-jdk16.jar
        
        

在这里插入图片描述

你可能感兴趣的:(实用小技巧,maven,spring,boot,java)