java 导出pdf 的组件

public class ExportReportServlet extends HttpServlet{


/**

*/

private static final long serialVersionUID = 1L;

private String encoding="UTF-8";

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding(encoding);

response.setCharacterEncoding(encoding);

//获取报表相关数据

Map<String, Object> mapInfo = getReportInfo(request);

// 生成Excel报表

if (mapInfo.get("type").equals("excel")) {

try{

IExportReport exportReport =ExportReportFactory.newInstance(ReportType.Excel);

exportReport.createReport(request, response, mapInfo);

}catch(Exception e){

e.printStackTrace();

}

} else if(mapInfo.get("type").equals("pdf")){

//生成PDF报表

try {

IExportReport exportReport =ExportReportFactory.newInstance(ReportType.PDF);

exportReport.createReport(request, response, mapInfo);

} catch (Exception e) {

e.printStackTrace();

}

}else if(mapInfo.get("type").equals("word")){

//生成WORD报表

try {

IExportReport exportReport =ExportReportFactory.newInstance(ReportType.Word);

exportReport.createReport(request, response, mapInfo);

} catch (Exception e) {

e.printStackTrace();

}

}else {

//生成jpg图片

try {

IExportReport exportReport =ExportReportFactory.newInstance(ReportType.JPG);

exportReport.createReport(request, response, mapInfo);

} catch (Exception e) {

e.printStackTrace();

}

}

}

/**

* Function:获取生产报表的相关信息

* @author  chenck

* CreateTime 2011年7月2日

* @param  HttpServletRequest request

* @return  Map<String, Object> 

*/

private Map<String, Object> getReportInfo(HttpServletRequest request) {

// 存放报表内容的相关信息

Map<String, Object> report_map = new HashMap<String, Object>();

//获取客户端信息

String type = request.getParameter("type"); // 要导出的文件类型

String title = request.getParameter("title"); // 导出的文件名称

String tableCaption = request.getParameter("tableCaption"); // 表示前台<table>中元素<caption>的集合

String tableCol = request.getParameter("tableCol"); // 表示前台<table>的最大列数的集合

String tableContext = request.getParameter("tableBody"); // 表示前台<table>的内容的集合

String widthTD = request.getParameter("widthTD"); // 表示前台<table>第一行所有列的宽度

String[] tableCaptionList = tableCaption.split("#CAPTION#");

String[]    tableColList = tableCol.split(";");

String[]    tableList = tableContext.split("#TABLE#");

String[] widthList = widthTD.split(";");

report_map.put("widthList", widthList);

//将处理后的信息放入HashMap中

report_map.put("type", type);

report_map.put("title", title);

report_map.put("tableCaptionList", tableCaptionList);

report_map.put("tableColList", tableColList);

report_map.put("tableList", tableList);

//返回map

return report_map;

}


@Override

public void init() throws ServletException {

       String _encoding=getInitParameter("encoding");

       if(null!=_encoding && !"".equals(_encoding)){

      this.encoding=_encoding;

       }

}

public String getEncoding() {

return encoding;

}

public void setEncoding(String encoding) {

this.encoding = encoding;

}

}

public class ExportReportFactory {

private static IExportReport exportReport;

private ExportReportFactory(){}


/**

* Function 根据报表类型实例化报表对象

* CreateTime 2011年7月8日

* @param reportType 报表类型(enum)

* @return IExportReport 报表实例

*/

public static IExportReport newInstance(ReportType reportType) {

switch (reportType) {

   case Excel:

    exportReport=new ExportReportForExcel();

    break;

   case PDF:

    exportReport=new ExportReportForPDF();

    break;

   case Word:

    exportReport=new ExportReportForWord();

    break;

   case JPG:

    exportReport=new ExportReportForJPG();

    break;

}

return exportReport;

}

}

public class ReportFontFactory {

public enum Font_Type{

TITLE,HEADER,CONTENT;

    }

private ReportFontFactory(){}

/**

* Function 创建PDF报表字体

* CreateTime 2014年7月22日

* @author yyh

* @param fontType 字体类型(enum)

* @return Font 字体

* @throws DocumentException

* @throws IOException

*/

public  static Font getFontChinese(Font_Type fontType) throws DocumentException, IOException{

BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

com.lowagie.text.Font fontChinese=null;

switch (fontType){

  case TITLE:

  fontChinese= new com.lowagie.text.Font(bfChinese,12, com.lowagie.text.Font.BOLD);

  break;

  case HEADER:

  fontChinese = new com.lowagie.text.Font(bfChinese,8, com.lowagie.text.Font.BOLD);

  break;

  case CONTENT:

  fontChinese = new com.lowagie.text.Font(bfChinese,8, com.lowagie.text.Font.NORMAL);

  break;

  default:  

  fontChinese = new com.lowagie.text.Font(bfChinese,7, com.lowagie.text.Font.NORMAL);

      break;

  

}

        return fontChinese;

}

/**

* Function 创建PDF报表字体

* CreateTime 2014年7月22日

* @author yyh

* @param fontType 字体类型(enum)

* @return Font 字体

* @throws DocumentException

* @throws IOException

*/

public static Font getFontCambodia(Font_Type fontType)throws DocumentException, IOException{

BaseFont bfCambodia=BaseFont.createFont("c://Windows/Fonts/KhmerOSsys.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

//BaseFont.createFont("c://Windows/Fonts/Khmviravuth.ttf", BaseFont.WINANSI,BaseFont.EMBEDDED);

com.lowagie.text.Font fontCambodia=null;

switch (fontType){

  case TITLE:

  fontCambodia= new com.lowagie.text.Font(bfCambodia,12, com.lowagie.text.Font.BOLD);

  break;

  case HEADER:

  fontCambodia = new com.lowagie.text.Font(bfCambodia,8, com.lowagie.text.Font.BOLD);

  break;

  case CONTENT:

  fontCambodia = new com.lowagie.text.Font(bfCambodia,8, com.lowagie.text.Font.NORMAL);

  break;

  default:  

  fontCambodia = new com.lowagie.text.Font(bfCambodia,7, com.lowagie.text.Font.NORMAL);

      break;

  

}

return fontCambodia;

}


}


转载于:https://my.oschina.net/u/780265/blog/295528

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值