excel转pdf工具类
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.words.FontSettings;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
@Slf4j
public class ExcelToPdfUtils {
private static boolean getLicense() {
/* boolean result = false;
try {
String filePath = "/License.xml";
ClassPathResource classPathResource = new ClassPathResource(filePath);
InputStream is =classPathResource.getInputStream();
// InputStream is = PdfUtil.class.getClassLoader().getResourceAsStream(""); // license.xml应放在..\WebRoot\WEB-INF\classes路径下
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}*/
boolean result = true;
return result;
}
public static File excel2pdf(String inpath, String outPath) {
// 验证License 若不验证则转化出的pdf文档会有水印产生
if (!getLicense()) {
return null;
}
FileOutputStream outputStream = null;
File file = null;
try {
//获取当前java运行
String os = System.getProperty("os.name");
//当前环境:linux系统
if (os != null && os.toLowerCase().startsWith("linux")) {
//apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
FontSettings fontSettings = FontSettings.getDefaultInstance();
fontSettings.setFontsFolder(File.separator + "usr"
+ File.separator + "share" + File.separator + "fonts" + File.separator, true);
log.info("excel2pdf_linux环境加载字体包");
}
long old = System.currentTimeMillis();
file = new File(outPath); // 新建一个空白pdf文档
outputStream = new FileOutputStream(file);
Workbook wb = new Workbook(inpath);// 原始excel路径
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//当excel中对应的sheet页宽度太大时,在PDF中会拆断并分页。此处等比缩放,不分页。
pdfSaveOptions.setOnePagePerSheet(true);
wb.save(outputStream, pdfSaveOptions);
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
log.error("excel2pdf error:" + e);
return null;
} finally {
if (outputStream != null) {
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
log.error("关闭流失败:" + e);
return null;
}
}
}
return file;
}
/**
* @Description
* @Author chengweiping
* @Date 2020/11/13 10:50
*/
public static void excel2pdfNew(InputStream inputStream, OutputStream outputStream) {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
return;
}
try {
/* FontSettings.setFontsFolder(File.separator + "usr"
+ File.separator + "share" + File.separator + "fonts", true);*/
long old = System.currentTimeMillis();
Workbook wb = new Workbook(inputStream);// 原始excel路径
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
//缩放到一个页面(如果列太多 太长)
pdfSaveOptions.setOnePagePerSheet(true);
//重点,设置所有列放在一页里,会自动适应宽度
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
wb.save(outputStream, pdfSaveOptions);
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒"); //转化用时
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用的依赖
<dependency>
<groupId>com.luhuiguo</groupId>
<artifactId>aspose-cells</artifactId>
<version>22.10</version>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.6</version>
</dependency>
linux环境excel转pdf乱码问题
需要向linux服务器上传字体包,存放目录是 /usr/share/fonts
Windows上的字体包在C:\Windows\Fonts 下
操作步骤:
- 将你选择的字体文件,比如simsun.ttc(宋体),上传到你的程序中,我是放在和target同级的文件夹下
- 代码中指定你要用的字体包的路径
//apose在linux服务器上生成pdf,内容乱码问题,解决代码:将windows的字体上传到linux,取linux上的字体列表
FontSettings fontSettings = FontSettings.getDefaultInstance();
fontSettings.setFontsFolder(File.separator + "usr"
+ File.separator + "share" + File.separator + "fonts" + File.separator, true);
- 构建时 在docker file 中写入
FROM openjdk:8-sw66
COPY target/abc.jar /abc.jar
# 复制宋体字体文件到容器中的字体目录
COPY font/simsun.ttc /usr/share/fonts/simsun.ttc
# 安装字体配置工具
RUN apt-get install -y fontconfig xfonts-utils
# 配置字体路径
RUN mkfontscale && mkfontdir && fc-cache -fv
CMD ["java", "-jar","-Duser.timezone=GMT+8","abc.jar"]
abc.jar是你的服务的jar包
到这里,问题就解决啦!!!撒花!!!