PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
Object o = datas.get(0);
String objName = o.getClass().getSimpleName();
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String csvFilePath = objName + dateFormat.format(new Date()) + ".pdf";
// 读取字符编码
String utf = "UTF-8";
// 设置响应
response.reset();
response.setContentType("application/octet-stream");
response.setCharacterEncoding(utf);
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(csvFilePath, utf));
OutputStream outputStream = response.getOutputStream();
PDPageContentStream contents = new PDPageContentStream(document, page);
PDFont font = PDType1Font.HELVETICA_BOLD;
// 添加图片
File imagePath = new File(filePath);
if (!imagePath.exists()) {
imagePath.mkdirs();
}
if (base64 != null) {
String picImage = decodeBase64(base64, imagePath);
PDImageXObject pdImage = PDImageXObject.createFromFile(picImage,document);
//Drawing the image in the PDF document
contents.drawImage(pdImage, 10, 280);
// delete image
if (picImage != null) {
File picFile = new File(picImage);
picFile.delete();
}
}
contents.beginText();
contents.setFont(font, 14);
contents.newLineAtOffset(10, 200);
contents.showText("Hello World");
// contentStream.drawString("中文");
contents.endText();
contents.close();
document.save(outputStream);
document.close();
outputStream.flush();
outputStream.close();