项目里前端想要上传的ppt转成pdf之后上传oss,好方便前端在页面中预览。找了好几个ppt转pdf的方案, 最终选择Apache poi
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>4.1.2version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>4.1.2version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-scratchpadartifactId>
<version>4.1.2version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>itextpdfartifactId>
<version>5.4.3version>
dependency>
<dependency>
<groupId>com.itextpdfgroupId>
<artifactId>itext-asianartifactId>
<version>5.2.0version>
dependency>
由于ppt和pptx文件格式不同,ppt是基于二进制的文件,而pptx是基于xml文件, 也是就pptx是2007年后出现的新的ppt版本,对这两种文件处理方式转换PDF其实都差不多,只是要注意接收文件ppt或pptx以及获取两种文件内容 需要的类处理,即使用POI 里面的XMLSlide 和 HSLFSlide 进行分别处理。
因为最后是要把文件上传到阿里云oss, 需要的是InputStream, 所以这里返回的是转化后pdf的InputStream
/**
* @author ErrorRua
* @date 2022/11/17
* @description: 转换PPT为PDF工具类
*/
public final class PdfConvertUtil {
/**
* @description: pptxToPdf
* @param pptIs:
* @return java.io.InputStream
* @author ErrorRua
* @date 2022/11/21
*/
public static InputStream pptToPdf(InputStream pptIs) {
Document document = null;
PdfWriter pdfWriter = null;
try (HSLFSlideShow hslfSlideShow = new HSLFSlideShow(pptIs);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
) {
// 获取ppt文件页面
Dimension dimension = hslfSlideShow.getPageSize();
document = new Document();
// pdfWriter实例
pdfWriter = PdfWriter.getInstance(document, byteArrayOutputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();
for (HSLFSlide hslfSlide : hslfSlideList) {
// 设置字体, 解决中文乱码
for (HSLFShape shape : hslfSlide.getShapes()) {
HSLFTextShape textShape = (HSLFTextShape) shape;
for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
BufferedImage bufferedImage = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new Font("宋体", Font.PLAIN, 12));
hslfSlide.draw(graphics2d);
graphics2d.dispose();
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (document != null) {
document.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
}
}
/**
* @description: pptxToPdf
* @param pptIs:
* @return java.io.InputStream
* @author ErrorRua
* @date 2022/11/21
*/
public static InputStream pptxToPdf(InputStream pptIs) {
Document document = null;
PdfWriter pdfWriter = null;
try(ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
XMLSlideShow slideShow = new XMLSlideShow(pptIs);) {
Dimension dimension = slideShow.getPageSize();
document = new Document();
pdfWriter = PdfWriter.getInstance(document, byteArrayOutputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<XSLFSlide> slideList = slideShow.getSlides();
for (XSLFSlide slide : slideList) {
// 设置字体, 解决中文乱码
for (XSLFShape shape : slide.getShapes()) {
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
BufferedImage bufferedImage = new BufferedImage((int) dimension.getWidth(), (int) dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new Font("宋体", Font.PLAIN, 12));
slide.draw(graphics2d);
graphics2d.dispose();
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (document != null) {
document.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
}
}
}
import cn.hutool.core.util.StrUtil;
import java.awt.*;
import java.awt.image.BufferedImage;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.hslf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.Image;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* 类名称:PdfConvertUtil
* 类描述:转为为PDF工具类
*/
public final class PdfConverUtil {
/**
* pptToPdf
* @param pptPath PPT文件路径
* @param pdfDir 生成的PDF文件路径
* @return
*/
public static boolean pptToPdf(String pptPath, String pdfDir) {
if (StrUtil.isEmpty(pptPath)) {
throw new RuntimeException("word文档路径不能为空");
}
if (StrUtil.isEmpty(pdfDir)) {
throw new RuntimeException("pdf目录不能为空");
}
String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";
Document document = null;
HSLFSlideShow hslfSlideShow = null;
FileOutputStream fileOutputStream = null;
PdfWriter pdfWriter = null;
try {
hslfSlideShow = new HSLFSlideShow(new FileInputStream(pptPath));
// 获取ppt文件页面
Dimension dimension = hslfSlideShow.getPageSize();
fileOutputStream = new FileOutputStream(pdfPath);
document = new Document();
// pdfWriter实例
pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<HSLFSlide> hslfSlideList = hslfSlideShow.getSlides();
for (int i=0; i < hslfSlideList.size(); i++) {
HSLFSlide hslfSlide = hslfSlideList.get(i);
// 设置字体, 解决中文乱码
for (HSLFShape shape : hslfSlide.getShapes()) {
HSLFTextShape textShape = (HSLFTextShape) shape;
for (HSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (HSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
hslfSlide.draw(graphics2d);
graphics2d.dispose();
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (document != null) {
document.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
*
* @Title: pptxToPdf
* @param pptPath PPT文件路径
* @param pdfDir 生成的PDF文件路径
*/
public static boolean pptxToPdf(String pptPath, String pdfDir) {
if (StrUtil.isEmpty(pptPath)) {
throw new RuntimeException("word文档路径不能为空");
}
if (StrUtil.isEmpty(pdfDir)) {
throw new RuntimeException("pdf目录不能为空");
}
String pdfPath = pdfDir + StrUtil.sub(pptPath, pptPath.lastIndexOf(StrUtil.BACKSLASH), pptPath.lastIndexOf(StrUtil.DOT)) + StrUtil.DOT + "pdf";
Document document = null;
XMLSlideShow slideShow = null;
FileOutputStream fileOutputStream = null;
PdfWriter pdfWriter = null;
try {
slideShow = new XMLSlideShow(new FileInputStream(pptPath));
Dimension dimension = slideShow.getPageSize();
fileOutputStream = new FileOutputStream(pdfPath);
document = new Document();
pdfWriter = PdfWriter.getInstance(document, fileOutputStream);
document.open();
PdfPTable pdfPTable = new PdfPTable(1);
List<XSLFSlide> slideList = slideShow.getSlides();
for (int i = 0, row = slideList.size(); i < row; i++) {
XSLFSlide slide = slideList.get(i);
// 设置字体, 解决中文乱码
for (XSLFShape shape : slide.getShapes()) {
XSLFTextShape textShape = (XSLFTextShape) shape;
for (XSLFTextParagraph textParagraph : textShape.getTextParagraphs()) {
for (XSLFTextRun textRun : textParagraph.getTextRuns()) {
textRun.setFontFamily("宋体");
}
}
}
BufferedImage bufferedImage = new BufferedImage((int)dimension.getWidth(), (int)dimension.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics2d = bufferedImage.createGraphics();
graphics2d.setPaint(Color.white);
graphics2d.setFont(new java.awt.Font("宋体", java.awt.Font.PLAIN, 12));
slide.draw(graphics2d);
graphics2d.dispose();
Image image = Image.getInstance(bufferedImage, null);
image.scalePercent(50f);
// 写入单元格
pdfPTable.addCell(new PdfPCell(image, true));
document.add(image);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (document != null) {
document.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
相关文章: