gpt4 book ai didi

java - 发票生成 - thymeleaf、spring、itextpdf

转载 作者:行者123 更新时间:2023-11-30 06:42:09 37 4
gpt4 key购买 nike

我必须创建输出发票 PDF 文件的流程。我们找不到比创建 html 文件(使用 thymeleaf)并使用某种帮助程序(例如 itextpdf )将其格式化为 PDF 文件更好的解决方案。但我鼓励了一些问题,因为我无法包含 css 文件和图像。有没有更好的解决方案(免费)来处理它?或者你可以帮我吗?我将包括我的java代码。谢谢!

@Service
public class InvoiceTemplateHelper {

private final TemplateEngine templateEngine;
private ServletContext ctx;
private HttpServletRequest request;

@Autowired
public InvoiceTemplateHelper(TemplateEngine templateEngine, ServletContext ctx, HttpServletRequest request) {
this.templateEngine = templateEngine;
this.ctx = ctx;
this.request = request;
}

public String getHTMLInvoice() throws InvoiceCreationException {
WebContext context = new WebContext(request, null, ctx);
context.setVariable("invoiceTitle", "INVOICE/010101");

String body = templateEngine.process("invoice", context);
convertToPDF(body);
return body;
}

public void convertToPDF(String body) throws InvoiceCreationException {
try{
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("invoice.pdf"));
document.open();
InputStream is = new ByteArrayInputStream(body.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
document.close();
} catch (IOException e){
//TODO MESSAGE FOR EXCEPTION
throw new InvoiceCreationException();
} catch ( DocumentException e){
//TODO MESSAGE FOR EXCEPTION
throw new InvoiceCreationException();
}
}
}

以及带有示例样式和数据的虚拟 html 文件。

.clearfix:after {
content: "";
display: table;
clear: both;
}

a {
color: #5D6975;
text-decoration: underline;
}

body {
position: relative;
width: 21cm;
height: 29.7cm;
margin: 0 auto;
color: #001028;
background: #FFFFFF;
font-family: Arial, sans-serif;
font-size: 12px;
font-family: Arial;
}

header {
padding: 10px 0;
margin-bottom: 30px;
}

#logo {
text-align: center;
margin-bottom: 10px;
}

#logo img {
width: 90px;
}

h1 {
border-top: 1px solid #5D6975;
border-bottom: 1px solid #5D6975;
color: #5D6975;
font-size: 2.4em;
line-height: 1.4em;
font-weight: normal;
text-align: center;
margin: 0 0 20px 0;
background: url(../images/dimension.png);
}

#project {
float: left;
}

#project span {
color: #5D6975;
text-align: right;
width: 52px;
margin-right: 10px;
display: inline-block;
font-size: 0.8em;
}

#company {
float: right;
text-align: right;
}

#project div,
#company div {
white-space: nowrap;
}

table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
margin-bottom: 20px;
}

table tr:nth-child(2n-1) td {
background: #F5F5F5;
}

table th,
table td {
text-align: center;
}

table th {
padding: 5px 20px;
color: #5D6975;
border-bottom: 1px solid #C1CED9;
white-space: nowrap;
font-weight: normal;
}

table .service,
table .desc {
text-align: left;
}

table td {
padding: 20px;
text-align: right;
}

table td.service,
table td.desc {
vertical-align: top;
}

table td.unit,
table td.qty,
table td.total {
font-size: 1.2em;
}

table td.grand {
border-top: 1px solid #5D6975;;
}

#notices .notice {
color: #5D6975;
font-size: 1.2em;
}

footer {
color: #5D6975;
width: 100%;
height: 30px;
position: absolute;
bottom: 0;
border-top: 1px solid #C1CED9;
padding: 8px 0;
text-align: center;
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8"/>
<title th:remove="all">Taxi invoice</title>
<link rel="stylesheet" th:href="@{css/style.css}" media="all" />
</head>
<body>
<header class="clearfix">
<div id="logo">
<img th:src="@{images/logo.png}"/>
</div>
<h1 th:text="${invoiceTitle}">Invoice</h1>
<div id="company" class="clearfix">
<div><p th:text="#{company.name}"></p></div>
<div><p th:text="#{company.address}"></p></div>
<div><p th:text="#{comapny.phoneNumber}"></p></div>
<div>
<a th:mail="'mailto:'+#{company.mail}">
<p th:text="#{company.name}"></p>
</a>
</div>
</div>
<div id="project">
<div><span>PROJECT</span> Website development</div>
<div><span>CLIENT</span> John Doe</div>
<div><span>ADDRESS</span> 796 Silver Harbour, TX 79273, US</div>
<div><span>EMAIL</span> <a th:href="'mailto:'+#{company.mail}">john@example.com</a></div>
<div><span>DATE</span> August 17, 2015</div>
<div><span>DUE DATE</span> September 17, 2015</div>
</div>
</header>
<main>
<table>
<thead>
<tr>
<th class="service">SERVICE</th>
<th class="desc">DESCRIPTION</th>
<th>PRICE</th>
<th>QTY</th>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td class="service">Design</td>
<td class="desc">Creating a recognizable design solution based on the company's existing visual identity</td>
<td class="unit">$40.00</td>
<td class="qty">26</td>
<td class="total">$1,040.00</td>
</tr>
<tr>
<td class="service">Development</td>
<td class="desc">Developing a Content Management System-based Website</td>
<td class="unit">$40.00</td>
<td class="qty">80</td>
<td class="total">$3,200.00</td>
</tr>
<tr>
<td class="service">SEO</td>
<td class="desc">Optimize the site for search engines (SEO)</td>
<td class="unit">$40.00</td>
<td class="qty">20</td>
<td class="total">$800.00</td>
</tr>
<tr>
<td class="service">Training</td>
<td class="desc">Initial training sessions for staff responsible for uploading web content</td>
<td class="unit">$40.00</td>
<td class="qty">4</td>
<td class="total">$160.00</td>
</tr>
<tr>
<td colspan="4">SUBTOTAL</td>
<td class="total">$5,200.00</td>
</tr>
<tr>
<td colspan="4">TAX 25%</td>
<td class="total">$1,300.00</td>
</tr>
<tr>
<td colspan="4" class="grand total">GRAND TOTAL</td>
<td class="grand total">$6,500.00</td>
</tr>
</tbody>
</table>
<div id="notices">
<div>NOTICE:</div>
<div class="notice">A finance charge of 1.5% will be made on unpaid balances after 30 days.</div>
</div>
</main>
<footer>
Invoice was created on a computer and is valid without the signature and seal.
</footer>
</body>
</html>

最佳答案

看看 pdfHtml。这是最新的 iText 附加组件。它支持 HTML5 和 CSS 3。已经有一些示例,因此您应该能够快速开始将 html 转换为 pdf 文档。

关于java - 发票生成 - thymeleaf、spring、itextpdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44220739/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com