项目结构iText7
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了项目结构iText7相关的知识,希望对你有一定的参考价值。
我想让IText7使用css将html转换为PDF,但无法正确设置BaseURI。有些css是外部文件,有些是通过CDN。
<link rel="stylesheet" type="text/css" href="/css/assessment.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootswatch/3.3.7/flatly/bootstrap.min.css">
我在src目录中的结构是:
| +---resources
| | ---main
| | | application.properties
| | |
| | +---static
| | | +---css
| | | | assessment.css
| | | |
| | | ---js
| | | assessment.js
| | |
| | ---templates
| | assessment.html
| | finished.html
| | greeting.html
| | output.html
所以我试过了:
public static final String BASEURI = "src/resources/main/static"
但是我得到了堆栈错误的页面:
2018-09-04 12:02:17.881 ERROR 11008 --- [nio-8080-exec-5] c.i.s.r.resource.ResourceResolver : Unable to retrieve stream with given base URI (file:/C:/Users/AlGrant/IdeaProjects/needsassessment/) and source path (../fonts/glyphicons-halflings-regular.woff2)
和
2018-09-04 12:02:17.887 ERROR 11008 --- [nio-8080-exec-5] c.i.h.attach.impl.DefaultHtmlProcessor : Unable to retrieve font:
@font-face {
font-family: 'Glyphicons Halflings'
src: url('../fonts/glyphicons-halflings-regular.eot')
src: url('../fonts/glyphicons-halflings-regular.eot?#iefix')
format('embedded-opentype'),url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'),url('../fonts/glyphicons-halflings-regular.woff') format('woff'),url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'),url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg')
和
java.io.FileNotFoundException: C:UsersAlGrantIdeaProjectsassessmentcssassessment.css (The system cannot find the path specified)
和
2018-09-04 13:34:16.922 ERROR 16808 --- [nio-8080-exec-4] c.i.s.css.parse.CssRuleSetParser : Error while parsing css selector: .was-validated .custom-control-input:valid~.custom-control-label::before
java.lang.IllegalArgumentException: Unsupported pseudo css selector: :valid
我的java类看起来像:
public static final String BASEURI = "src/main/resources/static";
@Qualifier("templateEngine")
@Autowired
private TemplateEngine templateEngine;
public void createPdf(String templateName, Map map) throws Exception {
Assert.notNull(templateName, "The templateName can not be null");
Context ctx = new Context();
if (map != null) {
Iterator itMap = map.entrySet().iterator();
while (itMap.hasNext()) {
Map.Entry pair = (Map.Entry) itMap.next();
ctx.setVariable(pair.getKey().toString(), pair.getValue());
}
}
String processedHtml = templateEngine.process(templateName, ctx);
try {
PdfWriter writer = new PdfWriter("C:\tmp\assessment.pdf");
PdfDocument pdfDoc = new PdfDocument(writer);
ConverterProperties converterProperties = new ConverterProperties();
HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
System.out.println("PDF created successfully");
}
所有资源都需要本地化吗? iText文档说BaseURI可能是外部文件或在线文件(CDN?),但没有显示如何包含CDN和本地CSS?
答案
你有一个String常量BASEURI
,但我没有看到你在任何地方使用它。
根据iText 7 pdfHTML示例(例如here),您需要在方法ConverterProperties
中将该URI设置为createPdf
的值:
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri(BASEURI);
HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
以上是关于项目结构iText7的主要内容,如果未能解决你的问题,请参考以下文章
iText7高级教程之html2pdf——2.使用CSS定义样式
iText7高级教程之html2pdf——2.使用CSS定义样式