|
|
@ -28,14 +28,14 @@ public class PdfUtil { |
|
|
|
|
|
|
|
/** |
|
|
|
* 生成pdf |
|
|
|
* @param parentPath |
|
|
|
* @param title |
|
|
|
* @param parentPath 文件夹路径 |
|
|
|
* @param title 标题 |
|
|
|
* @param subhead 副标题 |
|
|
|
* @param intros |
|
|
|
* @param content |
|
|
|
* @param intros 介绍 |
|
|
|
* @param contentArr 正文 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static String credatePdf(String parentPath, String title, String subhead, List<Row> intros, List<Row> content, Margin margin) { |
|
|
|
public static String createPdf(String parentPath, String title, String subhead, Margin margin, List<Row> intros, List<Row>... contentArr) { |
|
|
|
String fileName = "pdf/" + DateUtil.today() + "/" + System.currentTimeMillis() + ".pdf"; |
|
|
|
log.info("pdf文件名:{}", fileName ); |
|
|
|
File file = new File(parentPath, fileName); |
|
|
@ -51,7 +51,7 @@ public class PdfUtil { |
|
|
|
// 中文字体
|
|
|
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); |
|
|
|
// 标题字体
|
|
|
|
Font titleChinese = new Font(bfChinese, 24, Font.BOLD); |
|
|
|
Font titleChinese = new Font(bfChinese, 14, Font.NORMAL); |
|
|
|
//设置标题
|
|
|
|
Paragraph par = new Paragraph(title, titleChinese); |
|
|
|
par.setAlignment(Element.ALIGN_CENTER); |
|
|
@ -59,22 +59,27 @@ public class PdfUtil { |
|
|
|
|
|
|
|
if (StrUtil.isNotBlank(subhead)) { |
|
|
|
// 标题字体
|
|
|
|
Font subheadChinese = new Font(bfChinese, 22, Font.NORMAL); |
|
|
|
Font subheadChinese = new Font(bfChinese, 15, Font.NORMAL); |
|
|
|
//设置标题
|
|
|
|
Paragraph subheadPar = new Paragraph(subhead, subheadChinese); |
|
|
|
subheadPar.setAlignment(Element.ALIGN_CENTER); |
|
|
|
document.add(subheadPar); |
|
|
|
} |
|
|
|
// 每行加空白
|
|
|
|
fillBlankRow(document, titleChinese); |
|
|
|
fillBlankRow(document, new Font(bfChinese, 3, Font.NORMAL)); |
|
|
|
//设置介绍内容
|
|
|
|
if (CollectionUtil.isNotEmpty(intros)) { |
|
|
|
fillRow(intros, document); |
|
|
|
} |
|
|
|
if (CollectionUtil.isNotEmpty(content)) { |
|
|
|
fillRow(content, document); |
|
|
|
if (contentArr != null && contentArr.length > 0 ) { |
|
|
|
for (List<Row> content:contentArr) { |
|
|
|
if (CollectionUtil.isNotEmpty(content)) { |
|
|
|
fillRow(content, document); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} catch (DocumentException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
log.error("导出pdf异常", e); |
|
|
@ -121,7 +126,7 @@ public class PdfUtil { |
|
|
|
|
|
|
|
PdfPTable table = new PdfPTable(size); |
|
|
|
table.setSpacingBefore(0); |
|
|
|
table.setWidthPercentage(90); |
|
|
|
table.setWidthPercentage(100); |
|
|
|
for (int j = 0; j < rows.size(); j++) { |
|
|
|
Row row = rows.get(j); |
|
|
|
table.setHorizontalAlignment(row.align); |
|
|
@ -129,8 +134,9 @@ public class PdfUtil { |
|
|
|
for (int i = 0; i < row.cells.size(); i++) { |
|
|
|
Cell cell = row.cells.get(i); |
|
|
|
PdfPCell pdfpCell = new PdfPCell(new Phrase(cell.content,font)); |
|
|
|
pdfpCell.setBorderWidthTop(cell.borderTop == null ? 0 : cell.borderTop); |
|
|
|
pdfpCell.setBorderWidthTop(j == 0 && cell.borderTop == null && cell.borderBottom != null ? cell.borderBottom : cell.borderTop == null ? 0 : cell.borderTop); |
|
|
|
pdfpCell.setBorderWidthLeft(cell.borderLeft == null ? 0 : cell.borderLeft); |
|
|
|
// pdfpCell.setBorderWidthRight(i==row.cells.size() && cell.borderRight == null && cell.borderLeft != null ? cell.borderLeft : cell.borderRight == null ? 0 : cell.borderRight);
|
|
|
|
pdfpCell.setBorderWidthRight(cell.borderRight == null ? 0 : cell.borderRight); |
|
|
|
pdfpCell.setBorderWidthBottom(cell.borderBottom == null ? 0 : cell.borderBottom); |
|
|
|
pdfpCell.setColspan(cell.colSpan); |
|
|
@ -195,6 +201,22 @@ public class PdfUtil { |
|
|
|
// 单元格是否居中
|
|
|
|
private boolean isCenter = true; |
|
|
|
|
|
|
|
public Cell() { |
|
|
|
} |
|
|
|
public Cell(String content) { |
|
|
|
this.content = content; |
|
|
|
} |
|
|
|
public Cell(String content,int colSpan,int rowSpan) { |
|
|
|
this.content = content; |
|
|
|
this.colSpan = colSpan == 0 ? 1 : colSpan; |
|
|
|
this.rowSpan = rowSpan == 0 ? 1 : rowSpan; |
|
|
|
} |
|
|
|
public Cell(String content,int colSpan,int rowSpan,Integer borderRight) { |
|
|
|
this.content = content; |
|
|
|
this.colSpan = colSpan == 0 ? 1 : colSpan; |
|
|
|
this.rowSpan = rowSpan == 0 ? 1 : rowSpan; |
|
|
|
this.borderRight = borderRight; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -203,7 +225,7 @@ public class PdfUtil { |
|
|
|
@Data |
|
|
|
public static class Margin{ |
|
|
|
private float left = 72; |
|
|
|
private float right = 0; |
|
|
|
private float right = 72; |
|
|
|
private float top = 32; |
|
|
|
private float bottom = 32; |
|
|
|
} |
|
|
|