package com.rongwei.bscommon.sys.utils;

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

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

/**
 * @author shangmi
 * @title WordToPDFUtils
 * @date 2023/8/29 15:54
 * @description word转PDF工具类
 */
public class WordToPDFUtils {

    /**
     * word转PDF
     * @param fileUrl word文件路径
     * @return pdf输出流
     */
    public ByteArrayOutputStream wordToPdf(String fileUrl) throws Exception {
        File file = new File(fileUrl);
        if (!file.exists()) {
            return null;
        }
        String ext = file.getName().substring(file.getName().lastIndexOf(".")).substring(1);
        if (ext.equals("doc")||ext.equals("docx")){
            InputStream inputStream = Files.newInputStream(file.toPath());
            ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
            Document nodes = new Document(inputStream);
            nodes.save(byteOutput, SaveFormat.PDF);
            return byteOutput;
        }
        return null;
    }
}
