使用 iText 7 将 HTML 模板转换为 PDF:如何移动跨页打印的 table
Convert HTML Template to PDF using iText 7: how to move a table that prints across pages
我正在开发一个 asp.net mvc 项目,并且我正在使用一个 html 模板来生成一个包含动态数据的发票文档,我通过 handlebars 填充了这些数据。 html 结果正在使用 iText 7 转换为 PDF,因为这是公司使用的工具,但是我在将其转换为 PDF 时遇到问题,因为 html table 显示数据,有时有足够的数据只适合一页,但在其他情况下,有很多行遇到分页符,跨两页打印数据。每当数据不适合一页时,我需要将整个 table 块移动到下一页。
这是模板 (Mytemplate.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
<!-- Removed inline styles for space reasons -->
</style>
</head>
<body>
<div style="width: 100%; margin: 0 auto; text-align: center; display: flex; justify-content: center;">
<div style="max-width: 700px; background-color: white; width: 100%">
<table style="width:100%">
<tbody>
<tr>
<td>
<table class="table-header table-information m-0 border-zero">
<tr>
<td class="text-al" style="vertical-align: top">
<img src="{{baseUrl}}\images\clear_purple.png" style="width: 150px; height: 50px;">
</td>
<td class="text-al" style="vertical-align: top">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Insured: {{insured}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">{{facilityName}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="text-al vertical-ab">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Invoice Period: {{invoicePeriod}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">Date sent to LCTI: {{invoiceDateLTCI}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">LTCI Rep: {{clientReferrer}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Residency Information</p>
</th>
</tr>
<tr>
<td class="td-information p-0 vertical-ab" colspan="3">
<table class="table-information border-zero mb-0"">
<tbody>
<tr>
<td class="td-information w-35">
<p class="text text-bold">Facility Type</p>
<p class="text text-bold">Room/Unit Number</p>
<p class="text text-bold">Insurance Provider</p>
<p class="text text-bold">Insured’s Policy Number</p>
<p class="text text-bold">Invoice Number</p>
</td>
<td class="td-information w-65">
<p class="text-light">{{residencyInfo.facilityType}}</p>
<p class="text-light">{{residencyInfo.roomNumberChangeTo}}{{#if residencyInfo.roomNumberChange}} - {{residencyInfo.roomNumberChangeReason}}{{/if}}</p>
<p class="text-light">{{residencyInfo.insurerProvider}}</p>
<p class="text-light">{{residencyInfo.policyNumber}}</p>
<p class="text-light">{{residencyInfo.invoiceNumber}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="td-information td-border-top br-1 w-45">
<table class="w-full">
<tbody>
{{#if residencyInfo.haveDaysOutOfFacility}}
<tr>
<td>
<p class="text text-bold">Total Days in Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysInFacility}}</p>
</td>
</tr>
<tr>
<td>
<p class="text text-bold">Total Days out of Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysOutOfFacility}}</p>
</td>
</tr>
{{/if}}
{{#if residencyInfo.haveBedHoldDays}}
<tr>
<td>
<p class="text text-bold">Bedhold Days</p>
</td>
<td>
<p class="text-light">{{residencyInfo.bedHoldDays}}</p>
</td>
</tr>
{{/if}}
</tbody>
</table>
</td>
<td class="td-information bl td-border-top">
<table class="w-full">
<tbody>
<tr>
<td class="td-information-width">
<p class="text-light-2">Reason for Absence</p>
</td>
<td class="td-information-width">
<p class="text-light-2 text-ar">Dates of Absence</p>
</td>
</tr>
{{#each residencyInfo.absenceReasons}}
<tr>
<td>
<p class="text-light">{{reason}}</p>
</td>
<td>
<p class="text-light text-ar">{{from}}th{{#if hasTo}} - {{to}}th{{/if}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Care Provided</p>
</th>
</tr>
<tr>
<td class="td-information p-0" style="width: 50%;">
<table class="table-information border-zero mb-0">
<tbody>
<tr>
<th class="td-information w-58 th-activity td-border-bottom">
<p class="text text-bold">Activities of Daily Living (ADLS)</p>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Standby</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Hands On</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Provided</p>
</div>
</th>
</tr>
{{#each careProvided.adls}}
<tr>
<td class="td-information th-activity br-1 vertical-ac td-border-bottom">
<p class="text text-light">{{name}}</p>
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if standBy}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if handsOn}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if provided}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
<td class="td-information p-0" rowspan="3" style="width: 50%; padding-top: 7px;">
<table class="table-information m-0 border-zero mb-0">
<tbody>
<tr>
<th class="td-information td-border-bottom">
<p class="text text-bold pl-10">Instrumental Activities of Daily Living (IADLS)</p>
</th>
</tr>
{{#each careProvided.iadls}}
<tr>
<td class="td-information pt-0 pb-0">
<p class="text text-light pl-10 ">{{name}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="4">
<p class="text-title">Invoice Totals</p>
</th>
</tr>
<tr>
<td style="width: 58%;">
<table class="table-information border-zero m-0">
<tbody>
{{#each invoiceTotals.charges}}
<tr>
<td class="td-information pt-0-10" style="width:60%">
<p class="text text-bold" style="font-size:15px">{{name}}</p>
</td>
<td class="td-information pt-0-10" style="width:15%">
<p class="text text-light text-ar">{{amount}}</p>
</td>
<td class="td-information p-4" style="width:25%">
{{#if includedRnB}}
<p class="text-small">Included in R&B</p>
{{/if}}
</td>
</tr>
{{/each}}
<tr>
<td class="td-information text-ar" style="padding-right: 10px" colspan="2">
<p class="text-title-35 text-green-500 text-ar">${{invoiceTotals.totalAmount}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="td-information pt-0" rowspan="2" style="width: 48%;">
<table class="table-information p-0 m-0 border-zero">
<tbody>
<tr>
<th class="td-information pl-0 pt-0-10"><p class="text text-bold">Other Payment sources:</p></th>
</tr>
{{#each invoiceTotals.otherPaymentSources}}
<tr>
<td class="td-information pl-0 pt-0-10"><p class="text text-light">{{name}}</p></td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Reviewed By</p>
</th>
<th class="td-background" style="width: 35%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed By</p>
</th>
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed On</p>
</th>
<th class="td-background" style="width: 15%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Ip Address</p>
</th>
</tr>
<tr>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text pl-4" style="font-size: 15px">{{signature.facility.title}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-family: BrushScript; font-size: 24px">{{signature.facility.signedBy}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-size: 15px">{{signature.facility.signedOn}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text text-light" style="font-size: 15px">{{signature.facility.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.client.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.client.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.local.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.local.ipAddress}}</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
你可以看到{{insured}}等标记和{{#each residencyInfo.absenceReasons}}等循环数据的命令,所有这些都是车把进行数据替换所必需的。部分代码:
string templateHtml = File.ReadAllText(System.IO.Path.Combine(basePath, "templates/MyTemplate.html"));
var template = Handlebars.Compile(templateHtml);
var data = new
{
baseUrl = basePath,
insured = invoice.CustomerStatus, // Customer Name
facilityName = invoice.FacilityName,
invoicePeriod = invoice.PeriodFrom.ToString("MMM yyyy"),
invoiceDateLTCI = DateTime.Now.ToString("MM/dd/yyyy"),
clientReferrer = invoice.ClientReferrerName,
residencyInfo = new
{
facilityType = currenttFacilityType,
insurerProvider = invoice.Insurer,
policyNumber = invoice.ClaimNumber,
... // removed intencionally
absenceReasons = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityAbsenceReasons), jsonFacilityAbsenceReasonsDefinition)
},
careProvided = new
{
adls = ..., // removed intencionally
iadls = ... // removed intencionally
},
invoiceTotals = new
{
charges = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityCharges), jsonFacilityChargesDefinition),
totalAmount = totalAmount,
otherPaymentSources = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityOtherPayments), jsonFacilityOtherPaymentsDefinition)
},
signature = new
{
facility = new
{
signedBy = invoice.SignatureName,
signedOn = DateTime.ParseExact(invoice.SignatureDate, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy h:mm tt"),
title = invoice.SignatureFacilityTitleName,
ipAddress = invoice.IpAddress
},
client = new
{
... // removed intencionally
},
...
}
};
string invoiceHtml = template(data);
invoiceHtml 有最终的 HTML 数据,用于通过 iText7 生成 PDF。部分代码:
ConverterProperties converterProperties = new ConverterProperties();
// Create a Temp PDF file temporary, remove this when the whole invoice is created to manaqge it as stream byte[]
string file = System.IO.Path.Combine(basePath, $@"Documents\Temp_Invoice_{invoice.FacilityInvoiceId}.pdf");
PdfWriter writer = new PdfWriter(file);
PdfDocument pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.LEGAL);
var document = HtmlConverter.ConvertToDocument(invoiceHtml, pdf, converterProperties);
document.Close();
这是 PDF 文档,您可以在其中看到签名者部分打印在分页符上。在这种情况下,如果上面部分(费用)中打印的数据太多而无法以这种方式生成签名者部分打印件,我需要将签名者块移动到新页面。即使,如果费用部分(发票总计)太多,也无法放在第一页,我需要将其移至下一页。这是屏幕截图:
我尝试使用 ConverToElements 恢复 IElements,但它给了我没有页面的整个元素对象。
感谢您的帮助。
CSS 属性 page-break-inside
由 iText 7 的 HTML 处理为 PDF 转换以避免元素内的分页符。在您不想分成两页的 table 上:
<table style="page-break-inside: avoid;">
<!-- table content -->
</table>
当然,如果table长到比一页还大的高度,就没必要table拆分了。
或者,当首先使用 ConvertToElements
转换为 Elements 时,您可以在生成的 Table
实例上使用 SetKeepTogether(true)
。
我正在开发一个 asp.net mvc 项目,并且我正在使用一个 html 模板来生成一个包含动态数据的发票文档,我通过 handlebars 填充了这些数据。 html 结果正在使用 iText 7 转换为 PDF,因为这是公司使用的工具,但是我在将其转换为 PDF 时遇到问题,因为 html table 显示数据,有时有足够的数据只适合一页,但在其他情况下,有很多行遇到分页符,跨两页打印数据。每当数据不适合一页时,我需要将整个 table 块移动到下一页。
这是模板 (Mytemplate.html):
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
<!-- Removed inline styles for space reasons -->
</style>
</head>
<body>
<div style="width: 100%; margin: 0 auto; text-align: center; display: flex; justify-content: center;">
<div style="max-width: 700px; background-color: white; width: 100%">
<table style="width:100%">
<tbody>
<tr>
<td>
<table class="table-header table-information m-0 border-zero">
<tr>
<td class="text-al" style="vertical-align: top">
<img src="{{baseUrl}}\images\clear_purple.png" style="width: 150px; height: 50px;">
</td>
<td class="text-al" style="vertical-align: top">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Insured: {{insured}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">{{facilityName}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="text-al vertical-ab">
<table class="table-information m-0 border-zero">
<tbody>
<tr>
<td class="td-information p-0">
<p class="text p-0">Invoice Period: {{invoicePeriod}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">Date sent to LCTI: {{invoiceDateLTCI}}</p>
</td>
</tr>
<tr>
<td class="td-information p-0">
<p class="text p-0">LTCI Rep: {{clientReferrer}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Residency Information</p>
</th>
</tr>
<tr>
<td class="td-information p-0 vertical-ab" colspan="3">
<table class="table-information border-zero mb-0"">
<tbody>
<tr>
<td class="td-information w-35">
<p class="text text-bold">Facility Type</p>
<p class="text text-bold">Room/Unit Number</p>
<p class="text text-bold">Insurance Provider</p>
<p class="text text-bold">Insured’s Policy Number</p>
<p class="text text-bold">Invoice Number</p>
</td>
<td class="td-information w-65">
<p class="text-light">{{residencyInfo.facilityType}}</p>
<p class="text-light">{{residencyInfo.roomNumberChangeTo}}{{#if residencyInfo.roomNumberChange}} - {{residencyInfo.roomNumberChangeReason}}{{/if}}</p>
<p class="text-light">{{residencyInfo.insurerProvider}}</p>
<p class="text-light">{{residencyInfo.policyNumber}}</p>
<p class="text-light">{{residencyInfo.invoiceNumber}}</p>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td class="td-information td-border-top br-1 w-45">
<table class="w-full">
<tbody>
{{#if residencyInfo.haveDaysOutOfFacility}}
<tr>
<td>
<p class="text text-bold">Total Days in Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysInFacility}}</p>
</td>
</tr>
<tr>
<td>
<p class="text text-bold">Total Days out of Facility</p>
</td>
<td>
<p class="text-light">{{residencyInfo.totalDaysOutOfFacility}}</p>
</td>
</tr>
{{/if}}
{{#if residencyInfo.haveBedHoldDays}}
<tr>
<td>
<p class="text text-bold">Bedhold Days</p>
</td>
<td>
<p class="text-light">{{residencyInfo.bedHoldDays}}</p>
</td>
</tr>
{{/if}}
</tbody>
</table>
</td>
<td class="td-information bl td-border-top">
<table class="w-full">
<tbody>
<tr>
<td class="td-information-width">
<p class="text-light-2">Reason for Absence</p>
</td>
<td class="td-information-width">
<p class="text-light-2 text-ar">Dates of Absence</p>
</td>
</tr>
{{#each residencyInfo.absenceReasons}}
<tr>
<td>
<p class="text-light">{{reason}}</p>
</td>
<td>
<p class="text-light text-ar">{{from}}th{{#if hasTo}} - {{to}}th{{/if}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="3">
<p class="text-title">Care Provided</p>
</th>
</tr>
<tr>
<td class="td-information p-0" style="width: 50%;">
<table class="table-information border-zero mb-0">
<tbody>
<tr>
<th class="td-information w-58 th-activity td-border-bottom">
<p class="text text-bold">Activities of Daily Living (ADLS)</p>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Standby</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Hands On</p>
</div>
</th>
<th class="td-information p-0 w-14 td-border-bottom vertical-ab">
<div class="rotate-315">
<p class="text-s10 text-ac text-gray-500">Provided</p>
</div>
</th>
</tr>
{{#each careProvided.adls}}
<tr>
<td class="td-information th-activity br-1 vertical-ac td-border-bottom">
<p class="text text-light">{{name}}</p>
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if standBy}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if handsOn}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
<td class="td-information p-4 br-1 text-ac vertical-ac td-border-bottom">
{{#if provided}}
<img src="{{../baseUrl}}\images\clear_check.png" style="width: 14px; height: 14px;">
{{/if}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
<td class="td-information p-0" rowspan="3" style="width: 50%; padding-top: 7px;">
<table class="table-information m-0 border-zero mb-0">
<tbody>
<tr>
<th class="td-information td-border-bottom">
<p class="text text-bold pl-10">Instrumental Activities of Daily Living (IADLS)</p>
</th>
</tr>
{{#each careProvided.iadls}}
<tr>
<td class="td-information pt-0 pb-0">
<p class="text text-light pl-10 ">{{name}}</p>
</td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" colspan="4">
<p class="text-title">Invoice Totals</p>
</th>
</tr>
<tr>
<td style="width: 58%;">
<table class="table-information border-zero m-0">
<tbody>
{{#each invoiceTotals.charges}}
<tr>
<td class="td-information pt-0-10" style="width:60%">
<p class="text text-bold" style="font-size:15px">{{name}}</p>
</td>
<td class="td-information pt-0-10" style="width:15%">
<p class="text text-light text-ar">{{amount}}</p>
</td>
<td class="td-information p-4" style="width:25%">
{{#if includedRnB}}
<p class="text-small">Included in R&B</p>
{{/if}}
</td>
</tr>
{{/each}}
<tr>
<td class="td-information text-ar" style="padding-right: 10px" colspan="2">
<p class="text-title-35 text-green-500 text-ar">${{invoiceTotals.totalAmount}}</p>
</td>
</tr>
</tbody>
</table>
</td>
<td class="td-information pt-0" rowspan="2" style="width: 48%;">
<table class="table-information p-0 m-0 border-zero">
<tbody>
<tr>
<th class="td-information pl-0 pt-0-10"><p class="text text-bold">Other Payment sources:</p></th>
</tr>
{{#each invoiceTotals.otherPaymentSources}}
<tr>
<td class="td-information pl-0 pt-0-10"><p class="text text-light">{{name}}</p></td>
</tr>
{{/each}}
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<table class="table-information">
<tbody>
<tr class="tr-information">
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Reviewed By</p>
</th>
<th class="td-background" style="width: 35%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed By</p>
</th>
<th class="td-background" style="width: 25%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Signed On</p>
</th>
<th class="td-background" style="width: 15%">
<p class="text-title text-al" style="font-size: 16px; padding-left: 10px">Ip Address</p>
</th>
</tr>
<tr>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text pl-4" style="font-size: 15px">{{signature.facility.title}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-family: BrushScript; font-size: 24px">{{signature.facility.signedBy}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text" style="font-size: 15px">{{signature.facility.signedOn}}</p></td>
<td class="td-information pt-0-10" style="padding-top: 16px;"><p class="text text-light" style="font-size: 15px">{{signature.facility.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.client.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.client.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.client.ipAddress}}</p></td>
</tr>
<tr>
<td class="td-information pt-0-10"><p class="text pl-4" style="font-size: 15px">{{signature.local.title}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedBy}}</p></td>
<td class="td-information pt-0-10"><p class="text" style="font-size: 15px">{{signature.local.signedOn}}</p></td>
<td class="td-information pt-0-10"><p class="text text-light" style="font-size: 15px">{{signature.local.ipAddress}}</p></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
你可以看到{{insured}}等标记和{{#each residencyInfo.absenceReasons}}等循环数据的命令,所有这些都是车把进行数据替换所必需的。部分代码:
string templateHtml = File.ReadAllText(System.IO.Path.Combine(basePath, "templates/MyTemplate.html"));
var template = Handlebars.Compile(templateHtml);
var data = new
{
baseUrl = basePath,
insured = invoice.CustomerStatus, // Customer Name
facilityName = invoice.FacilityName,
invoicePeriod = invoice.PeriodFrom.ToString("MMM yyyy"),
invoiceDateLTCI = DateTime.Now.ToString("MM/dd/yyyy"),
clientReferrer = invoice.ClientReferrerName,
residencyInfo = new
{
facilityType = currenttFacilityType,
insurerProvider = invoice.Insurer,
policyNumber = invoice.ClaimNumber,
... // removed intencionally
absenceReasons = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityAbsenceReasons), jsonFacilityAbsenceReasonsDefinition)
},
careProvided = new
{
adls = ..., // removed intencionally
iadls = ... // removed intencionally
},
invoiceTotals = new
{
charges = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityCharges), jsonFacilityChargesDefinition),
totalAmount = totalAmount,
otherPaymentSources = JsonConvert.DeserializeAnonymousType(JsonConvert.SerializeObject(facilityOtherPayments), jsonFacilityOtherPaymentsDefinition)
},
signature = new
{
facility = new
{
signedBy = invoice.SignatureName,
signedOn = DateTime.ParseExact(invoice.SignatureDate, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy h:mm tt"),
title = invoice.SignatureFacilityTitleName,
ipAddress = invoice.IpAddress
},
client = new
{
... // removed intencionally
},
...
}
};
string invoiceHtml = template(data);
invoiceHtml 有最终的 HTML 数据,用于通过 iText7 生成 PDF。部分代码:
ConverterProperties converterProperties = new ConverterProperties();
// Create a Temp PDF file temporary, remove this when the whole invoice is created to manaqge it as stream byte[]
string file = System.IO.Path.Combine(basePath, $@"Documents\Temp_Invoice_{invoice.FacilityInvoiceId}.pdf");
PdfWriter writer = new PdfWriter(file);
PdfDocument pdf = new PdfDocument(writer);
pdf.SetDefaultPageSize(PageSize.LEGAL);
var document = HtmlConverter.ConvertToDocument(invoiceHtml, pdf, converterProperties);
document.Close();
这是 PDF 文档,您可以在其中看到签名者部分打印在分页符上。在这种情况下,如果上面部分(费用)中打印的数据太多而无法以这种方式生成签名者部分打印件,我需要将签名者块移动到新页面。即使,如果费用部分(发票总计)太多,也无法放在第一页,我需要将其移至下一页。这是屏幕截图:
我尝试使用 ConverToElements 恢复 IElements,但它给了我没有页面的整个元素对象。
感谢您的帮助。
CSS 属性 page-break-inside
由 iText 7 的 HTML 处理为 PDF 转换以避免元素内的分页符。在您不想分成两页的 table 上:
<table style="page-break-inside: avoid;">
<!-- table content -->
</table>
当然,如果table长到比一页还大的高度,就没必要table拆分了。
或者,当首先使用 ConvertToElements
转换为 Elements 时,您可以在生成的 Table
实例上使用 SetKeepTogether(true)
。