java poi pdf 导出
(java poi pdf导出 文字+图片两张放置一行)
思路:流传入图片 ,pdf没有行的概念,只有列即为一行,两张图片可以先建立一列在一列总再建立两列各放置一张图片 然后调整图片大小即可
入口:
@PostMapping(value = "/exportDIY")
@ResponseBody
@ApiOperation("多格式导出")
@ControllerLog(description = "多格式导出", logLevel = 6)
public ResponseResult exportDIY(HttpServletResponse response, @RequestBody Map<String, String> request) {
return logService.exportDIY(response, request);
}
主题:
这部分读入流图片写成图片,然后再填充到pdf文件中,临时文件可考虑删除,一直就只有一张。
FileOutputStream fileOutputStream1 = null;
FileOutputStream fileOutputStream2 = null;
ByteArrayInputStream dataChartStringin1 = null;
ByteArrayInputStream dataChartStringin2 = null;
try {
String filename = fileName + ".pdf";
response.setContentType("application/pdf;chartset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
Rectangle rectPageSize = new Rectangle(PageSize.A4); // 定义A4页面大小
rectPageSize = rectPageSize.rotate();
Document document = new Document(rectPageSize, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
PdfPageEventHelper pdfPageEventHelper = new PdfPageEventHelper();
pdfPageEventHelper.onEndPage(writer, document);
document.open();
addPdfTitleMsg(request, document);
// 获取流
BASE64Decoder base64Decoder = new BASE64Decoder();
String dataChartString1 = URLDecoder.decode(request.get("image1").substring(22), "UTF-8").replaceAll(" ", "+");
dataChartStringin1 = new ByteArrayInputStream(base64Decoder.decodeBuffer(dataChartString1)); //将picInfoByte作为输入流;
//读文件
byte[] b1 = new byte[128];
String path1 = new ClassPathResource("temp/temp1.png").getFile().getPath();
File file1 = new File(path1);
fileOutputStream1 = new FileOutputStream(file1);
while (dataChartStringin1.read(b1) != -1) {
fileOutputStream1.write(b1);
}
Image image1 = Image.getInstance(path1);
image1.scaleAbsolute(280, 120); // 设置图片大小
image1.setAccessibleAttribute(PdfName.ALT, new PdfString("temp1"));
String dataChartString2 = URLDecoder.decode(request.get("image2").substring(22), "UTF-8").replaceAll(" ", "+");
dataChartStringin2 = new ByteArrayInputStream(base64Decoder.decodeBuffer(dataChartString2)); //将picInfoByte作为输入流;
byte[] b2 = new byte[128];
String path2 = new ClassPathResource("temp/temp1.png").getFile().getPath();
File file2 = new File(path2);
fileOutputStream2 = new FileOutputStream(file2);
while (dataChartStringin2.read(b2) != -1) {
fileOutputStream2.write(b2);
}
Image image2 = Image.getInstance(path2);
image2.scaleAbsolute(280, 120); // 设置图片大小
image2.setAccessibleAttribute(PdfName.ALT, new PdfString("temp2"));
// 以上图片 完成
PdfPTable headerTable = new PdfPTable(2);
PdfPCell acell = new PdfPCell(image1, false);
removeBorder(acell);
headerTable.addCell(acell);
PdfPCell rCell = new PdfPCell(image2, false);
removeBorder(rCell);
headerTable.addCell(rCell);
//将主要的表格headerTable加入document文档对象中
document.add(headerTable);
document.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream1 != null) {
fileOutputStream1.close();
}
if (dataChartStringin1 != null) {
dataChartStringin1.close();
}
if (fileOutputStream2 != null) {
fileOutputStream2.close();
}
if (dataChartStringin2 != null) {
dataChartStringin2.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void addPdfTitleMsg(Map<String, String> request, Document document) throws DocumentException {
PdfUtil.addElement("终端审计日志统计", Element.ALIGN_CENTER, document);
PdfPTable table = new PdfPTable(1);
Font font = new Font(FontUtil.getBfChinese(), 12);
// 换行
document.add(new Chunk(("\n")));
// 时间
PdfUtil.pcellColorNoBorder(table, new Paragraph(getTimeMsg(request), font));
document.add(table);
}
/**
* PDF写入每个列字段,在列内居左,无边框
*
* @param table
* @param p
*/
public static void pcellColorNoBorder(PdfPTable table, Paragraph p) {
PdfPCell cell_color = new PdfPCell(p);
cell_color.setVerticalAlignment(Element.ALIGN_LEFT);// 字居中
cell_color.setBorder(PdfPCell.NO_BORDER);
table.addCell(cell_color);
}
以上postman自测OK
以上。