查了很多资料都没有我想要的pdf,于是根据iText基础知识(这里是看了宝爷的笔记点击打开链接)自行编辑了一个简单的表格pdf文档
运用spring、mybatis、CXF框架
SSM框架打印pdf:https://blog.csdn.net/weixin_40989555/article/details/106302423
需要的jar包:https://download.csdn.net/download/weixin_40989555/10487913
jar包已经解决中文的编码问题,可以直接使用。
若编写多行多列表格(maven依赖):
<!-- pdf打印 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!-- 设置pdf文件密码,还要引入下面bcprov-jdk15on.jar包:-->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.54</version>
</dependency>
话不多说,下面进行编码。
jsp页面
<!-- form里正文区 -->
<form class="" action="" id="form_delayWarn" method="post">
<div class="modal-body">
<div class="" id="delayExp" style="display:none">
<div class="panel-title">
<div class="panel-title01">
xxxxx提醒</div>
</div>
<div class="panel-heading">
<div class="row">
<div class="col-sm-12">
<div class="col-xs-12 col-md-10">
<textarea id="getresult1" name="getresult1" rows="3" class="form-control" readonly></textarea>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- form里按钮区 -->
<div class="modal-footer">
<button type="button" class="unified-bigbotton-blue" οnclick="pdfPrint()">是</button>
<button type="button" class="unified-bigbotton-blue" data-dismiss="modal">否</button>
</div>
</form>
js
/**
* 点击“是”进行PDF打印
*/
function pdfPrint(){
var aab001 = qdw_aab001;
document.getElementById("form_delayWarn").action = "./lrs/querydelay/delWarnPdfPrint.do?aab001=" + aab001;
return doConfirm("下载", function() {
document.getElementById("form_delayWarn").submit();
});
/* 直接新开一个页面显示不下载 (js2方法)
var url = "./lrs/querydelay/delWarnPdfPrint.do?aab001=" + aab001;
window.open(url, "xxxxxxx"); */
}
Controller层
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.itextpdf.text.Element;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
private QueryDelayServiceq qdService;
@SuppressWarnings("unchecked")
@RequestMapping(value = "/delWarnPdfPrint" , method = RequestMethod.POST)
@ResponseBody
public String delWarnPdfPrint(String aab001, HttpServletRequest req, HttpServletResponse resp) throws Exception {
log.info("xxxxPDF打印-返回数据");
//返回list集合数据
String alllist = qdService.delWarnPdfPrint(aab001);
File file = null;
InputStream fin = null;
ServletOutputStream out = null;
List<BC90> list = new ArrayList<BC90>();
List<DWPdf> pdflist = new ArrayList<DWPdf>();
try {
list = (List<BC90>) JacksonUtil.toList(alllist, ArrayList.class, BC90.class);//这是我自定义的String转list集合方法,跳过
for (BC90 bc90 : list) {
DWPdf dpdf = new DWPdf();
dpdf.setRownum(bc90.getRownum());
dpdf.setAac003(bc90.getAac003());
dpdf.setAac002(bc90.getAac002());
dpdf.setAbc939(bc90.getAbc939());
dpdf.setAbc913(bc90.getAbc913());
dpdf.setAbc914(bc90.getAbc914());
dpdf.setAbc909(bc90.getAbc909());
pdflist.add(dpdf);
}
} catch (Exception e) {
e.printStackTrace();
log.error("逾期合同json转换出错",e);
}
/*******pdf文件内容生成开始*********/
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
//创建文件夹
String filePar = null;
Date nowTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd.hh.mm.ss");
String time = sdf.format(nowTime);//获取当前时间字符串形式
filePar = "C:/企业逾期合同"+time;// 文件夹路径
File myPath = new File( filePar );
file = new File(filePar+"/"+"企业逾期合同.pdf");
if (!myPath.exists()){
myPath.mkdir();
}
/*
* import com.lowagie.text.pdf.PdfWriter;
*/
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));// throws FileNotFoundException,
// DocumentException
document.open();
/*
* 设中文字体
*/
BaseFont bfChinese = null;// FontFactory.getFont(FontFactory.COURIER,
// 14, Font.BOLD, new CMYKColor(0, 255, 0, 0);//大小,粗细,颜色
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
} catch (IOException e) {
e.printStackTrace();
}
//定义中文字体大小
Font f10 = new Font(bfChinese, 10, Font.NORMAL);
Font f12 = new Font(bfChinese, 12, Font.NORMAL);
Font f26 = new Font(bfChinese, 26, Font.NORMAL);//一号字体
/*
* 创建标题
*/
Paragraph title1 = new Paragraph("标 题", f26);
title1.setAlignment(Element.ALIGN_CENTER);
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Section section1 = chapter1;
// 创建有7列的表格
int colNumber = 7;
PdfPTable t = new PdfPTable(colNumber);
t.setSpacingBefore(25);//设置段落上空白
t.setSpacingAfter(25);//设置段落上下空白
t.setHorizontalAlignment(Element.ALIGN_CENTER);// 居左
float[] cellsWidth = { 0.08f, 0.1355f, 0.2f, 0.3f, 0.2f, 0.2f , 0.145f }; // 定义表格的宽度
t.setWidths(cellsWidth);// 单元格宽度
t.setTotalWidth(500f);//表格的总宽度
t.setWidthPercentage(100);// 表格的宽度百分比
//设置表头
PdfPCell c1 = new PdfPCell(new Paragraph("序号",f12));
t.addCell(c1);
PdfPCell c2 = new PdfPCell(new Paragraph("姓名",f12));
t.addCell(c2);
PdfPCell c3 = new PdfPCell(new Paragraph("身份证号",f12));
t.addCell(c3);
PdfPCell c4 = new PdfPCell(new Paragraph("单位名称",f12));
t.addCell(c4);
PdfPCell c5 = new PdfPCell(new Paragraph("合同开始日期",f12));
t.addCell(c5);
PdfPCell c6 = new PdfPCell(new Paragraph("合同结束日期",f12));
t.addCell(c6);
PdfPCell c7 = new PdfPCell(new Paragraph("合同状态",f12));
t.addCell(c7);
//向表格类动态填充list集合数据
for(int i=0;i<pdflist.size();i++){
DWPdf dpdf = pdflist.get(i);
t.addCell(new Paragraph(dpdf.getRownum(),f10));
t.addCell(new Paragraph(dpdf.getAac003(),f10));
t.addCell(new Paragraph(dpdf.getAac002(),f10));
t.addCell(new Paragraph(dpdf.getAbc939(),f10));
t.addCell(new Paragraph(dpdf.getAbc913().toString(),f10));
t.addCell(new Paragraph(dpdf.getAbc914().toString(),f10));
t.addCell(new Paragraph(dpdf.getAbc909(),f10));
}
section1.add(t);
document.add(chapter1);
document.close();
/*******pdf文件生成结束*********/
try {
fin = new FileInputStream(file);
resp.setCharacterEncoding("utf-8");
resp.setContentType("application/pdf");
String filename = "**********";
// 设置浏览器以下载的方式处理该文件
if (req.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
filename = URLEncoder.encode(filename, "UTF-8");
} else {
filename = new String(filename.getBytes("UTF-8"), "ISO8859-1");
}
//下载使用 删除可用(js2方法)直接在页面显示不下载
resp.addHeader("Content-Disposition", "attachment;filename=" + filename + ".pdf");//这里填想要的文件格式
out = resp.getOutputStream();
byte[] buffer = new byte[512]; // 缓冲区
int bytesToRead = -1;
// 通过循环将读入的Word文件的内容输出到浏览器中
while ((bytesToRead = fin.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
} finally {
if (fin != null)
fin.close();
if (out != null)
out.close();
if (file != null)
file.delete(); // 删除临时文件
if (myPath != null)
myPath.delete();
}
return null;
}
Service层
public String delWarnPdfPrint(String aab001) {
if(aab001 != null){
List<BC90> delWrList = BC90Mapper.delWarnPdfPrint(aab001);
Map<String, Object> pdfMap = new TreeMap<String, Object>();
Map<String, Object> allMap = new TreeMap<String, Object>();
allMap.put("delWrList", delWrList);
return JsonUtil.toJson(delWrList);//这里是自定义方法转译维json字符串返回数据
}
return respErrorMsg("请重新登入系统或刷新页面");
}
mapper层
List<BC90> delWarnPdfPrint(String aab001);
写了那么多来看看效果吧