可预览文件转PDF-使用Aspose
Word、Excel、TXT、PDF文件等转PDF--使用Aspose
需求背景
支持各种文件上传,并支持部分文件的预览。
由于项目前端的文件预览需要支持以下格式,但是ppt、txt无法预览,需要转换为PDF查看。
1.docx、doc文件起初使用documents4j,因为项目是部署在Linux上的,本地转换是正常的,在Linux上执行需要ms office,执行不了vbs,会报错,最后换成了Aspose。
2.Excel文件用documents4j转换时效果不好,列数太多,宽度太宽会分页显示,也换成Aspose来处理。
word和excel都参考了这个地址 传送门。
3.ppt使用 itextpdf 和 poi,转换为图片再写入PDF中。
Word、Excel文档转PDF
下载aspose-word的jar包,在项目的resource下创建lib,放入jar包。网上找个license.xml也放在resource下。没有license会有水印。
maven项目,需要在pom文件中引入依赖,项目打包时也要把jar包打上。
<dependency>
<groupId>com.aspose</groupId>
<artifactId>cells</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/aspose-cells-8.5.2.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>words</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/src/main/resources/lib/aspose-words-16.8.0.jar</systemPath>
</dependency>
原帖是写的接口,在我的项目中我写成了工具类,方便调用。
1.载入license授权文件,License导入的是com.aspose.words.License下的,excel导入的是cells下的。
protected static boolean matchLicense() {
boolean result = false;
InputStream is = Word2Pdf.class.getClassLoader().getResourceAsStream("licenses/license.xml");
License wordLicense = new License();
try {
wordLicense.setLicense(is);
result = true;
} catch (Exception e) {
log.warn("载入word授权文件失败");
}
return result;
}
2.匹配文件类型,doc、docx、txt都支持
protected static boolean matchFileType(String fileName) {
//fileTypes为static变量
fileTypes.add("doc");
fileTypes.add("docx");
fileTypes.add("txt");
int index = fileName.indexOf(".");
String suffix = fileName.substring(index+1, fileName.length());
if(fileTypes.contains(suffix)) {
return true;
}
return false;
}
3.word 文件转换,依次调用
public static void convert2PDF(File sourceFile, File targetFile) {
FileOutputStream os = null;
Document doc = null;
try {
os = new FileOutputStream(targetFile);
doc = new Document(sourceFile.getAbsolutePath());
doc.save(os, SaveFormat.PDF);
} catch (Exception e) {
log.error("输出到" + targetFile.getAbsolutePath() + "错误:" + e);
}finally {
//关闭流
...
}
}
4.excel文件转换,设置表页的列宽度自适应
public static void convert2PDF(File sourceFile, File targetFile) {
FileOutputStream fileOs = null;
Workbook wb = null;
try {
fileOs = new FileOutputStream(targetFile);
wb = new Workbook(sourceFile.getAbsolutePath());
WorksheetCollection worksheets = wb.getWorksheets();
Iterator<Worksheet> iterator = worksheets.iterator();
while (iterator.hasNext()){
Worksheet next = iterator.next();
setColumnWithAuto(next);
}
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setAllColumnsInOnePagePerSheet(true);
wb.save(fileOs, saveOptions);
} catch (FileNotFoundException e) {
log.error("输出到"+targetFile.getAbsolutePath()+"错误:"+e);
} catch (Exception e) {
log.error("转换excel出错:"+e);
}finally {
//.....关闭流
}
}
/**
* 设置表页的列宽度自适应
* @param sheet
*/
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++)
{
try {
sheet.autoFitColumn(col, 0, rowCount);
} catch (Exception e) {
e.printStackTrace();
}
}
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);
}
}
}
PPT转PDF
抄袭的,贴上,下次用😋, 有ppt和pptx两种格式
package com.bridata.flightarea.document.utils;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class PPT2PDFUtils {
protected static final Logger log = LoggerFactory.getLogger(PPT2PDFUtils.class);
public static int width = 720;
public static int height = 540;
/**
* 将pptx转换为图片,直接保存在内存中,ppt很大时可能会oom!!!
*
* @param is ppt 输入流
* @return
* @throws IOException
*/
public static List<byte[]> pptx2Image(InputStream is) throws IOException {
// 获取系统可用字体
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
List availFonts = CollectionUtils.arrayToList(fontNames);
XMLSlideShow ppt = new XMLSlideShow(is);
Dimension pgSize = ppt.getPageSize();
int pageSize = ppt.getSlides().size();
List<byte[]> imgList = new ArrayList<>(pageSize);
log.info("ppt 总页数:{}, 尺寸: width={},height={}", pageSize, pgSize.width, pgSize.height);
for (int i = 0; i < pageSize; i++) {
//防止中文乱码
for (XSLFShape shape : ppt.getSlides().get(i).getShapes()) {
if (shape instanceof XSLFTextShape) {
XSLFTextShape tsh = (XSLFTextShape) shape;
for (XSLFTextParagraph p : tsh) {
for (XSLFTextRun r : p) {
String fontFamily = r.getFontFamily();
fontFamily = "宋体";
r.setFontFamily(fontFamily);
}
}
}
}
BufferedImage img = new BufferedImage(pgSize.width, pgSize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgSize.width, pgSize.height));
// render
ppt.getSlides().get(i).draw(graphics);
// save the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
imgList.add(out.toByteArray());
IOUtils.closeQuietly(out);
}
if (is != null) {
IOUtils.closeQuietly(is);
}
return imgList;
}
/**
* 将ppt转换为图片,直接保存在内存中,ppt很大时可能会oom!!!
*
* @param is ppt 输入流
* @return
* @throws IOException
*/
public static List<byte[]> ppt2Image(InputStream is) throws IOException {
// 获取系统可用字体
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontNames = e.getAvailableFontFamilyNames();
List availFonts = CollectionUtils.arrayToList(fontNames);
HSLFSlideShow ppt = new HSLFSlideShow(new HSLFSlideShowImpl(is));
Dimension pgsize = ppt.getPageSize();
int pageSize = ppt.getSlides().size();
List<byte[]> imgList = new ArrayList<>(pageSize);
log.info("ppt 总页数:{}, 尺寸: width={},height={}", pageSize, pgsize.width, pgsize.height);
for (int i = 0; i < pageSize; i++) {
//防止中文乱码
for (HSLFShape shape : ppt.getSlides().get(i).getShapes()) {
if (shape instanceof HSLFTextShape) {
HSLFTextShape tsh = (HSLFTextShape) shape;
for (HSLFTextParagraph p : tsh) {
for (HSLFTextRun r : p) {
String fontFamily = r.getFontFamily();
fontFamily = "宋体";
r.setFontFamily(fontFamily);
}
}
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
// render
ppt.getSlides().get(i).draw(graphics);
// save the output
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(img, "png", out);
imgList.add(out.toByteArray());
IOUtils.closeQuietly(out);
}
if (is != null) {
IOUtils.closeQuietly(is);
}
return imgList;
}
/**
* 将图片转换成pdf
*
* @return
* @throws Exception
*/
public static byte[] img2PDF(List<byte[]> images) throws Exception {
com.itextpdf.text.Rectangle pageSize = new com.itextpdf.text.Rectangle(0, 0, width, height); // 自定义页面大小
Document doc= new Document(pageSize, 0, 0, 0, 0);
log.info(">>>>A4尺寸:width={},height={}", pageSize.getWidth(), pageSize.getHeight());
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
PdfWriter.getInstance(doc, pdfOut);
doc.open();
float scale = pageSize.getWidth() / width;
for (byte[] image : images) {
com.itextpdf.text.Image pic = com.itextpdf.text.Image.getInstance(image);
pic.setScaleToFitLineWhenOverflow(true);
pic.scaleToFit(pageSize.getWidth(), height * scale);
doc.add(pic);
}
doc.close();
byte[] pdf = pdfOut.toByteArray();
IOUtils.closeQuietly(pdfOut);
return pdf;
}
/**
* ppt转化为pdf
* @param is ppt 输入流
* @return pdf 字节文件
* @throws Exception
*/
public static byte[] ppt2PDF(InputStream is) throws Exception {
return img2PDF(ppt2Image(is));
}
/**
* pptx转化为pdf
* @param is pptx 输入流
* @return pdf 字节文件
* @throws Exception
*/
public static byte[] pptx2PDF(InputStream is) throws Exception {
return img2PDF(pptx2Image(is));
}
}
图片转PDF
这个比较简单,用itextpdf,创建一个document,把图片转成Element中的Image,设置一些格式,比如居中之类的,写入document中。
再用document对象创建pdfWriter实例,输出即可。
private void imgToPdf(String input, OutputStream outputStream) throws IOException, DocumentException {
Document document = new Document();
//创建document对象
document.setMargins(0, 0, 0, 0);
//创建pdfWriter实例
PdfWriter.getInstance(document, outputStream);
//打开文档
document.open();
//在文档中增加图片
Image img = Image.getInstance(input);
img.setAlignment(Image.ALIGN_CENTER);
document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
document.newPage();
document.add(img);
document.close();
}