WordToPdfUtil.java 6.04 KB
package com.lhcredit.common.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * Created by llss on 2018/9/10
 */
@Component
@Slf4j
public class WordToPdfUtil  implements InitializingBean {
    @Value("${tempDirectory}")
    public  String tempDirectoryLux;
    @Value("${fileDirectory}")
    private  String fileDirectoryLux;
    @Value("${tempDirectoryWin}")
    private  String tempDirectoryWin;
    @Value("${fileDirectoryWin}")
    private  String fileDirectoryWin;
    @Value("${reportTemplateWin}")
    private  String resourcePathWin;
    @Value("${reportTemplate}")
    private  String resourcePathLux;
    public static String tempDirectory;
    public static String fileDirectory;
    public static String resourcePath;
    public  void init(){
        String os = System.getProperty("os.name");
        if (StringUtils.isEmpty(WordToPdfUtil.fileDirectory)||StringUtils.isEmpty(WordToPdfUtil.tempDirectory)){
            if (os.contains("Windows")){
                WordToPdfUtil.fileDirectory=fileDirectoryWin;
                WordToPdfUtil.tempDirectory=tempDirectoryWin;
                WordToPdfUtil.resourcePath=resourcePathWin;
            }else{
                WordToPdfUtil.fileDirectory=fileDirectoryLux;
                WordToPdfUtil.tempDirectory=tempDirectoryLux;
                WordToPdfUtil.resourcePath=resourcePathLux;
            }
        }
    }

    public static boolean getLicense() {
        boolean result = false;
        try {
//            String tempDirectory ="C:\\Users\\llss\\IdeaProjects\\dna-portals\\src\\templates";
            InputStream is = new FileInputStream(WordToPdfUtil.tempDirectory + "license.xml");
            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String doc2pdf(InputStream inputStream, String pdfName) {
        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            pdfName=pdfName+System.currentTimeMillis()+".pdf";
            long old = System.currentTimeMillis();
            File file = new File(WordToPdfUtil.fileDirectory + pdfName);  //新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document( inputStream);                    //Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
            return WordToPdfUtil.fileDirectory + pdfName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String doc2pdf(String wordPath, String pdfName) {
        if (!getLicense()) {          // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            pdfName=pdfName+System.currentTimeMillis()+".pdf";
            long old = System.currentTimeMillis();
            File file = new File(WordToPdfUtil.fileDirectory + pdfName);  //新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document( wordPath);                    //Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
            return WordToPdfUtil.fileDirectory + pdfName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        init();
    }

    public static void main(String[] args) throws Exception {
        doc2pdf2("D:\\word\\华为技术有限公司1718934279631.docx","11111.pdf");
    }
    public static String doc2pdf2(String wordPath, String pdfPath) throws Exception {
//        fileDirectory="D:\\word\\";
//        InputStream is = new FileInputStream("D:\\javaProject\\credit-manager-new2\\src\\main\\resources\\docTemp\\license.xml");
        InputStream is = new FileInputStream(WordToPdfUtil.tempDirectory + "license.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        try {
            long old = System.currentTimeMillis();
            File file = new File(fileDirectory + pdfPath);  //新建一个pdf文档
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath);                    //Address是将要被转化的word文档
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");  //转化用时
            return fileDirectory + pdfPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String filePathChangeOs(String path){
        String os = System.getProperty("os.name");
        if (!os.contains("Windows")) {
            path = path.replace("/\\", "/").replace("\\", "/");
        }else{
            path = path.replace("/", "\\").replace("\\", "\\");
        }
        return path;
    }

}