ITExtrenderer -HTML到PDF转换问题
总而言之,我使用的是thymleaf,itextrenderer,Spring引导尝试生成PDF文件。
输入:JSON文件
Java代码:读取JSON文件,对其进行处理以检索信息并将其保存在JavaObjects中。该信息将显示在PDF中。
final TemplateEngine templateEngine;
private final ObjectMapper objectMapper;
//reportAsBytes will be sent to HTML file
byte[] reportAsBytes = generateReport(
TEST_TEMPLATE,
objectMapperWithNullValues.valueToTree(dataForReport)
);
public byte[] generateReport(String templateId, JsonNode data) {
Map<String, Object> dataAsMap = convertDataToMap(data);
String reportAsHtml = generateReportAsHtml(templateId, dataAsMap, 'en');
return generatePdfReportAsBytes(reportAsHtml);
}
private Map<String, Object> convertDataToMap(JsonNode data) {
return objectMapper.convertValue(data, new TypeReference<Map<String,Object>>() {});
}
private String generateReportAsHtml(String templateId, Map<String, Object> jsonData, Locale reportLocale) {
Context context = new Context();
context.setVariable("data", jsonData);
context.setLocale(reportLocale);
return templateEngine.process(templateId, context);
}
private byte[] generatePdfReportAsBytes(String reportAsHtml) {
ITextRenderer renderer = createRenderer();
renderer.setDocumentFromString(reportAsHtml);
renderer.layout();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
renderer.createPDF(outputStream);
return outputStream.toByteArray();
}
Thymleaf:HTML模板具有位置持有人$ {“ Data” .javaobject}以获取Java的数据以填充PDF文件。
PDF报告正确生成。但是,当任何JavaObject的字符串值都带有特殊字符,例如'&gt;','&lt;' ,'&amp;'停止报告生成。
这里有人指导吗?
All , I am using Thymleaf , ITextRenderer , Spring Boot to try to generate the PDF file.
Input : JSON file
Java Code: Reads the JSON file , process it to retrieve the information and save it in JavaObjects. The information will be displayed in the PDF.
final TemplateEngine templateEngine;
private final ObjectMapper objectMapper;
//reportAsBytes will be sent to HTML file
byte[] reportAsBytes = generateReport(
TEST_TEMPLATE,
objectMapperWithNullValues.valueToTree(dataForReport)
);
public byte[] generateReport(String templateId, JsonNode data) {
Map<String, Object> dataAsMap = convertDataToMap(data);
String reportAsHtml = generateReportAsHtml(templateId, dataAsMap, 'en');
return generatePdfReportAsBytes(reportAsHtml);
}
private Map<String, Object> convertDataToMap(JsonNode data) {
return objectMapper.convertValue(data, new TypeReference<Map<String,Object>>() {});
}
private String generateReportAsHtml(String templateId, Map<String, Object> jsonData, Locale reportLocale) {
Context context = new Context();
context.setVariable("data", jsonData);
context.setLocale(reportLocale);
return templateEngine.process(templateId, context);
}
private byte[] generatePdfReportAsBytes(String reportAsHtml) {
ITextRenderer renderer = createRenderer();
renderer.setDocumentFromString(reportAsHtml);
renderer.layout();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
renderer.createPDF(outputStream);
return outputStream.toByteArray();
}
Thymleaf: HTML template having place holders ${"data".javaobject} to get the data from Java to populate the PDF file.
The PDF report is generated correctly . But , when the string value for any of the javaObject with special characters like '>', '<' , '&' stops the report generation.
Is there anyone to guide here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有一个问题的答案。
我们可以为具有特殊字符的文本进行消毒,并希望在PDF文件中出现
HTMLUTILS中的此类消毒,可以在Spring-Web-5.3.18.Jar中使用。
I have an answer for the question.
We can do the sanitization for the text which has special characters and want to appear as such in PDF file
HtmlUtils is available in spring-web-5.3.18.jar.