转pdf的方式有很多,有的依赖windows环境,有的需要在服务器上部署插件,有的转换效果不好,转换效果好又不依赖插件的付费,几经对比采用aspost方式进行文件转换,aspost是付费服务,本文提供破解方式,仅供学习探讨,商业用途需购买许可。
一.导入依赖
1.jar资源下载
链接:https://pan.baidu.com/s/1AMlmFzmuoNm2JGQrTk340g
提取码:1129
2.jar包引入
付费jar包无法直接通过maven下载,需将下载好的jar包放在本地maven资源库中,部署到服务器时也需将jar包上传至服务器资源库。
<!-- aspose-PPT操作依赖 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose.slides</artifactId>
<version>19.3</version>
</dependency>
<!-- aspose-Excel操作依赖 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>8.5.2</version>
</dependency>
<!-- aspose-Word操作依赖 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>14.9.0-jdk16</version>
</dependency>
<!-- aspose-CAD操作依赖 -->
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-cad</artifactId>
<version>22.7</version>
</dependency>
3.jar包发送至服务器资源库
参考:https://blog.youkuaiyun.com/secretdaixin/article/details/129041068
二.文件转换工具类
1.Word转PDF
package com.dmp.common.utils.file;
import cn.hutool.core.io.FileUtil;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2023/2/14 15:05
*/
@Slf4j
public class WordToPdfUtils {
public static Boolean convert(File sourceFile, File targetFile) {
InputStream inputStream = FileUtil.getInputStream(sourceFile);
OutputStream outputStream = FileUtil.getOutputStream(targetFile);
return convert(inputStream, outputStream);
}
public static Boolean convert(InputStream inputStream, OutputStream outputStream) {
try {
Document doc = new Document(inputStream);
doc.save(outputStream, SaveFormat.PDF);
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
2.Excel转PDF
package com.dmp.common.utils.file;
import cn.hutool.core.io.FileUtil;
import com.aspose.cells.*;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.Iterator;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2023/2/14 15:09
*/
@Slf4j
public class CellToPdfUtils {
/**
* 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>";
public static void convert(File sourceFile, File targetFile) {
InputStream inputStream = FileUtil.getInputStream(sourceFile);
OutputStream outputStream = FileUtil.getOutputStream(targetFile);
convert(inputStream, outputStream);
}
public static void convert(InputStream inputStream, OutputStream outputStream) {
try {
getCellLicense();
cellConvert2PDF(inputStream,outputStream);
//Workbook workbook = new Workbook(sourceFile.getAbsolutePath());
//workbook.save(targetFile.getAbsolutePath(), com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void cellConvert2PDF(InputStream inputStream, OutputStream outputStream) {
try {
Workbook wb = new Workbook(inputStream);
Iterator<Worksheet> iterator = wb.getWorksheets().iterator();
while (iterator.hasNext()){
Worksheet next = iterator.next();
setColumnWithAuto(next);
}
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setAllColumnsInOnePagePerSheet(true);
wb.save(outputStream, saveOptions);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 设置表页的列宽度自适应
* @param sheet
*/
@SneakyThrows
private static void setColumnWithAuto(Worksheet sheet) {
Cells cells = sheet.getCells();
int columnCount = cells.getMaxColumn() + 1;//获取表页的最大列数
int rowCount = cells.getMaxRow();//获取表页的最大行数
for (int col = 0; col < columnCount; col++) {
sheet.autoFitColumn(col, 0, rowCount);
}
for (int col = 0; col < columnCount; col++) {
int pixel = cells.getColumnWidthPixel(col) + 30;
if (pixel > 255) {
cells.setColumnWidthPixel(col, 255);
} else {
cells.setColumnWidthPixel(col, pixel);
}
}
}
public static boolean getCellLicense() throws IOException {
boolean result = false;
ByteArrayInputStream licenseInputStream = null;
try {
/*String file = FileConversionUtils.class.getClassLoader().getResource("license.xml").getFile();
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(new FileInputStream(file));*/
licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
com.aspose.cells.License wordLicense = new com.aspose.cells.License();
wordLicense.setLicense(licenseInputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
licenseInputStream.close();
}
return result;
}
}
3.PPT转PDF
package com.dmp.common.utils.file;
import cn.hutool.core.io.FileUtil;
import com.aspose.slides.Presentation;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2023/2/14 15:12
*/
@Slf4j
public class PowerPointToPdfUtils {
/**
* 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>";
public static Boolean convert(File sourceFile, File targetFile) {
InputStream inputStream = FileUtil.getInputStream(sourceFile);
OutputStream outputStream = FileUtil.getOutputStream(targetFile);
return convert(inputStream, outputStream);
}
public static Boolean convert(InputStream inputStream, OutputStream outputStream) {
try {
getPPtLicense();
Presentation pres = new Presentation(inputStream);
pres.save(outputStream, com.aspose.slides.SaveFormat.Pdf);
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static boolean getPPtLicense() throws IOException {
boolean result = false;
ByteArrayInputStream licenseInputStream = null;
try {
licenseInputStream = new ByteArrayInputStream(myLicense.getBytes());
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(licenseInputStream);
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
licenseInputStream.close();
}
return result;
}
}
4.dwg转svg/png
package com.dmp.common.utils.file;
import cn.hutool.core.io.FileUtil;
import com.aspose.cad.Image;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.imageoptions.CadRasterizationOptions;
import com.aspose.cad.imageoptions.PngOptions;
import com.aspose.cad.imageoptions.SvgOptions;
import java.io.InputStream;
import java.io.OutputStream;
/**
* @author daixin
* @version 1.0
* @description: TODO
* @date 2023/2/14 15:12
*/
public class CADFileUtils {
/**
* dwg转换成png
* @param sourcePath
* @param targetPath
*/
public static void dwgToPng(String sourcePath,String targetPath){
InputStream inputStream = FileUtil.getInputStream(sourcePath);
OutputStream outputStream = FileUtil.getOutputStream(targetPath);
dwgToPng(inputStream, outputStream);
}
/**
* dwg转换成png
* @param inputStream
* @param outputStream
*/
public static void dwgToPng(InputStream inputStream, OutputStream outputStream){
PngOptions pngOptions = new PngOptions();
// 设置png的压缩等级 0 最低 9 最高
pngOptions.setCompressionLevel(0);
CadImage image = (CadImage)CadImage.load(inputStream);
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//等比缩放
uniformScale(image,cadRasterizationOptions);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
pngOptions.setVectorRasterizationOptions(cadRasterizationOptions);
image.save(outputStream, pngOptions);
}
/**
* dwg转换成Svg
* @param sourcePath
* @param targetPath
*/
public static void dwgToSvg(String sourcePath,String targetPath){
InputStream inputStream = FileUtil.getInputStream(sourcePath);
OutputStream outputStream = FileUtil.getOutputStream(targetPath);
dwgToSvg(inputStream, outputStream);
}
/**
* dwg转换成Svg
* @param inputStream
* @param outputStream
*/
public static void dwgToSvg(InputStream inputStream, OutputStream outputStream){
SvgOptions options = new SvgOptions();
Image image = Image.load(inputStream);
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
//等比缩放
uniformScale(image,cadRasterizationOptions);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
options.setVectorRasterizationOptions(cadRasterizationOptions);
image.save(outputStream,options);
}
/**
* 等比缩放
* @param image
* @param cadRasterizationOptions
*/
private static void uniformScale(Image image, CadRasterizationOptions cadRasterizationOptions){
double width = image.getWidth();
double height = image.getHeight();
double baseNum = 2073600;
double zoom = Math.sqrt(baseNum / width / height);
cadRasterizationOptions.setPageHeight((int)(height * zoom));
cadRasterizationOptions.setPageWidth((int)(width * zoom));
}
}