package com.toher.project.openpdf;
/**
* @Author 李怀明
* @Date 2024/7/28 12:35 AM
* @Version 1.0
*/
import com.lowagie.text.Font;
import com.lowagie.text.*;
import com.lowagie.text.Image;
import com.lowagie.text.alignment.HorizontalAlignment;
import com.lowagie.text.alignment.VerticalAlignment;
import com.lowagie.text.html.simpleparser.HTMLWorker;
import com.lowagie.text.pdf.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.Map;
@Service
public class OpenPdfService {
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public byte[] generatePdf() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
// 创建PDF文档
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, out);
//如果需要定义字体,将自己的字体放在 resources/fonts目录下
//BaseFont font = BaseFont.createFont("fonts/Viaoda_Libre/ViaodaLibre-Regular.ttf", BaseFont.IDENTITY_H, false);
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font docFont = new Font(bfChinese, 10, Font.UNDEFINED, Color.BLACK);
//设置水印
writer.setPageEvent(new PdfPageEventHelper() {
@Override
public void onEndPage(PdfWriter writer, Document document) {
PdfContentByte waterMar = writer.getDirectContentUnder();
String text = "Micro麦可乐";
addTextFullWaterMark(waterMar, text, bfChinese);
}
});
// 设置边距
document.setMargins(20, 20, 20, 20);
// 打开文档
document.open();
/**
* 01 表格演示
*/
String[] tableTitle = new String[]{"清单", "数量", "单价", "总价"};
Table table = new Table(tableTitle.length);
table.setWidths(new float[]{70, 10, 10, 10});
// 设置表格前的间距
table.setSpacing(0);
// 设置表格在页面中所占的宽度百分比
table.setWidth(100);
table.setBorder(0);
//模拟5行表格数据
for (int row = 0; row < 5; row++) {
for (int i = 0; i < tableTitle.length; i++) {
Chunk chunk;
if (row == 0) {
chunk = new Chunk(tableTitle[i], docFont);
} else {
chunk = new Chunk(row + "行 模拟数据" + i, docFont);
}
// 建立单元格
Cell cell = new Cell(chunk);
// 设置水平对齐
cell.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置垂直对齐
cell.setVerticalAlignment(VerticalAlignment.CENTER);
table.addCell(cell);
}
}
document.add(table);
/**
* 02 写入图片
*/
byte[] byteArray = new byte[0];
InputStream inputStream = this.getClass().getResourceAsStream("/static/img/test.png");
if (inputStream != null) {
byteArray = new byte[inputStream.available()];
inputStream.read(byteArray);
}
Image image = Image.getInstance(byteArray);
// 图片进行缩放
image.scaleAbsolute(200, 200);
document.add(image);
/**
* 03 写入html模版内容
*/
HTMLWorker htmlWorker = new HTMLWorker(document);
String html = "
Hello, micro
";
htmlWorker.parse(new StringReader(html));
// 关闭文档
document.close();
return out.toByteArray();
}
public static void addTextFullWaterMark(PdfContentByte waterMar, String text, BaseFont bfChinese) {
waterMar.beginText();
PdfGState gs = new PdfGState();
// 设置填充字体不透明度为0.2f
gs.setFillOpacity(0.2f);
waterMar.setFontAndSize(bfChinese, 40);
// 设置透明度
waterMar.setGState(gs);
// 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
for (int x = 0; x <= 700; x += 200) {
for (int y = 0; y <= 800; y += 200) {
waterMar.showTextAligned(Element.ALIGN_RIGHT, text, x, y, 35);
}
}
// 设置水印颜色
waterMar.setColorFill(Color.GRAY);
//结束设置
waterMar.endText();
waterMar.stroke();
}
}