package com.pig4cloud.pigx.admin.utils;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* @author xiechenyu
* @Description:
* @date 2024/5/22 9:27
*/
public class ConvertPdfUtil {
private static final List<String> allowedFileExtensions = Arrays.asList("xls", "xlsx", "doc", "docx");
public static MultipartFile convertToPdf(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
if (Objects.isNull(originalFilename)) {
throw new RuntimeException("请上传正确文件");
}
checkFileExtensions(originalFilename);
// 转换为PDF文件
String fileExtension = getFileExtension(originalFilename);
if (Objects.equals(fileExtension, allowedFileExtensions.get(0)) || Objects.equals(fileExtension, allowedFileExtensions.get(1))) {
return excelToPdf(file);
} else if (Objects.equals(fileExtension, allowedFileExtensions.get(2)) || Objects.equals(fileExtension, allowedFileExtensions.get(3))) {
return wordToPdf(file);
}
return file;
}
//word文件转pdf,支持doc,docx
private static MultipartFile wordToPdf(MultipartFile file) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
Document pdfDocument = new Document(pdf, PageSize.A4.rotate());
InputStream in = file.getInputStream();
XWPFDocument word = new XWPFDocument(in);) {
//指定pdf的字体样式为STSong-Light,编码方式为UniGB-UCS2-H,不指定编码方式中文乱码
PdfFont pdfFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
// 逐段写入PDF
for (XWPFParagraph paragraph : word.getParagraphs()) {
pdfDocument.add(new Paragraph(paragraph.getText()).setFont(pdfFont));
}
pdfDocument.close();
String fileExtension = getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
byte[] abbreviateByteArray = byteArrayOutputStream.toByteArray();
return new MockMultipartFile(file.getName(), file.getOriginalFilename().replace("" + fileExtension, "pdf"), "application/pdf", abbreviateByteArray);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//excel文件转pdf,支持xsl,xslx
private static MultipartFile excelToPdf(MultipartFile file) {
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PdfDocument pdf = new PdfDocument(new PdfWriter(byteArrayOutputStream));
Document document = new Document(pdf, PageSize.A4.rotate());
InputStream in = file.getInputStream();) {
//指定pdf的字体样式为STSong-Light,编码方式为UniGB-UCS2-H
PdfFont pdfFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
//excel文件类型有xls和xlsx两种,对应的构造器不一样
String fileExtension = getFileExtension(Objects.requireNonNull(file.getOriginalFilename()));
Workbook workbook = Objects.equals(fileExtension, ".xsl") ? new HSSFWorkbook(in) : new XSSFWorkbook(in);
int numberOfSheets = workbook.getNumberOfSheets();
for (int k = 0; k < numberOfSheets; k++) {
Sheet sheet = workbook.getSheetAt(k);
//获取总列数
int column = getMaxColumn(sheet);
//创建一个pdf的table对象,以便在pdf中以表的形式展示,列数和excel列数一致
Table table = new Table(column);
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
for (int j = 0; j < column; j++) {
String str = "";
//获取excel单元格
Row row = sheet.getRow(i);
if (Objects.isNull(row)) {
continue;
}
org.apache.poi.ss.usermodel.Cell cell = row.getCell(j);
if (Objects.nonNull(cell)) {
CellType cellType = cell.getCellType();
if (cellType == CellType.NUMERIC) {
str = (int) cell.getNumericCellValue() + "";
} else if (cellType == CellType.FORMULA) {
switch (cell.getCachedFormulaResultType()) {
case NUMERIC:
str = String.valueOf(cell.getNumericCellValue());
break;
case STRING:
str = cell.getRichStringCellValue().getString();
break;
default:
str = "";
break;
}
} else {
str = cell.getStringCellValue();
}
}
//将pdf的单元格初始化并添加到pdf中
Cell cells = new Cell().setFont(pdfFont).add(new Paragraph(str));
table.addCell(cells);
}
}
//两个table之间增加一个空格
document.add(new Paragraph(" "));
document.add(table);
}
document.close();
byte[] abbreviateByteArray = byteArrayOutputStream.toByteArray();
//这里的contentType必须为application/pdf,不然会格式损坏
return new MockMultipartFile(file.getName(), file.getOriginalFilename().replace("." + fileExtension, ".pdf"), "application/pdf", abbreviateByteArray);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static int getMaxColumn(Sheet sheet) {
int maxColumn = 0;
for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
if (Objects.nonNull(sheet.getRow(i))) {
maxColumn = Math.max(maxColumn, sheet.getRow(i).getLastCellNum());
}
}
return maxColumn;
}
private static void checkFileExtensions(String originalFilename) {
String fileExtension = getFileExtension(originalFilename);
if (!allowedFileExtensions.contains(fileExtension)) {
throw new RuntimeException("文件格式不正确,支持转换为PDF文件的格式有:xls、xlsx、doc、docx");
}
}
private static String getFileExtension(String filename) {
if (filename.lastIndexOf('.') != -1 && filename.lastIndexOf('.') != 0) {
return filename.substring(filename.lastIndexOf('.') + 1);
} else {
return "";
}
}
}
08-09
1万+
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交