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
*@paramresponse
*@paramresultMap 包含参数:svg(绘图svg参数及hicharts图布局参数) logoT(页⾯总标题)
*@paramlist 页⾯包含植⼊栏⽬排⾏表格图,该list存储绘制表格所⽤的数据
*@paramtableTh 页⾯包含植⼊栏⽬排⾏表格图,该字符串作为表格表头
*@paramtableTd 页⾯包含植⼊栏⽬排⾏表格图,该字符串作为表格内容填充时,实体类反射值所⽤的⽅法名(必须与实体⽅法严格⼀致)
*@return
*/
publicByteArrayOutputStream getPDFStream(HttpServletRequest request,
HttpServletResponse response,
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.setBackgroundColor(new BaseColor(248,248,248));//页⾯背景⾊
document.setPageSize(rect);//页⾯参数//页边空⽩
document.setMargins(20, 20, 30, 20);
document.open();//设置页头信息
if(null!=("logoT")){
BaseFont bfChinese= ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese= new Font(bfChinese,20, Font.BOLD);
Paragraph paragraph= new Paragraph((("logoT"),FontChinese);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
PdfPTable table= null;
String ContextPath();
String basePath= 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(basePath+noData);
havaData= false;
}else{//有数据
image1 = Instance(highcharts(request,response,positionAndSvg[4]).toByteArray());
havaData= true;
}if("left".equals(positionAndSvg[1])){
String title1= URLDecoder.decode(positionAndSvg[2],"utf-8");
setTitleByCharts(document,30,title1,"",0,87,55,Element.ALIGN_LEFT,headfont);if(!"cooperateProporOne".equals(positionAndSv {
setTitleByCharts(document,0,"左图","右图",248,248,248,Element.ALIGN_CENTER,blackTextFont);
}else{
setTitleByCharts(document,0,"","",248,248,248,Element.ALIGN_CENTER,blackTextFont);
}
table= new PdfPTable(2);float[] wid ={0.50f,0.50f}; //列宽度的⽐例
table.setWidths(wid);
table=PdfPTableImage(table,image1,80f);
}else if("right".equals(positionAndSvg[1])){
table=PdfPTableImage(table,image1,80f);
table.setSpacingBefore(10);
table=setTableHeightWeight(table,360f,1000);
document.add(table);
table= null;
}else if("center".equals(positionAndSvg[1])){//总览全局
String title1 = URLDecoder.decode(positionAndSvg[2],"utf-8");
setTitleByCharts(document,30,title1,"",0,87,55,Element.ALIGN_LEFT,headfont);
setTitleByCharts(document,0,"","",248,248,248,Element.ALIGN_CENTER,blackTextFont);
table= new PdfPTable(1);float[] wid ={1.00f}; //列宽度的⽐例
table.setWidths(wid);if(havaData){
table=PdfPTableImageTable(table,image1,1000f,600f);
}else{
table= PdfPTableImageTable(Instance(basePath+noDataCenter),1000f,600f);
}
table=setTableHeightWeight(table,400f,1000);
document.add(table);
table=null;
}
}//添加⽔印Start---------------------------------------------------------------------------------------------
PdfFileExportUtil pdfFileExportUtil = newPdfFileExportUtil();
pdfWriter.w
PictureWaterMarkPdfPageEvent(basePath+waterMark));//pdfWriter.w
TextWaterMarkPdfPageEvent("xxx科技"));//添加⽔印End-----------------------------------------------------------------------------------------------
document.close();returnbuffer;
}catch(BadElementException e) {
e.printStackTrace();return null;
}catch(MalformedURLException e) {
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=useTable(table,Element.ALIGN_CENTER);
PdfPCell cellzr=createCellImage(image1,imgPercent);
cellzr.setBorder(0);
cellzr.setBackgroundColor(new BaseColor(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=useTable(table,Element.ALIGN_CENTER);
PdfPCell cellzr=createCellImageTable(image1,imgPercentWidth,imgPercentHeight);
cellzr.setBorder(0);
cellzr.setBackgroundColor(new BaseColor(248,248,248));
table.addCell(cellzr);returntable;
}/*** 设置表头
*@paramdocument
*@paramSpacingBefore
*@paramtitle1
*@paramtitle2
*@paramr1
*@paramr2
*@paramr3
*@paramele
*@paramfont
svg图*@throwsException*/
private void setTitleByCharts(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.setBorder(0);
cellzr.setBackgroundColor(newBaseColor(r1,r2,r3));
zrfbtitleTable.addCell(cellzr);
PdfPCell cellzr1=createCellLeft(title2,font,ele);
cellzr1.setBorder(0);
cellzr1.setBackgroundColor(newBaseColor(r1,r2,r3));
zrfbtitleTable.addCell(cellzr1);
zrfbtitleTable.setSpacingBefore(SpacingBefore);
zrfbtitleTable=setTableHeightWeight(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{
BaseFont bfChinese;try{
bfChinese= ateFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); headfont= new Font(bfChinese, 15, Font.BOLD);//设置字体⼤⼩
headfont.setColor(BaseColor.WHITE);
blackTextFont= new Font(bfChinese, 11, Font.BOLD);//设置字体⼤⼩
blackTextFont.setColor(BaseColor.BLACK);
colorfont= new Font(bfChinese, 11, Font.NORMAL);//设置字体⼤⼩
colorfont.setColor(BaseColor.RED);
}catch(Exception e) {
e.printStackTrace();
}
}/*** 创建指定内容背景⾊的Table元素Cell
*@paramvalue
*@paramfont
*@paramc1
*@paramc2
*@paramc3
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论