java⽣成pdf图表_Highcharts图表导出为pdf的JavaWeb实践packagecom.demo.utils;importjava.io.ByteArrayOutputStream;importjava.io.IOException;importjava.io.StringReader;importjava.io @Description XXX分析页⾯PDF导出⼯具⽅法*/
public classComprehensivePdfUtil {/*** 获得PDF字节输出流及pdf布局业务逻辑
*@paramrequest
*@paramrespon
*@paramresultMap 包含参数:svg(绘图svg参数及hicharts图布局参数) logoT(页⾯总标题)
*@paramlist 页⾯包含植⼊栏⽬排⾏表格图,该list存储绘制表格所⽤的数据
*@paramtableTh 页⾯包含植⼊栏⽬排⾏表格图,该字符串作为表格表头
*@paramtableTd 页⾯包含植⼊栏⽬排⾏表格图,该字符串作为表格内容填充时,实体类反射值所⽤的⽅法名(必须与实体⽅法严格⼀致)
*@return
*/
publicByteArrayOutputStream getPDFStream(HttpServletRequest request,
HttpServletRespon respon,
MapresultMap){try{//图⽚变量定义
String noData = "/style/images/noData.png";//⽆数据左右图
String noDataCenter = "/style/images/noDataCenter.png";//⽆数据中间图
String waterMark = "/style/images/PDFSHUIYIN.png";//PDF导出⽂件⽔印图⽚
String [] svgName = (String[]) ("svg");//导出PDF页⾯所有svg图像
Document document = newDocument();
ByteArrayOutputStream buffer= newByteArrayOutputStream();
PdfWriter Instance(document, buffer);//设置页⾯⼤⼩
int pageHeight = 2000;
Rectangle rect= new Rectangle(0,0,1200,pageHeight);
rect.tBackgroundColor(new BaColor(248,248,248));//页⾯背景⾊
document.tPageSize(rect);//页⾯参数//页边空⽩
document.tMargins(20, 20, 30, 20);
document.open();//设置页头信息
if(null!=("logoT")){
BaFont bfChine= ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED);
Font FontChine= new Font(bfChine,20, Font.BOLD);
Paragraph paragraph= new Paragraph((("logoT"),FontChine);
paragraph.tAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
PdfPTable table= null;
String ContextPath();
String baPath= Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";//开始循环写⼊svg图像到pdf⽂档对象
for(String str:svgName){//
//positionAndSvg数组中元素说明://positionAndSvg[0]表⽰svg图像所在页⾯的div的id//positionAndSvg[1]表⽰svg图像在⽔平⽅
向的相对位置://1.left(⽔平⽅向两张图,居左且占⽐50%)//2.right(⽔平⽅向两张图,居右且占⽐50%)//3.center(⽔平⽅向⼀张图,居中且
占⽐100%)//positionAndSvg[2]表⽰svg图像模块的标题如:xxx⾛势图//positionAndSvg[3]表⽰soft/hard即软⼴图或者硬⼴图,当⽆数据时为⽆数据提⽰效果图提供判断依据//positionAndSvg[4]表⽰svg图像元素,形如
// String[] positionAndSvg = str.split("___");
Image image1= null;boolean havaData = true;if("noData".equals(positionAndSvg[4])){//⽆数据时
image1 = Instance(baPath+noData);
havaData= fal;
}el{//有数据
image1 = Instance(highcharts(request,respon,positionAndSvg[4]).toByteArray());
havaData= true;
}if("left".equals(positionAndSvg[1])){
创新素材
String title1= URLDecoder.decode(positionAndSvg[2],"utf-8");
tTitleByCharts(document,30,title1,"",0,87,55,Element.ALIGN_LEFT,headfont);if(!"cooperateProporOne".equals(positionAndSv {
tTitleByCharts(document,0,"左图","右图",248,248,248,Element.ALIGN_CENTER,blackTextFont);
}el{
tTitleByCharts(document,0,"","",248,248,248,Element.ALIGN_CENTER,blackTextFont);
}
table= new PdfPTable(2);float[] wid ={0.50f,0.50f}; //列宽度的⽐例
table.tWidths(wid);
table=PdfPTableImage(table,image1,80f);
}el if("right".equals(positionAndSvg[1])){
table=PdfPTableImage(table,image1,80f);
table.tSpacingBefore(10);
四年级阅读理解table=tTableHeightWeight(table,360f,1000);周公解梦梦见捡钱
document.add(table);
table= null;
}el if("center".equals(positionAndSvg[1])){//总览全局
String title1 = URLDecoder.decode(positionAndSvg[2],"utf-8");
tTitleByCharts(document,30,title1,"",0,87,55,Element.ALIGN_LEFT,headfont);
tTitleByCharts(document,0,"","",248,248,248,Element.ALIGN_CENTER,blackTextFont);
table= new PdfPTable(1);float[] wid ={1.00f}; //列宽度的⽐例
table.tWidths(wid);if(havaData){
table=PdfPTableImageTable(table,image1,1000f,600f);
}el{
table= PdfPTableImageTable(Instance(baPath+noDataCenter),1000f,600f);
}
table=tTableHeightWeight(table,400f,1000);
document.add(table);
table=null;
}
}//添加⽔印Start---------------------------------------------------------------------------------------------
PdfFileExportUtil pdfFileExportUtil = newPdfFileExportUtil();
pdfWriter.w
PictureWaterMarkPdfPageEvent(baPath+waterMark));//pdfWriter.w
TextWaterMarkPdfPageEvent("xxx科技"));//添加⽔印End-----------------------------------------------------------------------------------------------
document.clo();returnbuffer;
}catch(BadElementException e) {
e.printStackTrace();return null;
}catch(MalformedURLException e) {enables
e.printStackTrace();return null;
}catch(DocumentException e) {
e.printStackTrace();return null;
}catch(IOException e) {
e.printStackTrace();return null;
}catch(Exception e) {
e.printStackTrace();return null;
}
}/*** 设置图⽚类型Cell属性
*@paramtable不若初见
*@paramimage1
*@paramimgPercent
*@return*@throwsException*/
private PdfPTable PdfPTableImage(PdfPTable table,Image image1,floatimgPercent){
table=uTable(table,Element.ALIGN_CENTER);
PdfPCell cellzr=createCellImage(image1,imgPercent);
井漏cellzr.tBorder(0);
cellzr.tBackgroundColor(new BaColor(248,248,248));
table.addCell(cellzr);returntable;
}/*** 设置图⽚类型Table的Cell属性
*@paramtable
*@paramimage1
*@paramimgPercentWidth
*@paramimgPercentHeight
*@return*@throwsException*/
private PdfPTable PdfPTableImageTable(PdfPTable table,Image image1,float imgPercentWidth,floatimgPercentHeight){
table=uTable(table,Element.ALIGN_CENTER);
PdfPCell cellzr=createCellImageTable(image1,imgPercentWidth,imgPercentHeight);
cellzr.tBorder(0);
cellzr.tBackgroundColor(new BaColor(248,248,248));
table.addCell(cellzr);returntable;
}/*** 设置表头
*@paramdocument
*@paramSpacingBefore
*@paramtitle1
*@paramtitle2
*@paramr1
*@paramr2
*@paramr3
*@paramele
*@paramfont
*@throwsException*/
private void tTitleByCharts(Document document,int SpacingBefore,String title1,String title2,int r1,int r2,int r3,intele,Font font){try{float[] titlewidthsLeft = {0.50f,0.50f};
PdfPTable zrfbtitleTable=createTable(titlewidthsLeft);
PdfPCell cellzr=createCellLeft(title1,font,ele);
cellzr.tBorder(0);
cellzr.tBackgroundColor(newBaColor(r1,r2,r3));
zrfbtitleTable.addCell(cellzr);
PdfPCell cellzr1=createCellLeft(title2,font,ele);
cellzr1.tBorder(0);
cellzr1.tBackgroundColor(newBaColor(r1,r2,r3));
zrfbtitleTable.addCell(cellzr1);
zrfbtitleTable.tSpacingBefore(SpacingBefore);
zrfbtitleTable=tTableHeightWeight(zrfbtitleTable,30f,1000);
document.add(zrfbtitleTable);
}catch(DocumentException e) {
e.printStackTrace();
}
}/*** 导出Pdf所⽤字体静态变量*/
private static Font headfont ;//title字体
private static Font blackTextFont ;//⿊⾊字体
学校工会工作报告
private staticFont colorfont;int maxWidth = 500;static{消防队扑救火灾是否收取费用
BaFont bfChine;try{
bfChine= ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.NOT_EMBEDDED); headfont= new Font(bfChine, 15, Font.BOLD);//设置字体⼤⼩
headfont.tColor(BaColor.WHITE);
blackTextFont= new Font(bfChine, 11, Font.BOLD);//设置字体⼤⼩
blackTextFont.tColor(BaColor.BLACK);
colorfont= new Font(bfChine, 11, Font.NORMAL);//设置字体⼤⼩
colorfont.tColor(BaColor.RED);
}catch(Exception e) {
e.printStackTrace();
}
}/*** 创建指定内容背景⾊的Table元素Cell
*@paramvalue
*@paramfont
*@paramc1
*@paramc2
*@paramc3