package com.mv.common.utils.pdf;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mv.common.constant.DictTypeConstant;
import com.mv.common.constant.TipCode;
import com.mv.common.excel.PictureModel;
import com.mv.common.utils.DictCacheUtil;
import com.mv.common.utils.MessageUtils;
import com.mv.common.utils.SpringContextUtils;
import com.mv.common.utils.StringUtilNum;
import com.mv.modules.minio.MinioUtil;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
public class PDFUtil {
public static BaseFont bfChinese;
private static int fontTitleSize = 14;
/**
* 导出一个表格数据
*
* @param response 响应
* @param titles 一行标题
* @param datas 数据
* @param fileTitle 文件标题
* @param fileNo 文件编号
* @throws Exception
*/
public static void exportData(HttpServletResponse response, List<List<String>> titles, List<List<Object>> datas, String fileTitle,
String fileNo, String fileName) {
// 设置输出的格式
response.setContentType("application/pdf");
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
OutputStream outputStream = null;
try {
if (null == bfChinese) {
bfChinese = BaseFont.createFont("/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
//临时文件目录
outputStream = response.getOutputStream();
//1.打开文档并设置基本属性
Document document = new Document();
//writer
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
writer.setPageSize(PageSize.A4);
writer.setPageEvent(new PDFBuilder());
document.open();
if (StringUtils.isNotEmpty(fileNo)) {
//文件编号
Paragraph text2 = new Paragraph(MessageUtils.getMessage(TipCode.CODE_900002) + fileNo, getPdfChineseFont(fontTitleSize));
//标题样式
text2.setPaddingTop(0.1f);
text2.setSpacingAfter(10);
document.add(text2);
}
if (StringUtils.isNotEmpty(fileTitle)) {
//文件编号
Paragraph text1 = new Paragraph(MessageUtils.getMessage(TipCode.CODE_900001) + fileTitle, getPdfChineseFont(fontTitleSize));
//标题样式
text1.setPaddingTop(0.1f);
text1.setSpacingAfter(10);
document.add(text1);
}
//第一表格列宽
PdfPTable table1 = new PdfPTable(titles.size());
table1.setWidthPercentage(100);
table1.setSpacingBefore(10);
table1.setHorizontalAlignment(Element.ALIGN_LEFT);
PdfPCell pdfCell;
Paragraph paragraph;
//table数据填充
for (int j = 0; j < titles.size(); j++) {
//选好cell处理数据
pdfCell = new PdfPCell();
setTableStyle(table1, pdfCell);
paragraph = new Paragraph(StringUtilNum.getString(titles.get(j).get(0)), getPdfChineseFont(12));
pdfCell.setPhrase(paragraph);
table1.addCell(pdfCell);
}
for (int i = 0; i < datas.size(); i++) {
for (int j = 0; j < datas.get(i).size(); j++) {
//选好cell处理数据
pdfCell = new PdfPCell();
setTableStyle(table1, pdfCell);
paragraph = new Paragraph(StringUtilNum.getString(datas.get(i).get(j)), getPdfChineseFont(12));
pdfCell.setPhrase(paragraph);
table1.addCell(pdfCell);
}
}
document.add(table1);
document.close();
} catch (Exception e) {
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
}
}
}
}
/**
* 导出图片
*/
public static void exportImage(HttpServletResponse response, String images, String fileTitle,
String fileNo, String fileName) {
JSONArray array = JSONArray.parseArray(images);
if (array.size() == 0) {
return;
}
// 设置输出的格式
response.setContentType("application/pdf");
try {
fileName = URLEncoder.encode(fileName, "UTF-8");
} catch (UnsupportedEncodingException e) {
}
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
OutputStream outputStream = null;
try {
if (null == bfChinese) {
bfChinese = BaseFont.createFont("/fonts/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
//临时文件目录
outputStream = response.getOutputStream();
//1.打开文档并设置基本属性
Document document = new Document();
//writer
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);
writer.setPageSize(PageSize.A4);
writer.setPageEvent(new PDFBuilder());
document.open();
if (StringUtils.isNotEmpty(fileNo)) {
//文件编号
Paragraph text2 = new Paragraph(MessageUtils.getMessage(TipCode.CODE_900002) + fileNo, getPdfChineseFont(fontTitleSize));
//标题样式
text2.setPaddingTop(0.1f);
text2.setSpacingAfter(10);
document.add(text2);
}
if (StringUtils.isNotEmpty(fileTitle)) {
//文件编号
Paragraph text1 = new Paragraph(MessageUtils.getMessage(TipCode.CODE_900001) + fileTitle, getPdfChineseFont(fontTitleSize));
//标题样式
text1.setPaddingTop(0.1f);
text1.setSpacingAfter(10);
document.add(text1);
}
// 通过文件名创建图像
MinioUtil minioUtil = SpringContextUtils.getBean("minioUtil", MinioUtil.class);
JSONObject object;
byte[] bytes;
Image image;
for (int j = 0; j < array.size(); j++) {
object = array.getJSONObject(j);
bytes = minioUtil.getObjectInputByte(object.getString("bucketName"), object.getString("url"));
image = Image.getInstance(bytes);
image.setSpacingBefore(10);
float scaleFactor = document.getPageSize().getWidth() / image.getScaledWidth();
image.scaleToFit(document.getPageSize().getWidth(), image.getScaledHeight() * scaleFactor);
image.setAlignmen
PDF平铺导出数据,关于导出图片,表格的代码数据
162 浏览量
2024-02-03
15:54:29
上传
评论
收藏 7KB RAR 举报
有你有我OK
- 粉丝: 9
- 资源: 4