17 changed files with 526 additions and 51 deletions
@ -0,0 +1,164 @@ |
|||||
|
package com.ccsens.util; |
||||
|
|
||||
|
import cn.hutool.core.collection.CollectionUtil; |
||||
|
import cn.hutool.core.date.DateUtil; |
||||
|
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 lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileNotFoundException; |
||||
|
import java.io.FileOutputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: wuHuiJuan |
||||
|
* @create: 2019/12/20 09:34 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class PdfUtil { |
||||
|
|
||||
|
/** |
||||
|
* 生成pdf |
||||
|
* @param parentPath |
||||
|
* @param title |
||||
|
* @param intros |
||||
|
* @param content |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String credatePdf(String parentPath, String title, List<Row> intros, List<Row> content) { |
||||
|
String fileName = "pdf/" + DateUtil.today() + "/" + title + System.currentTimeMillis() + ".pdf"; |
||||
|
log.info("pdf文件名:{}", fileName ); |
||||
|
File file = new File(parentPath, fileName); |
||||
|
if (!file.getParentFile().exists()) { |
||||
|
file.getParentFile().mkdirs(); |
||||
|
} |
||||
|
//新建文件
|
||||
|
Document document = new Document(); |
||||
|
try { |
||||
|
|
||||
|
PdfWriter.getInstance(document, new FileOutputStream(file)); |
||||
|
document.open(); |
||||
|
|
||||
|
// 中文字体
|
||||
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); |
||||
|
// 标题字体
|
||||
|
Font titleChinese = new Font(bfChinese, 18, Font.BOLD); |
||||
|
//设置标题
|
||||
|
Paragraph par = new Paragraph(title, titleChinese); |
||||
|
par.setAlignment(Element.ALIGN_CENTER); |
||||
|
document.add(par); |
||||
|
// 每行加空白
|
||||
|
fillBlankRow(document, titleChinese); |
||||
|
|
||||
|
//设置介绍内容
|
||||
|
if (CollectionUtil.isNotEmpty(intros)) { |
||||
|
fillRow(intros, document); |
||||
|
fillBlankRow(document, titleChinese); |
||||
|
} |
||||
|
if (CollectionUtil.isEmpty(content)) { |
||||
|
fillRow(content, document); |
||||
|
} |
||||
|
|
||||
|
} catch (DocumentException e) { |
||||
|
e.printStackTrace(); |
||||
|
log.error("导出pdf异常", e); |
||||
|
return null; |
||||
|
} catch (FileNotFoundException e) { |
||||
|
e.printStackTrace(); |
||||
|
log.error("导出pdf异常", e); |
||||
|
return null; |
||||
|
} catch (IOException e) { |
||||
|
e.printStackTrace(); |
||||
|
log.error("导出pdf异常", e); |
||||
|
return null; |
||||
|
} finally { |
||||
|
document.close(); |
||||
|
} |
||||
|
|
||||
|
return fileName; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 添加空白行 |
||||
|
* @param document |
||||
|
* @param titleChinese |
||||
|
* @throws DocumentException |
||||
|
*/ |
||||
|
private static void fillBlankRow(Document document, Font titleChinese) throws DocumentException { |
||||
|
Paragraph par; |
||||
|
par = new Paragraph(" ", titleChinese); |
||||
|
par.setAlignment(Element.ALIGN_LEFT); |
||||
|
document.add(par); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 添加内容 |
||||
|
* @param rows |
||||
|
* @param document |
||||
|
*/ |
||||
|
private static void fillRow(List<Row> rows, Document document) throws IOException, DocumentException { |
||||
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); |
||||
|
rows.forEach(row -> { |
||||
|
|
||||
|
PdfPTable table = new PdfPTable(row.getCells().size()); |
||||
|
table.setWidthPercentage(100); |
||||
|
table.setSpacingBefore(10); |
||||
|
|
||||
|
|
||||
|
|
||||
|
Font font = new Font(bfChinese, row.fontSize, row.fontStyle); |
||||
|
row.cells.forEach(cell -> { |
||||
|
PdfPCell pdfpCell = new PdfPCell(new Paragraph(cell.content,font)); |
||||
|
pdfpCell.setBorder(cell.border); |
||||
|
pdfpCell.setColspan(cell.colSpan); |
||||
|
pdfpCell.setRowspan(cell.rowSpan); |
||||
|
table.addCell(pdfpCell); |
||||
|
}); |
||||
|
try { |
||||
|
table.setWidthPercentage(90); |
||||
|
table.setHorizontalAlignment(row.align); |
||||
|
document.add(table); |
||||
|
} catch (DocumentException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* pdf每行的内容 |
||||
|
*/ |
||||
|
@Data |
||||
|
public static class Row{ |
||||
|
//表格内容
|
||||
|
private List<Cell> cells = new ArrayList<>(); |
||||
|
//文字位置(居左)
|
||||
|
private int align = Element.ALIGN_LEFT; |
||||
|
private int fontSize = 12; |
||||
|
private int fontStyle = Font.NORMAL; |
||||
|
private int height = 80; |
||||
|
|
||||
|
public void addCell(Cell cell) { |
||||
|
cells.add(cell); |
||||
|
} |
||||
|
} |
||||
|
@Data |
||||
|
public static class Cell{ |
||||
|
//内容
|
||||
|
private String content; |
||||
|
//边框宽度
|
||||
|
private int border = 1; |
||||
|
//横向合并数
|
||||
|
private int colSpan = 1; |
||||
|
//纵向合并数
|
||||
|
private int rowSpan = 1; |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,42 @@ |
|||||
|
package com.ccsens.util; |
||||
|
|
||||
|
import org.junit.Test; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @description: |
||||
|
* @author: wuHuiJuan |
||||
|
* @create: 2019/12/20 10:18 |
||||
|
*/ |
||||
|
public class PdfUtilTest { |
||||
|
@Test |
||||
|
public void test(){ |
||||
|
List<PdfUtil.Row> rows = new ArrayList<>(); |
||||
|
for (int i = 0; i < 3; i++) { |
||||
|
PdfUtil.Cell cell = new PdfUtil.Cell(); |
||||
|
cell.setContent("cell" + i); |
||||
|
PdfUtil.Row row = new PdfUtil.Row(); |
||||
|
row.addCell(cell); |
||||
|
PdfUtil.Cell cell2 = new PdfUtil.Cell(); |
||||
|
cell2.setContent("cell--" + i); |
||||
|
row.addCell(cell2); |
||||
|
rows.add(row); |
||||
|
} |
||||
|
List<PdfUtil.Row> contents = new ArrayList<>(); |
||||
|
for (int i = 0; i < 3; i++) { |
||||
|
PdfUtil.Cell cell = new PdfUtil.Cell(); |
||||
|
cell.setContent("*******" + i); |
||||
|
PdfUtil.Row row = new PdfUtil.Row(); |
||||
|
row.addCell(cell); |
||||
|
PdfUtil.Cell cell2 = new PdfUtil.Cell(); |
||||
|
cell2.setContent("+++++++" + i); |
||||
|
PdfUtil.Row row2 = new PdfUtil.Row(); |
||||
|
row2.addCell(cell2); |
||||
|
contents.add(row); |
||||
|
contents.add(row2); |
||||
|
} |
||||
|
PdfUtil.credatePdf("/home/", "评测", rows, contents); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue