spring-boot - Thymeleaf:无法使用 Spring Boot 2.0.3 在线下载 PDF 的模板
问题描述
我正在下载一个带有 Web 应用程序的 PDF 文件,该应用程序使用 Spring Boot 2.0.3 开发并使用 Thymeleaf,来自管理部分,受 Spring Security 保护。在本地它工作正常,但在网上我得到这个错误:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/email/confirmedbooking", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
这是控制器:
@GetMapping("/admin/bookings/booking-pdf")
public void generatePdfBooking(@RequestParam Long idbooking, HttpServletResponse response)
throws IOException, Exception{
bookingService.setBookingService(idbooking);
Booking booking = bookingService.getBooking();
Guest guest = bookingService.getGuest();
String idlanguage;
if(guest.getIdlanguage() != null){
idlanguage = guest.getIdlanguage();
} else {idlanguage = "en";}
Map<String, Object> map = new HashMap<>();
map.put("booking", booking);
map.put("guest", guest);
byte[] data = pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);
pdfGenerator.streamReport(response, data, "id-" + booking.getIdbooking() + ".pdf");
}
这是html页面的摘录:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<link rel="stylesheet" href="/webjars/bootstrap /css/bootstrap.min.css"/>
<link rel="stylesheet" href="/css/rentalwebs.css"/>
</head>
<body>
<table style="width:680px" class="table table-borderless">
<tbody>
<tr>
<td>
<h2 th:text="${property.name}"></h2>
</td>
</tr>
<tr>
<td>
<span th:text="#{booking.id}"></span>
<span th:text="${booking.idbooking}"></span>
....
作为模板生成器,我使用的是 org.xhtmlrenderer.pdf.ITextRenderer (flying-saucer-pdf)。
我已经测试过提供不同的 .html 文件来生成 PDF pdfGenerator.createPdf("/email/confirmedbooking", map, idlanguage);
,但结果总是相同的。
解决方案
按照 Daniel Mikusa 的建议,我已经能够解决这个问题,取出第一个 / from /email/confirmedbooking
,留下这样的代码:
byte[] data = pdfGenerator.createPdf("email/confirmedbooking", map, idlanguage);
似乎它无法识别路径,可能是因为双斜杠问题,已经在这篇文章中解释过:Error resolve template with Spring Boot using Thymeleaf packaged in a jar
推荐阅读
- c# - .NET Core 控制台应用程序 - 未在运行时设置适当的基于 appsettings 的环境变量
- android - 更改列表排序时,Android Compose LazyColum 渲染问题
- c# - System.ArgumentException: '路径中有非法字符。使用 C# 删除文件时
- python-3.x - 相对导入 python3 错误:尝试在没有已知父包的情况下进行相对导入
- android - 如何从以下 ROOM 数据库查询结果中提取 INT?
- linux - 提取给定字符串中的子字符串
- javascript - 从 vue-router 调用后,Vue 3 组件未更新
- javascript - React window.pageYOffset 总是在基于类的组件中记录零
- mailmerge - 尝试使用 UCanAccess 5.0.1 和 Jackcess 打开 Access 97 数据库时出错,邮件与 LibreOffice 合并
- batch-file - 为变量引用两个txt文件创建新文件