项目结构 iText7
Posted
技术标签:
【中文标题】项目结构 iText7【英文标题】:Project Structure iText7 【发布时间】:2019-02-08 23:07:35 【问题描述】:我想让 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:\Users\AlGrant\IdeaProjects\assessment\css\assessment.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?
【问题讨论】:
艾尔格兰特,我的回答有帮助还是您还有未解决的问题? 【参考方案1】:您有一个字符串常量 BASEURI
,但我没有看到您在任何地方使用它。
根据 iText 7 pdfHTML 示例(例如 here),您需要在方法 createPdf
中将该 URI 设置为您的 ConverterProperties
的值:
ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri(BASEURI);
HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
【讨论】:
以上是关于项目结构 iText7的主要内容,如果未能解决你的问题,请参考以下文章
iText7高级教程之html2pdf——1.从Hello HTML开始