java 写 pdf 方式

这篇博客主要介绍了如何在Java中创建PDF文件,从JSP页面出发,通过Action调用来实现这一过程,虽然没有详细展示接口代码,但重点在于实际的实现类部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jsp 页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!doctype html>
<html>
<head>
<%@ include file="/WEB-INF/pages/globalweb/head/pagehead_v5.ini"%>
</head>
<body>


<iframe src="" marginwidth="0" target="_self"  id="faFrame"
	marginheight="0" frameborder="0" width="100%" height="945px" 
     style="overflow-x: hidden; overflow-y: auto;">

</iframe>

</body>
<script type="text/javascript">
	$("#faFrame").attr("src",$.getURL(_path + "/fagl/faxxck_dyxx.html? 
   id=${faxx_id}"+'&dlbs='+$("#dlbs").val()));
	
</script>
</html>


Action 调用

	public String dyxx(){
		ByteArrayOutputStream bous = null;
		try{
				StringBuffer title = new StringBuffer("");
				title.append(faxxckModel.getfamc());
				//开始生成pdf
				bous = service.createPdf(title,faxxckModel);

				this.fileName = new String((title.toString() + ".pdf").getBytes(), "ISO8859-1");
				this.inputStream = new ByteArrayInputStream(bous.toByteArray());
				return Result.PDF_INLINE;
		} catch (Exception e) {
			logException(e);
			return ERROR;
		}finally{
			IOUtils.closeQuietly(bous);
		}
	}

接口不写了

直接 实现类

@Override
	public ByteArrayOutputStream createPdf(PyfaxxckModel model)  throws DocumentException, IOException, ParseException, com.itextpdf.text.DocumentException{
		
		int cellByHeight = 20;
		
		//生成一个PDF文档
		com.itextpdf.text.Document document = new com.itextpdf.text.Document(com.itextpdf.text.PageSize.A4,30,30,20,20);
		
		
		//定义字节数组输出流对象,用于返回
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		
		//获取pdf写对象
		com.itextpdf.text.pdf.PdfWriter.getInstance(document, os);
		//设置pdf文件的基本属性
		document.addTitle("");
		document.addAuthor("zfsoft");
		document.addSubject("zfsoft");
		document.addKeywords("zfsoft");
		document.addCreator("zfsoft");
		document.open();
		

		//定义字体
		com.itextpdf.text.pdf.BaseFont bfChinese = com.itextpdf.text.pdf.BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);   
		

		com.itextpdf.text.pdf.BaseFont bfChineseWHXW = com.itextpdf.text.pdf.BaseFont.createFont(ServletActionContext.getServletContext().getRealPath("/")+"/font/STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//华文新魏
		com.itextpdf.text.pdf.BaseFont bfChineseHT = com.itextpdf.text.pdf.BaseFont.createFont(ServletActionContext.getServletContext().getRealPath("/")+"/font/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//黑体
		com.itextpdf.text.pdf.BaseFont bfChineseFS = com.itextpdf.text.pdf.BaseFont.createFont(ServletActionContext.getServletContext().getRealPath("/")+"/font/SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//仿宋
		com.itextpdf.text.Font HeadFont = new com.itextpdf.text.Font(bfChinese,36,Font.NORMAL);
		com.itextpdf.text.Font HeadSchoolFont = new com.itextpdf.text.Font(bfChineseWHXW,36,Font.BOLD);
		com.itextpdf.text.Font cellFontHT = new com.itextpdf.text.Font(bfChineseHT, 16, Font.BOLD);
		com.itextpdf.text.Font cellFontFS = new com.itextpdf.text.Font(bfChineseFS, 14, Font.NORMAL);
		com.itextpdf.text.Font cellFont = new com.itextpdf.text.Font(bfChinese, 16, Font.NORMAL);
		com.itextpdf.text.Font smallCellFont = new com.itextpdf.text.Font(bfChinese, 12, Font.NORMAL);
		com.itextpdf.text.Font smallCellFontB = new com.itextpdf.text.Font(bfChinese, 12, Font.BOLD);
		
		
		
	
		
		PyfaxfyqModel pyfaxfyqModel = new PyfaxfyqModel();
		pyfaxfyqModel.setPyfaxx_id(model.getPyfaxx_id());
		

		List<Map<String, String>> xnxqDzxxList = dao.getXnxqDzxx(model.getId());
		if (xnxqDzxxList.size()>0) {
			for(int j=0; j<xnxqDzxxList.size(); j++){
				
				document.newPage();
				
				document.add(JwglxtFrequentlyUserUtil.setParagraph("进程表", cellFontHT, Element.ALIGN_CENTER, 40, 16, 16)); 
				
				com.itextpdf.text.pdf.PdfPTable table = new com.itextpdf.text.pdf.PdfPTable((int)9);
				int[] BGBody = new int[]{24,42,18,18,15,15,15,10,20};
				table.setWidths(BGBody);
				table.setWidthPercentage(100f);
				table.setSpacingAfter(10);
				
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("号", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("名称", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("类别", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("性质", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("分配", 2, 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("方式", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("分", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("\r\n(周)", 1, 2, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("理", 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				table.addCell(JwglxtFrequentlyUserUtil.makeCell("实", 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
				
				String njm = xnxqDzxxList.get(j).get("JYXDNJDM");
				String xqm = xnxqDzxxList.get(j).get("JYXDXQM");
				String pm = xnxqDzxxList.get(j).get("PM");
				String xbx = "";	
				double xf = 0;			
				double zdxdxf = 0;	
				double bxxf = 0;		
				double xxxf = 0;		
				double ksxf = 0;		//
				
				
				List<Map<String,String>> kcxxList = dao.cxPyfakcxxByXnxqInfo(njm,xqm,model.getId());
				if (kcxxList.size()>0) {
					for (Map<String, String> kcxxMap : kcxxList) {
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("H"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("MC"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("MC"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("ZMC"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("X_LL"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("M_SJ"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("KMC"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("X"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						table.addCell(JwglxtFrequentlyUserUtil.makeCell(kcxxMap.get("Z"), 1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFont, cellByHeight));
						
						xbx=kcxxMap.get("XBX");
						xf = Double.parseDouble(kcxxMap.get("X"));
						
						if (xbx.equals("bx")) {
							bxxf += xf;
						}
						if (xbx.equals("xx")) {
							xxxf += xf;
						}
						if (kcxxMap.get("KHFSDM").equals("1")) {
							ksxf += xf;
						}
						zdxdxf = bxxf +xxxf;
					}
					table.addCell(JwglxtFrequentlyUserUtil.makeCell( "分 ", 9,1, PdfPCell.ALIGN_CENTER, PdfPCell.ALIGN_MIDDLE, smallCellFontB, cellByHeight));
				}
				document.add(table);
			}
		}
	
		document.close();
		return os;
	
	}

 

Java使用com.lowagie.text.pdf插件编PDF报表工具类,支持动态报表创建,使用简单,附件中包含了测试类和生成的报表文件。附件中的代码需要修改相关的保存路径后可以直接使用。创建一张报表例子: private JsFileExportResult createRowReport() { String condition = "开始时间:2018-02-02 14:00:30 结束时间:2018-02-06 16:00:30"; PDFGridReport pdfReport = new PDFGridReport("报表创建测试", GridReportTestModel.getModels()); pdfReport.addCondition(condition); pdfReport.header("字段名称1", "item1", 0, 0).width(160); pdfReport.header("字段名称3", "item3", 0, 2).getCell().backgroundColor(Color.ORANGE); pdfReport.header("字段名称4", "item4", 0, 3); pdfReport.header("字段名称5", "item5", 0, 4); pdfReport.header("字段名称2", "item2", 0, 1); pdfReport.header("值", "value", 0, 5).alignH(HAlign.ALIGN_CENTER).getCell().alignH(HAlign.ALIGN_RIGHT); pdfReport.header("时间", "time", 0, 6); pdfReport.header("图片", "image", 0, 7).width(60).alignH(HAlign.ALIGN_CENTER).getCell() .alignH(HAlign.ALIGN_CENTER).alignV(VAlign.ALIGN_MIDDLE); // 横向打印 pdfReport.getPageSetting().setPrintHorizontal(); pdfReport.group("item1").childGroup("item2"); pdfReport.setCellFormat(new PDFCellValueFormat() { @Override public String format(String fieldName, String oriValue) { if ("value".equals(fieldName)) { return String.format("%.2f", Double.parseDouble(oriValue)).toString(); } else { return oriValue; } } }); JsFileExportResult fileResult = pdfReport.createReport(); return fileResult; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值