public class GeneratePDF extends Struts2Action{
//private String pdfOptType;
// 日志
private Log log = LogFactory.getLog(this.getClass());
//公共宽度
private static int[] PUBLIC_WIDTH = { 3000 };
// 日志
//private static Log log = LogFactory.getLog(CreditPDFTemplate.class);
private static Font fsFont = new Font(PdfUtils.fsFont, 13, Font.NORMAL, PdfUtils.baseColor);
private static Font hFont = new Font(PdfUtils.hFont, 16, Font.NORMAL, PdfUtils.baseColor);
private static Font hFont13 = new Font(PdfUtils.hFont, 13, Font.NORMAL, PdfUtils.baseColor);
//保存成功,跳转到下载页面
//public String saveMajor(){
//return SUCCESS;
//}
//下载PDF
public void downloadPdf(){
try {
//log.info("生成PDF参数:" + data);
HttpServletResponse response = ServletActionContext.getResponse();
UserSession userSession = AuthorityUtil.getSysUserSession();
log.info("生成PDF调用模板开始!");
GeneratePDF.getCreditPdf(response);
log.info("生成PDF结束!");
} catch (Exception e) {
e.printStackTrace();
log.info(e.toString());
}
}
//生成PDF模板
public static void getCreditPdf(HttpServletResponse response){
PdfWriter pdfWriter = null;
Document document = new Document(PageSize.A4);
try {
//准备数据
//-----这里写查询SQL语句,简洁版先不写这部分了
// 指定PDF导出位置
pdfWriter = PdfWriter.getInstance(document, response.getOutputStream());
//定义
String fileName="资金运用关联交易信息披露.PDF";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
document.open();
/********************************* 标题数据 ***********************************/
PdfUtils.setBlankRow(document,15);
PdfUtils.setBlankRow(document,15);
String[] qianzi = { "经营评价负责人:__________ 联系电话:__________" };
PdfUtils.createTable("",document, PUBLIC_WIDTH, 10, 1, qianzi, fsFont, Element.ALIGN_CENTER, false,1, false);
PdfUtils.setBlankRow(document,15);
PdfUtils.setBlankRow(document,15);
PdfUtils.setBlankRow(document,15);
String[] qianzi2 = { "经营评价联系人:__________ 联系电话:__________" };
PdfUtils.createTable("",document, PUBLIC_WIDTH, 10, 1, qianzi2, fsFont, Element.ALIGN_CENTER, false,1, false);
PdfUtils.setBlankRow(document,15);
PdfUtils.setBlankRow(document,15);
PdfUtils.setBlankRow(document,15);
String[] qianzi3 = { "日期: 年 月 日" };
PdfUtils.createTable("",document, PUBLIC_WIDTH, 10, 1, qianzi3, fsFont, Element.ALIGN_RIGHT, false,1, false);
document.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (document != null) {
document.close();
}
if (pdfWriter != null) {
pdfWriter.close();
}
}
}
}
页面数据下载PDF到浏览器(简易版)
最新推荐文章于 2024-05-14 15:39:52 发布