parent
2f90e6d436
commit
19a443e8eb
@ -0,0 +1,117 @@ |
||||
package com.dxhy.order.invoice.utils; |
||||
|
||||
import com.dxhy.order.model.OrderInvoiceDetail; |
||||
import com.itextpdf.text.*; |
||||
import com.itextpdf.text.pdf.*; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.io.FileOutputStream; |
||||
import java.lang.reflect.Method; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author Gy Chen |
||||
* @date 2021/3/17 |
||||
*/ |
||||
@Component |
||||
@SuppressWarnings({"AliDeprecation", "AlibabaUndefineMagicConstant"}) |
||||
public class PDFExportUtil { |
||||
private static PdfWriter writer; |
||||
private static ArrayList<PdfPRow> listRow; |
||||
private static PdfPTable table; |
||||
|
||||
public static void pdfExport(String fileTitle, String filePath, String titleName, List<OrderInvoiceDetail> dataList, |
||||
List<String> titleColumns,List<String> titleValues, float[] columnWidths, boolean firstPage, boolean lastPage) { |
||||
Document document = new Document(PageSize.A4); |
||||
try { |
||||
|
||||
// 中文字体,解决中文不能显示问题
|
||||
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); |
||||
// 正文字体风格
|
||||
Font contextFont = new Font(bfChinese, 10, Font.NORMAL); |
||||
if (firstPage) { |
||||
|
||||
// 两列的表
|
||||
table = new PdfPTable(titleColumns.size()); |
||||
// 宽度100%填充
|
||||
table.setWidthPercentage(100); |
||||
// 前间距
|
||||
table.setSpacingBefore(10f); |
||||
// 后间距
|
||||
table.setSpacingAfter(10f); |
||||
listRow = table.getRows(); |
||||
// 设置列宽比例 1:3
|
||||
table.setWidths(columnWidths); |
||||
// 一行两格
|
||||
PdfPCell[] cells1 = new PdfPCell[titleColumns.size()]; |
||||
PdfPRow row1 = new PdfPRow(cells1); |
||||
for (int i = 0; i < titleColumns.size(); i++) { |
||||
cells1[i] = new PdfPCell(new Paragraph(titleColumns.get(i), contextFont)); |
||||
cells1[i].setUseAscender(true); |
||||
cells1[i].setUseDescender(true); |
||||
cells1[i].setHorizontalAlignment(Element.ALIGN_CENTER); |
||||
} |
||||
|
||||
listRow.add(row1); |
||||
} |
||||
|
||||
for (Object data : dataList) { |
||||
// 一行两格
|
||||
PdfPCell[] cells1 = new PdfPCell[titleColumns.size()]; |
||||
PdfPRow row1 = new PdfPRow(cells1); |
||||
int i = 0; |
||||
|
||||
for (String columnsName : titleValues) { |
||||
String getMethodName = "get" + columnsName.substring(0, 1).toUpperCase() + columnsName.substring(1); |
||||
Method declaredMethod = data.getClass().getDeclaredMethod(getMethodName); |
||||
String columnsData= (String)declaredMethod.invoke(data); |
||||
cells1[i] = new PdfPCell(new Paragraph(columnsData, contextFont)); |
||||
cells1[i].setUseAscender(true); |
||||
cells1[i].setUseDescender(true); |
||||
if (i == 6 || i == 7 || i == 8) { |
||||
cells1[i].setHorizontalAlignment(Element.ALIGN_RIGHT); |
||||
} else { |
||||
cells1[i].setHorizontalAlignment(Element.ALIGN_LEFT); |
||||
} |
||||
i++; |
||||
} |
||||
listRow.add(row1); |
||||
} |
||||
if (lastPage) { |
||||
document = new Document(new RectangleReadOnly(842.0F, 595.0F)); |
||||
// 新建一个pdf文件
|
||||
writer = PdfWriter.getInstance(document, new FileOutputStream(filePath)); |
||||
document.open(); |
||||
// 红标题字体风格
|
||||
Font redTitleFont = new Font(bfChinese, 20, Font.BOLD, BaseColor.BLACK); |
||||
// 红标题抬头
|
||||
Paragraph redTitle = new Paragraph(fileTitle, redTitleFont); |
||||
// 居中
|
||||
redTitle.setAlignment(Element.ALIGN_CENTER); |
||||
// 文档中加入红标题字段
|
||||
document.add(redTitle); |
||||
|
||||
// 标题字体风格
|
||||
Font titleFont = new Font(bfChinese, 12, Font.NORMAL); |
||||
// 标题内容
|
||||
Paragraph title = new Paragraph(titleName, titleFont); |
||||
// 离上一段落(标题)空的行数
|
||||
title.setSpacingBefore(25); |
||||
// 设置标题格式对齐方式
|
||||
title.setAlignment(Element.ALIGN_LEFT); |
||||
// 文档中加入标题字段
|
||||
document.add(title); |
||||
// 把表格添加到文件中
|
||||
document.add(table); |
||||
} |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} finally { |
||||
if (lastPage) { |
||||
document.close(); |
||||
writer.close(); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue