Java使用aspose实现其他格式转PDF

Java使用aspose实现其他格式转PDF

文件转换格式
openOffice免费 下载软件
aspose收费 试用有水印

我用的破解版aspose 记录一下

maven仓库上是没有的,需要自己手动导入jar包

  • aspose-word:对应word
  • aspose-cells:对应excel
  • aspose-siders:对应ppt

JAR包地址

https://pan.baidu.com/s/1l56RZ5SzG6BQQEySqDlJLw

提取码
xdid

1.手动导入jar包

在这里插入图片描述

pom

<!-- aspose -->
        <dependency>
            <groupId>aspose-word</groupId>
            <artifactId>aspose-word</artifactId>
            <version>1</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/jar/113318_aspose-words-jdk16-14.9.0.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>aspose-ppt</groupId>
            <artifactId>aspose-ppt</artifactId>
            <version>1</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/jar/aspose.slides-19.3.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>aspose-excel</groupId>
            <artifactId>aspose-excel</artifactId>
            <version>1</version>
            <scope>system</scope>
            <systemPath>${basedir}/src/main/resources/jar/aspose-cells-8.5.2.jar</systemPath>
        </dependency>

2.工具类


import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.corepass.common.business.BusinessException;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @Author: xjj
 * @Description: 其他格式转PDF
 * @Date: Create in 16:53 2021/3/18
 */
public class PdfUtil {
    /**
     * aspose插件的证书
     */
    private static final String myLicense = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";


    /**
     * word转pdf,存放在与word文件同样目录下
     * @param requestInputStream word文件路径
     */
    public static InputStream wordToPdf(InputStream requestInputStream){
        ByteArrayInputStream licenseInputStream = null;
        try{
            //设置证书
            licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
            License wordLicense = new License();
            wordLicense.setLicense(licenseInputStream);
            //转化
            //Document document = new Document(sourcePath);
            Document document = new Document(requestInputStream);
            OutputStream outputStream = new ByteArrayOutputStream();
            // document.save(targetPath, 40);
            document.save(outputStream, 40);

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(((ByteArrayOutputStream) outputStream).toByteArray());
            outputStream.close();
            requestInputStream.close();
            licenseInputStream.close();
            return byteArrayInputStream;
        }catch (Exception e){
            e.printStackTrace();
            throw new BusinessException("word转PDF失败");
        }
    }

    /**
     * ppt转pdf,存放在与ppt文件同样目录下
     * @param requestInputStream ppt文件路径
     */
    public static InputStream pptToPdf(InputStream requestInputStream){
        ByteArrayInputStream licenseInputStream = null;
        try {
            //设置证书
            licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
            com.aspose.slides.License wordLicense = new com.aspose.slides.License();
            wordLicense.setLicense(licenseInputStream);
            //转化
            Presentation presentation = new Presentation(requestInputStream);
            OutputStream outputStream = new ByteArrayOutputStream();
            presentation.save(outputStream, 1);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(((ByteArrayOutputStream) outputStream).toByteArray());
            outputStream.close();
            requestInputStream.close();
            licenseInputStream.close();
            return byteArrayInputStream;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("ppt转pdf失败");
        }
    }

    /**
     * excel转pdf,存放在与excel文件同样目录下
     * @param requestInputStream ppt文件路径
     */
    public static InputStream excelToPdf(InputStream requestInputStream){
        ByteArrayInputStream licenseInputStream = null;
        try {
            //设置证书
            licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
            com.aspose.cells.License wordLicense = new com.aspose.cells.License();
            wordLicense.setLicense(licenseInputStream);
            //转化
            Workbook workbook = new Workbook(requestInputStream);
            OutputStream outputStream = new ByteArrayOutputStream();
            workbook.save(outputStream, 13);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(((ByteArrayOutputStream) outputStream).toByteArray());
            outputStream.close();
            requestInputStream.close();
            licenseInputStream.close();
            return byteArrayInputStream;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("excel转pdf失败");
        }
    }
}

3.Linux实现

这个需求是看系统,你的服务器是使用windows系统,那这一步可以不看。
下面说的是Linux服务器下的配置。

这个转化过程,假如不加以配置的话,在Linux中转化后的PDF,如果带有中文是会有乱码现象的,因为Linux里面没有中文字符的样式,所以我们要做的就是将window中的中文样式导入到Linux服务器里面。

找到windows系统的中文样式,并打成一个包。这个位于C:\Windows\Fonts,这个文件一般来说是只读的,但我们可以直接复制一个,然后再打包,打包成一个zip包,我这里打包的名字叫做windowFonts.zip
然后传到Linux服务器里
Linux安装解压工具,如果你的服务器已经安装了就可以跳过

yum install -y unzip zip

解压到指定文件夹

unzip windowFonts.zip -d /usr/share/fonts/

安装字体,大功告成

fc-cache -fv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值