页面数据下载PDF到浏览器(简易版)

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();
			}
		}
	}

	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值