使用itext根据freemarks模版生成pdf,表格产生跨页问题解决方法

java根据模版动态生成pdf的文章点击打开链接

  使用XMLWorkerHelper.getInstance().parseXHtml();生成pdf。

如果直接根据ftl模版中的<table></table>填充数据,如果表格列大于第一列填充空白,整个表格会整体移到第二页。

整个表格列大于整页pdf空白,会直接不显示表格。主要看我<#list></list>循环的模版代码,就可以完全解决这个问题。

我的list中循环的是div+表格

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <meta http-equiv="Content-Style-Type" content="text/css"/>
    <title></title>
    <style type="text/css">
        body {
            font-family: pingfang sc light;
        }
        
		
		
		
		th,td{
			border: 1px  #ccc;
			text-align: center;	
			word-wrap : break-word ;
			word-break:break-all;	
		}
		table{
			border-collapse: collapse;
			table-layout: fixed;
			
		}
		.firstLine{
			font-size:12px;
			height:25px;
		}
		.SecondLine{
			font-size:12px;
			height:25px;
		}
		.SecondLineOne{
			float: left;
			text-align: left;
			width: 40px;
			
		}
		.SecondLineTwo{
			float: left;
			text-align: left;
			width: 320px;
			
		}
		.SecondLineThree{
			width: 17px;
			float: left;
			
		
		}
		.SecondLineFour{
			float: left;
			text-align: left;
			width: 320px;
			
		}
		.vertical{
			
		}
    </style>
</head>
<body>
<!--第一页开始-->


<div class="center" style="width:697px;">
	<div>
		<div class="firstLine" >
			<div style="float: left;text-align: left;width: 40px;height: 25px;"></div>
			<span style="width: 60px;float: left;">流程编号:</span><span style="width: 597px;">${(info.processNumber)!}</span>
		</div>
		<div class="firstLine">
			<div style="float: left;text-align: left;width: 40px;height: 25px;"></div>
			<span style="width: 60px;float: left;">流程标题:</span><span style="width: 597px;">${(info.title)!}</span>
		</div>
	    <div>
			<div class="SecondLine">
				<div class="SecondLineOne" ></div>
				<div class="SecondLineTwo" >所属公司:<span>${(info.company)!}</span></div>
				<div class="SecondLineThree" ></div>
				<div class="SecondLineFour">所在部门:<span>${(info.depart)!}</span></div>
			</div>
	    </div>
	    <div>
			<div class="SecondLine">
				<div class="SecondLineOne"></div>
				<div class="SecondLineTwo">申请人:<span>${(info.applicant)!}</span></div>
				<div class="SecondLineThree"></div>
				<div class="SecondLineFour">申请时间:<span>${(info.applicantDate)!}</span></div>
			</div>
	    </div>
	    <div  class="firstLine">
	    	<div style="float: left;text-align: left;width: 40px;height: 25px;"></div>
	    	<span  style="width: 60px;float: left;">联系方式:</span><span style="width: 597px;">${(info.contact)!}</span>
	    </div>
	    <div>
			<div class="SecondLine">
				<div class="SecondLineOne"></div>	
				<div class="SecondLineTwo">紧急情况:<span>${(info.emergency)!}</span></div>
				<div class="SecondLineThree"></div>
				<div class="SecondLineFour">发票总张数:<span>${(info.invoiceNum)!}</span></div>
			</div>
	    </div>
	    <div>
			<div class="SecondLine" style="height: 35px;">
				<div class="SecondLineOne"></div>
				<div class="SecondLineTwo">费用所属部门:<span>${(info.costDepart)!}</span></div>
				<div class="SecondLineThree"></div>
				<div class="SecondLineFour">费用所属公司:<span style="width: 240px; word-wrap : break-word ;word-break:break-all;">${(info.costCompany)!}</span></div>
			</div>
	    </div>
	    <div>
			<div class="SecondLine">
				<div class="SecondLineOne"></div>
				<div class="SecondLineTwo">费用付款总金额:<span>${(info.amount)!}</span></div>
				<div class="SecondLineThree"></div>
				<div class="SecondLineFour">费用付款总金额大写:<span>${(info.amountCapital)!}</span></div>
			</div>
	    </div>
	    <div class="firstLine">
	    	<div style="float: left;text-align: left;width: 40px;height: 25px;"></div>	
	    	<span style="width: 60px;float: left;">是否后补发票:</span><span style="width: 597px;">${(info.supply)!}</span>
	    </div>
	    <#if (info.remarks)?? >
	     	<div class="firstLine" style="height: 50px;">
	    		<div style="float: left;text-align: left;width: 40px;height: 25px;"></div>	
	    		<span style="width: 60px;float: left;">备注:</span><span style="width: 597px; word-wrap : break-word ;word-break:break-all;">${(info.remarks)!}</span>
	    	</div >
	    </#if>
	   
	 </div>
	 
	<div class="page">
		<div style="float: left;width: 617px;position: relative;right: -40px;">
			<table class="moduleTable">
				<thead>
					<tr>
						<th style="width: 40px;height:20px;"><span style="font-size:10px;">费用科目</span></th>
						<th style="width: 80px;height:20px;"><span style="font-size:10px;">付款金额</span></th>
						<th style="width: 80px;height:20px;"><span style="font-size:10px;">确认金额</span></th>
						<th style="width: 20px;height:20px;"><span style="font-size:10px;">发票张数</span></th>
						<th style="width: 121px;height:20px;"><span style="font-size:10px;">收款人账户名称及帐号</span></th>
						<th style="width: 110px;height:20px;"><span style="font-size:10px;">收款人开户银行及支行</span></th>
						<th style="width: 20px;height:20px;"><span style="font-size:10px;">账户类型</span></th>
						<th style="width: 160px;height:20px;"><span style="font-size:10px;">事由</span></th>
					</tr>
				</thead>
			</table>
		</div>		
		<#list module as module>
			<div style="float: left;width: 617px;position: relative;right: -40px;">
				<table class="moduleTable">
					<tbody>
						<tr>
							<td style="text-align: center;width: 40px;valign:top;height:50px;"><span style="font-size:10px;">${(module.subject)!}</span></td>
							<td style="text-align: center;width: 80px;height:50px;"><span style="font-size:10px;">${(module.amount)!}</span></td>
							<td style="text-align: center;width: 80px;height:50px;"><span style="font-size:10px;">${(module.confirm)!}</span></td>
							<td style="text-align: center;width: 20px;height:50px;"><span style="font-size:10px;">${(module.attachno)!}</span></td>
							<td style="text-align: center;width: 121px;height:50px;"><span style="font-size:10px;">${(module.accountTitle)!}<br/>${(module.account)!}</span></td>
							<td style="text-align: center;width: 110px;height:50px;"><span style="font-size:10px;">${(module.accountBank)!}<br/>${(module.openBank)!}</span></td>
							<td style="text-align: center;width: 20px;height:50px;"><span style="font-size:10px;">${(module.type)!}</span></td>
							<td style="text-align: center;width: 160px;height:50px;"><span style="font-size:10px;">${(module.reason)!}</span></td>
						</tr>
					</tbody>
				</table>
			</div>
		</#list>	
	</div>
	<div class="firstLine"></div>
	<div class="page">
		<div style="float: left;text-align: left;width: 40px;"></div>	
		<div style="float: left;width: 617px;position: relative;">
			<table>
				<thead>
					<tr>
						<th  style="width: 91px;height:15px;"><span style="font-size:10px;">执行环节</span></th>
						<th style="width: 92px;height:15px;"><span style="font-size:10px;">执行人</span></th>
						<th style="width: 92px;height:15px;"><span style="font-size:10px;">开始时间</span></th>
						<th style="width: 91px;height:15px;"><span style="font-size:10px;">结束时间</span></th>
						<th style="width: 90px;height:15px;"><span style="font-size:10px;">提交意见</span></th>
						<th style="width: 90px;height:15px;"><span style="font-size:10px;">审批方式</span></th>
						<th style="width: 90px;height:15px;"><span style="font-size:10px;">任务历时</span></th>
					</tr>
				</thead>
			</table>
		</div>		
		<#list record as record>
			<div style="float: left;width: 617px;position: relative;right: -40px;"">
				<table>
					<tbody>
						<tr>
							<td style="width: 91px;height:15px;"><span style="font-size:10px;height:30px;">${(record.link)!}</span></td>
							<td style="width: 92px;height:15px;"><span style="font-size:10px;height:30px;">${(record.user)!}</span></td>
							<td style="width: 92px;height:15px;"><span style="font-size:10px;height:30px;">${(record.createTime)!}</span></td>
							<td style="width: 91px;height:15px;"><span style="font-size:10px;height:30px;">${(record.endDate)!}</span></td>
							<td style="width: 90px;height:15px;"><span style="font-size:10px;height:30px;">${(record.opinion)!}</span></td>
							<td style="width: 90px;height:15px;"><span style="font-size:10px;height:30px;">${(record.way)!}</span></td>
							<td style="width: 90px;height:15px;"><span style="font-size:10px;height:30px;">${(record.duration)!}</span></td>
						</tr>
					</tbody>
				</table>
			</div>
		</#list>
	</div>
</div>


</body>
</html>






  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你使用的是iText7,可以通过获取模板中表格的行数,然后计算出表格的总高度,再设置表格的高度。 以下是一个示例代码,假设你的模板中只有一个表格表格下方没有其他内容: ``` java PdfDocument pdfDoc = new PdfDocument(new PdfReader("template.pdf"), new PdfWriter("output.pdf")); PdfPage page = pdfDoc.getFirstPage(); PdfCanvas canvas = new PdfCanvas(page); Rectangle pageSize = page.getPageSize(); // 获取模板中的表格 PdfFormXObject template = page.getFormXObject(1); PdfTable table = (PdfTable) template.getPdfObject(2); // 获取表格中的行数 int rowCount = table.getHeader().getRowCount() + table.getBody().size() + table.getFooter().getRowCount(); // 计算表格的总高度 float tableHeight = table.getTotalHeight(); // 设置表格的位置和高度 float x = 50; float y = pageSize.getTop() - tableHeight - 50; table.setFixedPosition(x, y, pageSize.getWidth() - 100); // 动态设置表格的高度 float availableHeight = y - pageSize.getBottom(); if (availableHeight < tableHeight) { // 表格高度超出了页面可用高度,需要缩放表格 table.scale(availableHeight / tableHeight, availableHeight / tableHeight); } // 将表格绘制到页面上 canvas.addXObject(template, 0, 0); pdfDoc.close(); ``` 在这个示例代码中,我们首先获取模板中的表格,然后计算出表格的总高度。接着,我们设置表格的位置和高度,然后动态调整表格的高度,使其适应页面高度,最后将表格绘制到页面上。如果表格高度超出了页面可用高度,我们需要缩放表格,以便完整地显示在页面上。 需要注意的是,这个示例代码假设模板中只有一个表格,并且表格下方没有其他内容。如果你的模板中还有其他内容,你需要根据实际情况计算可用高度。另外,如果你的模板中有多个表格,你需要分别计算每个表格的总高度,并根据实际情况设置每个表格的高度。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值