使用java生成PDF并保存到本地服务器中
1.导入maven
<!-- PDF工具包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- pdf中文处理 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2.生成所需的PDF文件
public Map pdf(Long id) throws IOException, DocumentException {
Map map = new HashMap();
String fileName = "pdfFile.pdf";
Document document = new Document(PageSize.A4); //纵向分布
String path="src/main/webapp/PDF/"; //绝对路径
File file=new File(path);
if (!file.exists()){
file.mkdirs();
}
File pdfFile=new File(path,fileName);
PdfWriter pdfWriter=PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
//data为需要创建PDF的数据
//创建一个有内容的表格文件
PdfPTable table = PdfUtil.createTable(data, document);
document.add(table); //添加表格到PDF中
//添加图片
//获取图片时,如果直接写url,有时候会拿不到图片,不清楚问题,因此这里将图片处理了下(ImageUtil.image2byte())
Image qrcode=Image.getInstance(ImageUtil.image2byte("url"));
qrcode.scaleToFit(45,45);
qrcode.setAbsolutePosition(500,760);
document.add(qrcode); //添加图片到PDF文件中
//中间横线
LineSeparator line=new LineSeparator(1f,100,BaseColor.GRAY,Element.ALIGN_CENTER,160f);
document.add(line); //添加横线
//顶部横线
LineSeparator lineTop=new LineSeparator(1f,110,BaseColor.BLACK,Element.ALIGN_CENTER,300f);
document.add(lineTop); //添加横线
//底部横线
LineSeparator lineBottom=new LineSeparator(1f,110,BaseColor.BLACK,Element.ALIGN_CENTER,-15f);
document.add(lineBottom); //添加横线
//左边竖线
PdfContentByte contentByte=pdfWriter.getDirectContent();
int x=10,y=493;
int x1=10,y2=807;
contentByte.saveState();
contentByte.moveTo(x,y);
contentByte.lineTo(x1,y2);
contentByte.stroke();
contentByte.restoreState(); //添加竖线
//右边竖线
PdfContentByte contentByte2=pdfWriter.getDirectContent();
int x3=585,y3=493;
int x4=585,y4=807;
contentByte2.saveState();
contentByte2.moveTo(x3,y3);
contentByte2.lineTo(x4,y4);
contentByte2.stroke();
contentByte2.restoreState();
document.close();
map.put("code",0);
map.put("url","url");
map.put("msg","OK");
return map;
}
3.创建PDF表格的方法
//设置空表格
PdfPTable table = new PdfPTable(2);//生成一个两列的表格
table.getDefaultCell().setBorderWidth(0f);
table.setWidthPercentage(100); //100%宽度填充
table.getDefaultCell().setPaddingTop(1f);
//设置中文字体
BaseFont baseFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont,10);
BaseFont titleFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
Font title=new Font(titleFont,15,Font.BOLD);
PdfPCell cell; //单元格
//1.1
//插入表格 --用于在单元格中插入表格
PdfPTable msgTable = new PdfPTable(3); //3列的表格
msgTable.setWidths(new int[]{400,300,300}); //设置每一列的宽度
PdfPCell msgCell;
msgCell = new PdfPCell(new Phrase("信息", title));
msgCell.setColspan(3); //合并3个单元格
msgCell.setFixedHeight(40f); //单元格的高度
msgCell.setUseAscender(true); //单元格居中
msgCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 设置水平靠左
msgCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
msgCell.disableBorderSide(15); //设置单元格的边框隐藏程度
msgTable.addCell(msgCell); //将单元格的内容添加到表中
msgCell = new PdfPCell(new Phrase("编号 ", font));
msgCell.setFixedHeight(25f);
msgCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // 设置水平居中
msgCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
msgCell.disableBorderSide(15);
msgTable.addCell(msgCell);
msgCell = new PdfPCell(new Phrase("信息 ", font));
msgCell.setColspan(2);
msgCell.setFixedHeight(25f);
msgCell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 设置水平居中
msgCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
msgCell.disableBorderSide(15);
msgTable.addCell(msgCell);
//msgTable设置了3列,因此,每一行的单元格(即3个单元格)必须要有内容,不然的话那一行不会显示出来
cell = new PdfPCell(msgTable); //把小表格添加到大表格的单元格中
cell.setFixedHeight(128);
cell.disableBorderSide(15);
//cell.setColspan(2);
table.addCell(cell); //把大单元格的内容添加到大表格中
//单元格中插入图片
//判断图片是否存在
if (!isImage()){
cell =new PdfPCell(new Phrase("无图片", font));
cell.setColspan(2);
cell.setFixedHeight(25f);
cell.disableBorderSide(15);
cell.setUseAscender(true); //单元格居中
cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 设置水平居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
}else {
Image image = Image.getInstance(ImageUtil.image2byte(url);
image.scaleToFit(45, 45); //修改图片大小,图片太大有时候会导致图片不显示,甚至整行不显示
cell.setColspan(2)
cell= new PdfPCell(image,true); //添加图片到单元格中
cell.setFixedHeight(25f);
cell.disableBorderSide(15);
cell.setUseAscender(true); //单元格居中
cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT); // 设置水平居中
cell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE); // 设置垂直居中
table.addCell(cell);
return table;
}
4.处理图片的工具类
public static byte[] image2byte(String path) throws IOException {
byte[] data = null;
URL url = null;
InputStream input = null;
try{
url = new URL(path);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
input = httpUrl.getInputStream();
}catch (Exception e) {
e.printStackTrace();
return null;
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
return data;
}