最近需要做一个生成pdf的功能,有很多工具,最终还是选择了itextpdf,废话就不多说了 直接撸代码。。。
xmlworker-5.5.11.jar
itextasian-5.5.9.jar
itextpdf-5.5.9.jar
$("#export_pdf").off("click").on("click",function(){
//隐藏表单下载
var form = $("
@RequestMapping("exportEnterpriseInfo")
@ResponseBody
public void exportEnterpriseInfo(HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获取所有参数
String data = request.getParameter("data");
System.out.println(data);
// 设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode("详情.pdf", "UTF-8"));
// CreateDoc.create(response,JSON.parseObject(data));
//pdf保存路径
String filePath = BaseInfo.getRealPath()+"/resources/gzmh/pdf/test.pdf";
CreateDoc.create(response,filePath,JSON.parseObject(data));
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
* pdf报告类
* @author pc
*
*/
public class CreateDoc {
/**
* 生成表格
* @param user ZDR
* @param formData 表格数据
* @param chartData 图表数据
* @param textData 文本数据
* @param param 其它参数
* @return
* @throws Exception
*/
public static String create(HttpServletResponse response, String filePath, JSONObject data) throws Exception {
// Image image = Image.getInstance(IMG);
File file = new File(filePath);
//生成PDF文档
Document document = new Document(PageSize.A4);
PdfWriter.getInstance(document,new FileOutputStream(file));
document.open();
//背景图
Image tImgCover = Image.getInstance(BaseInfo.getRealPath()+"/url.png");
/* 设置图片的位置 */
tImgCover.setAbsolutePosition(0, 0);
/* 设置图片的大小 */
tImgCover.scaleAbsolute(595, 842);
document.add(tImgCover);
document.addAuthor("作者名");//作者
document.addCreationDate();//创建时间
document.addCreator("http://baidu.com");//创建者
document.addTitle("报告");//标题
document.addSubject("报告");//主题
document.addKeywords("iText 生成PDF 纯代码实现 表格 等效果 ");//关键字
//顶部报告名称
String entname = data.getJSONObject("companyinfo").getString("entname");
if(entname == null || StringUtils.isEmpty(entname)) {
return null;
}
Paragraph paragraph = new Paragraph("", iTextPDFUtil.getColorFont(BaseColor.RED,19,"宋体"));
paragraph.setLeading(50f);// 行间距
paragraph.setAlignment(Element.ALIGN_CENTER);
Chunk chunk1 = new Chunk(entname+"\n",iTextPDFUtil.getColorFont(BaseColor.BLACK,30,"宋体"));
paragraph.add(chunk1);
document.add( Chunk.NEWLINE);
Chunk chunk3 = new Chunk("信息报告\n\n\n\n\n\n\n\n\n\n\n\n",iTextPDFUtil.getColorFont(BaseColor.BLACK,30,"宋体"));
paragraph.add(chunk3);
//报告日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
Chunk chunk2 = new Chunk("日期:"+date,iTextPDFUtil.getColorFont(BaseColor.BLACK,30,"宋体"));
paragraph.add(chunk2);
document.add(paragraph);
document.newPage();
/**
* 解析行政数据 生成表格
* @param document
* @param cateTitle
* @param job
* @throws Exception
*/
public static void createtable(Document document,String cateTitle,JSONObject job) throws Exception {
if("基本信息".equals(cateTitle)) {
JSONObject basicInfo = job.getJSONObject("companyinfo");
System.out.println("基本信息"+JSON.toJSONString(basicInfo));
getBasicData(document,"基本信息",basicInfo);
}
}
/**
* 解析基本信息
* @param document
* @param title
* @param info
* @throws DocumentException
*/
public static void getBasicData(Document document,String title,JSONObject basicInfo) throws DocumentException {
document.add(iTextPDFUtil.addTableGroupLine(title));
PdfPTable tableBaseInfo = new PdfPTable(2);
tableBaseInfo.setWidthPercentage(100);
tableBaseInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
//基本信息
//报告基础信息说明
document.add(new Paragraph("信息名称:"+(basicInfo.getString("entname")==null?"无":basicInfo.getString("entname")),iTextPDFUtil.getColorFont(BaseColor.BLACK,12,"宋体")));
document.add( Chunk.NEWLINE );
document.newPage();
}
/**
* 调用解析方法
* @param document
* @param title
* @param info
* @throws Exception
*/
public static void getTableData(Document document,String title,JSONArray info) throws Exception {
document.add(iTextPDFUtil.addTableGroupLine(title));
PdfPTable tableBaseInfo = new PdfPTable(2);
PdfPCell cellBaseInfo = new PdfPCell();
tableBaseInfo.setWidthPercentage(100);
tableBaseInfo.setHorizontalAlignment(Element.ALIGN_LEFT);
if("其他信息".equals(title)) {
getContentOther(tableBaseInfo,cellBaseInfo,info);
document.add(tableBaseInfo);
}
}
/**
* 其它信息解析
* @param tableBaseInfo
* @param cellBaseInfo
* @param info
* @throws Exception
*/
public static void getContentOther(PdfPTable tableBaseInfo,PdfPCell cellBaseInfo,JSONArray otherInfo) throws Exception {
/********循环部分************/
//创建表格
for (Object object : otherInfo) {
JSONObject jo = JSON.parseObject(object.toString());
//其它信息名称
iTextPDFUtil.addTableGroupTitle(tableBaseInfo,cellBaseInfo,jo.getString("action_name")==null?"无":jo.getString("action_name"));
//性别
cellBaseInfo = new PdfPCell(new Phrase("性别",iTextPDFUtil.getColorFont()));
tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
cellBaseInfo = new PdfPCell(new Phrase(jo.getString("implement_institution")==null?"无":jo.getString("implement_institution"),iTextPDFUtil.getColorFont()));
tableBaseInfo.addCell(iTextPDFUtil.addBaseCell(cellBaseInfo));
tableBaseInfo.setWidths(iTextPDFUtil.getColumnWiths(10f,30f));
//核查建议
// iTextPDFUtil.addSuggestLine(tableBaseInfo, cellBaseInfo, "该客户身份信息核验正常",new BaseColor(79,170,246));
//添加空行
tableBaseInfo.addCell(iTextPDFUtil.addBlankLine(20,2));
}
}
/**
* 添加水印
* @param response
* @param file
* @throws Exception
*/
public static void addWaterMark(HttpServletResponse response, File file) throws Exception {
FileInputStream input1 = new FileInputStream(file);
ServletOutputStream output2 = response.getOutputStream();
// FileOutputStream output2 = new FileOutputStream(new File(DEST));
iTextPDFUtil.stringWaterMark(input1, output2, "水印名称", 7, 6, 0.1f,45, 16,BaseColor.BLACK);//文字水印
// iTextPDFUtil.imageWaterMark(input1, output2, IMG, 0.2f);
}
}
import java.io.FileInputStream;
import java.io.OutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.codec.Base64.InputStream;
public class iTextPDFUtil {
/*
* @Description 蓝色背景色标题内容行添加
* @param table 表格
* @param cell 列
* @param text 文本
* @return void
**/
public static void addTableGroupTitle(PdfPTable table, PdfPCell cell, String text) {
cell = new PdfPCell(new Phrase(text,getColorFont(BaseColor.WHITE)));
table.addCell(addTitleCell(cell,25,new BaseColor(69,153,241),2,false));
}
/**
* @Description 蓝色背景色标题内容行添加
* @param table 表格
* @param cell 列
* @param text 文本
* @param colspan 需要合并的列
* @return void
**/
public static void addTableGroupTitle(PdfPTable table, PdfPCell cell, String text,int colspan) {
cell = new PdfPCell(new Phrase(text,getColorFont(BaseColor.WHITE)));
table.addCell(addTitleCell(cell,25,new BaseColor(69,153,241),colspan,false));
}
/**
* @Description 核查建议
* @param table 表格
* @param cell 列
* @param suggestText 核查建议内容
* @param fontColor 核查建议内容文字颜色
* @return com.itextpdf.text.Element
**/
public static void addSuggestLine(PdfPTable table,PdfPCell cell,String suggestText,BaseColor fontColor) throws Exception {
addSuggestLine(table, cell, suggestText, fontColor, 0,10f,30f);
}
/**
* @Description 核查建议
* @param table 表格
* @param cell 列
* @param suggestText 核查建议内容
* @param fontColor 核查建议内容文字颜色
* @param colspan 合并的列
* @param widths 列所占宽
* @return com.itextpdf.text.Element
**/
public static void addSuggestLine(PdfPTable table,PdfPCell cell,String suggestText,BaseColor fontColor,int colspan,float...widths) throws Exception {
cell = new PdfPCell(new Phrase("核查建议:",getColorFont()));
cell.setColspan(1);
table.addCell(addBaseCell(cell,23,new BaseColor(238,238,238),false));
cell = new PdfPCell(new Phrase(suggestText,getColorFont(fontColor)));
if(colspan>0){
cell.setColspan(colspan);
}
table.addCell(addBaseCell(cell,23,new BaseColor(238,238,238),false));
table.setWidths(getColumnWiths(widths));
}
/**
* @Description 信息分组table
* @param groupText 文本内容
* @return com.itextpdf.text.Element
**/
public static Element addTableGroupLine(String groupText) {
PdfPTable tableBaseInfoIndex = new PdfPTable(1);
tableBaseInfoIndex.setWidthPercentage(20);
PdfPCell cellBaseInfo = new PdfPCell(new Phrase(groupText,getColorFont()));
cellBaseInfo.setHorizontalAlignment(Element.ALIGN_CENTER);
tableBaseInfoIndex.addCell(addTitleCell(cellBaseInfo,28,new BaseColor(238,238,238),2,false));
tableBaseInfoIndex.addCell(addBlankLine(10,1));
return tableBaseInfoIndex;
}
/**
* @Description 指定颜色字体 默认处理中文显示
* @param color 字体颜色
* @param fontSize 字体大小
* @param fontFamily 字体
* @return com.itextpdf.text.Font
**/
public static Font getColorFont(BaseColor color, int fontSize, String fontFamily) {
Font font = new Font(getFont());
font.setColor(color);
if(fontSize>0&&(null!=fontFamily||!"".equals(fontFamily))){
font.setSize(fontSize);
font.setFamily(fontFamily);
}
return font;
}
/**
* @Description 指定颜色字体 默认处理中文显示
* @param color 字体颜色
* @return com.itextpdf.text.Font
**/
public static Font getColorFont(BaseColor color) {
return getColorFont(color, 0, null);
}
/**
* @Description 默认处理中文显示
* @return com.itextpdf.text.Font
**/
public static Font getColorFont() {
Font font = new Font(getFont());
return font;
}
/**
* @Description 指定列宽度
* @param widths 一个或多个
* @return float[]
**/
public static float[] getColumnWiths(float...widths){
float[] columnWidths = new float[widths.length];
for (int i = 0; i < widths.length; i++) {
columnWidths[i]=widths[i];
}
return columnWidths;
}
/**
* @Description 添加表头cell
* @param titleCell 要操作的cell
* @param fixedHeight 行高度
* @param baseColor 背景色
* @param colspan 合并的列数
* @param isBottomBorder 是否有下边框 true 有 fasle 没有
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell addTitleCell(PdfPCell titleCell,int fixedHeight,BaseColor baseColor,int colspan,boolean isBottomBorder){
titleCell.setColspan(colspan);
titleCell.setFixedHeight(fixedHeight);
titleCell.setUseVariableBorders(true);
titleCell.setUseAscender(true);
titleCell.setUseDescender(true);
titleCell.setBackgroundColor(baseColor);
if(isBottomBorder){
titleCell.setBorder(Rectangle.BOTTOM);
titleCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);
}else{
titleCell.setBorder(Rectangle.NO_BORDER);
}
titleCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
return titleCell;
}
/**
* @Description 添加空行
* @param fixedHeight 空行高度
* @param colspan 合并的列数
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell addBlankLine(int fixedHeight,int colspan){
PdfPCell blankLine = new PdfPCell();
blankLine.setFixedHeight(fixedHeight);
blankLine.setBorder(Rectangle.NO_BORDER);
blankLine.setColspan(colspan);
return blankLine;
}
/**
* @Description 添加默认cell
* @Date 2019/7/12 11:36
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell addBaseCell(PdfPCell baseCell){
baseCell.setFixedHeight(23);
baseCell.setUseVariableBorders(true);
baseCell.setUseAscender(true);
baseCell.setUseDescender(true);
baseCell.setBorder(Rectangle.BOTTOM);
baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);
baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
return baseCell;
}
/**
* @Description 添加cell
* @param isBottomBorder 是否有下边框 true 有 fasle 没有
* @Date 2019/7/12 11:36
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell addBaseCell(PdfPCell baseCell,boolean isBottomBorder){
baseCell.setFixedHeight(23);
baseCell.setUseVariableBorders(true);
baseCell.setUseAscender(true);
baseCell.setUseDescender(true);
if(isBottomBorder){
baseCell.setBorder(Rectangle.BOTTOM);
baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);
}else{
baseCell.setBorder(Rectangle.NO_BORDER);
}
baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
return baseCell;
}
/**
* @Description 添加cell
* @param baseCell 要操作的cell
* @param fixedHeight 行高
* @param color 背景色
* @param isBottomBorder 是否有下边框 true 有 fasle 没有
* @Date 2019/7/12 11:36
* @return com.itextpdf.text.pdf.PdfPCell
**/
public static PdfPCell addBaseCell(PdfPCell baseCell,int fixedHeight,BaseColor color,boolean isBottomBorder){
baseCell.setFixedHeight(fixedHeight);
baseCell.setUseVariableBorders(true);
baseCell.setUseAscender(true);
baseCell.setUseDescender(true);
if(null!=color){
baseCell.setBackgroundColor(color);
}
if(isBottomBorder){
baseCell.setBorder(Rectangle.BOTTOM);
baseCell.setBorderColorBottom(BaseColor.LIGHT_GRAY);
}else{
baseCell.setBorder(Rectangle.NO_BORDER);
}
baseCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
return baseCell;
}
/**
* @Description 设置中文支持
* @Date 2019/7/11 10:33
* @Param []
* @return com.itextpdf.text.pdf.BaseFont
**/
public static BaseFont getFont() {
BaseFont bf = null;
try {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
System.out.println("Exception = " + e.getMessage());
}
return bf;
}
/**
* 斜角排列、全屏多个重复的花式文字水印
*
* @param input1 需要加水印的PDF读取输入流
* @param output 输出生成PDF的输出流
* @param waterMarkString 水印字符
* @param xAmout x轴重复数量
* @param yAmout y轴重复数量
* @param opacity 水印透明度
* @param rotation 水印文字旋转角度,一般为45度角
* @param waterMarkFontSize 水印字体大小
* @param color 水印字体颜色
*/
public static void stringWaterMark(FileInputStream input1, OutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {
try {
PdfReader reader = new PdfReader(input1);
PdfStamper stamper = new PdfStamper(reader, output);
// 添加中文字体
BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() + 1;
PdfContentByte over;
// 给每一页加水印
for (int i = 1; i < total; i++) {
Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);
// 计算水印每个单位步长X,Y
float x = pageRect.getWidth() / xAmout;
float y = pageRect.getHeight() / yAmout;
over = stamper.getOverContent(i);
PdfGState gs = new PdfGState();
// 设置透明度为
gs.setFillOpacity(opacity);
over.setGState(gs);
over.saveState();
over.beginText();
over.setColorFill(color);
over.setFontAndSize(baseFont, waterMarkFontSize);
for (int n = 0; n < xAmout + 1; n++) {
for (int m = 0; m < yAmout + 1; m++) {
over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x * n, y * m, rotation);
}
}
over.endText();
}
stamper.close();
} catch (Exception e) {
new Exception("NetAnd PDF add Text Watermark error"+e.getMessage());
}
}
/**
* 图片水印,整张页面平铺
* @param input 需要加水印的PDF读取输入流
* @param output 输出生成PDF的输出流
* @param imageFile 水印图片路径
*/
public static void imageWaterMark(InputStream input, OutputStream output, String imageFile, float opacity) {
try {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, output);
Rectangle pageRect = stamper.getReader().getPageSize(1);
float w = pageRect.getWidth();
float h = pageRect.getHeight();
int total = reader.getNumberOfPages() + 1;
Image image = Image.getInstance(imageFile);
image.setAbsolutePosition(0, 0);// 坐标
image.scaleAbsolute(w, h);
PdfGState gs = new PdfGState();
gs.setFillOpacity(opacity);// 设置透明度
PdfContentByte over;
// 给每一页加水印
float x, y;
Rectangle pagesize;
for (int i = 1; i < total; i++) {
pagesize = reader.getPageSizeWithRotation(i);
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
over = stamper.getOverContent(i);
over.setGState(gs);
over.saveState();//没这个的话,图片透明度不起作用,必须在beginText之前,否则透明度不起作用,会被图片覆盖了内容而看不到文字了。
over.beginText();
// 添加水印图片
over.addImage(image);
}
stamper.close();
} catch (Exception e) {
new Exception("NetAnd PDF add image Watermark error" + e.getMessage());
}
}
/**
* @description 顶部表格卡片形式显示格式数据组装
* @param tableMobileHeader 要操作的表格
* @param cellMobileHeader 要操作的单元格
* @param clospan 合并列 不需要合并填写0
* @param fixedHeight 行高
* @param padding 间距
* @param border 边框
* @param borderColor 边框颜色
* @param backgroundColor 背景色
* @param vertical 垂直对齐方式
* @param horizontal 水平对齐方式
* @return void
**/
public static void addTableHeaderData(PdfPTable tableMobileHeader, PdfPCell cellMobileHeader, int clospan, float fixedHeight, int padding, int border, BaseColor borderColor, BaseColor backgroundColor, int vertical, int horizontal) {
cellMobileHeader.setUseBorderPadding(true);
cellMobileHeader.setUseAscender(true);
if(clospan>0){
cellMobileHeader.setColspan(clospan);
}
cellMobileHeader.setUseDescender(true);
cellMobileHeader.setFixedHeight(fixedHeight);
cellMobileHeader.setPadding(padding);
cellMobileHeader.setVerticalAlignment(vertical);
cellMobileHeader.setHorizontalAlignment(horizontal);
if(null!=backgroundColor){
cellMobileHeader.setBackgroundColor(backgroundColor);
}
cellMobileHeader.setBorder(border);
cellMobileHeader.setBorderColor(borderColor);
tableMobileHeader.addCell(cellMobileHeader);
}
/**
* @description 顶部表格卡片形式显示格式数据组装
* @param tableMobileHeader 要操作的表格
* @param cellMobileHeader 要操作的单元格
* @param clospan 合并列 不需要合并填写0
* @param backgroundColor 背景色
* @return void
**/
public static void addTableHeaderData(PdfPTable tableMobileHeader, PdfPCell cellMobileHeader, int clospan,BaseColor backgroundColor) {
addTableHeaderData(tableMobileHeader, cellMobileHeader, clospan, 100, 10, 30, BaseColor.WHITE, backgroundColor, 0, 0);
}
}