aspose word转pdf 和excel转PDF

aspose word转pdf 和excel转PDF

POM文件引入

    <!--word转PDF-->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose.words</artifactId>
        <version>14.11</version>
        <classifier>jdk16</classifier>
    </dependency>
    <!-- excel 转PDF-->
    <dependency>
        <groupId>com.aspose</groupId>
        <artifactId>aspose-cells</artifactId>
        <version>18.3</version>
    </dependency>

word转pdf


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.UUID;

/**
 * @author tqf
 * @description word转PDF
 * @date 2019-10-29
 */
public class WordToPdfUtil {

    private static Logger log = LogManager.getLogger();

    /**
     * 获取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) {
        }
        return result;
    }


    public static String convertDocx2Pdf(MultipartFile file, String templatePath, String fileName) {
        // 验证License
        if (!getLicense()) {
            log.error("验证License失败!");
        }
        FileOutputStream fileOS = null;
        String pdfPath = null;
        try {
            String uuid = UUID.randomUUID().toString();
            String str = templatePath+ File.separator + uuid + File.separator + fileName;
            File saveFile = new File(str);
            FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);
            Document doc = new Document(file.getInputStream());
            String pdfName = fileName.substring(0,fileName.lastIndexOf("."));
            pdfPath = "/" + uuid + "/" + pdfName + ".pdf";
            String pdfAbsPath = templatePath+ File.separator + uuid + File.separator + pdfName + ".pdf";
            fileOS = new FileOutputStream(new File(pdfAbsPath));
            // 保存转换的pdf文件
            doc.save(fileOS, SaveFormat.PDF);
            saveFile.delete();
        } catch (Exception e){
            log.error("error:", e);
        } finally {
            try {
                if(fileOS != null){
                    fileOS.close();
                }
            } catch (IOException e) {
                log.error("error:", e);
            }
        }
        return pdfPath;
    }


}

excel转PDF


import com.aspose.cells.License;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.PdfSecurityOptions;
import com.aspose.cells.Workbook;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.UUID;

/**
 * @version 1.0 说明 :
 */
public class ExcelToPdf {

	/**
	 * 获取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) {
		}
		return result;
	}




	public static String excel2pdf(MultipartFile file, String templatePath, String fileName) {
		String pdfPath = null;
		// 验证License
		if (!getLicense()) {
			System.out.println("验证License失败!");
		}
		try {
			String uuid = UUID.randomUUID().toString();
			String str = templatePath+ File.separator + uuid + File.separator + fileName;
			File saveFile = new File(str);
			FileUtils.copyInputStreamToFile(file.getInputStream(), saveFile);

			String pdfName = fileName.substring(0,fileName.lastIndexOf("."));
			Workbook wb = new Workbook(saveFile.getPath());// 原始excel路径
			//pdf保存选项,此处可以使excel转PDF时过长不会被分隔成多块
			PdfSaveOptions xlsSaveOption = new PdfSaveOptions();
			PdfSecurityOptions pso = new PdfSecurityOptions();
			xlsSaveOption.setSecurityOptions(pso);
			xlsSaveOption.getSecurityOptions().setPrintPermission(false);
			xlsSaveOption.setAllColumnsInOnePagePerSheet(true);
			pdfPath = "/" + uuid + "/" + pdfName + ".pdf";
			wb.save(saveFile.getParent() + File.separator + pdfName + ".pdf", xlsSaveOption);
			saveFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pdfPath;
	}

}

注明:excel转pdf除去水印的证书文件没生效,继续找寻中,有生效的希望可以共享一下

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值