项目结构iText7

问题描述 投票:0回答:1

我想让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?

pdf itext pdf-generation itext7
1个回答
0
投票

你有一个String常量BASEURI,但我没有看到你在任何地方使用它。

根据iText 7 pdfHTML示例(例如here),您需要在方法ConverterProperties中将该URI设置为createPdf的值:

ConverterProperties converterProperties = new ConverterProperties();
converterProperties.setBaseUri(BASEURI);
HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
© www.soinside.com 2019 - 2024. All rights reserved.