:before :after 伪元素 CSS 未在我的 PDF 中呈现

:before :after pseudo elements CSS not getting rendered in my PDF

提问人:Vaibhav Varade 提问时间:10/19/2023 更新时间:10/19/2023 访问量:32

问:

以下是我用于将 thymeleaf 模板转换为 PDF 的依赖项。

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.1.15</version>
        </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>3.0.4</version>
        </dependency>

这是我的HTML到PDF转换代码:

public ByteArrayInputStream htmlToPdf(String processedHtml, LoggerDto loggerDto, DBLogger logger){
        
            loggerDto.setMessage("Inside htmlToPdf of DocumentGeneratorUtil" );
            logger.printLog(DbEnum.INFO,loggerDto);
        
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            
            try {
        
                PdfDocument pdfDoc = new PdfDocument(new PdfWriter(byteArrayOutputStream));
                pdfDoc.setDefaultPageSize(PageSize.A4);
                
                DefaultFontProvider defaultFont = new DefaultFontProvider(false, true, false);
                
                ConverterProperties converterProperties = new ConverterProperties();
                converterProperties.setFontProvider(defaultFont);
                converterProperties.setBaseUri(ServiceUtility.getCurrentBaseUrl());
                
                HtmlConverter.convertToPdf(processedHtml, pdfDoc, converterProperties);
                
                FileOutputStream fout = new FileOutputStream("/home/vaibhav/Desktop/CreatePDF/sample.pdf");
                byteArrayOutputStream.writeTo(fout);
                
                ByteArrayInputStream pdfStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                byteArrayOutputStream.close();
                byteArrayOutputStream.flush();
                loggerDto.setMessage("PDF stream created" );
                logger.printLog(DbEnum.INFO,loggerDto);
                
                return pdfStream;

}

我想在我的pdf中渲染树结构的线条,我在下面使用CSS:

.tree-sec {
    position: relative;
}

.tree-sec:before {
    content: '';
    position: absolute;
    top: 12px;
    left: 32px;
    width: 1px;
    height: 99%;
    background-color: #dfe2eb;
}

.tree-sec:after {
    content: '';
    width: 2px;
    height: 8px;
    background: #ffffff;
    position: absolute;
    top: -2px;
    left: 0;
}

但它无法理解 :before :after 伪元素。

我尝试使用最新版本的 Kernel 和 html2pdf,但我的 html 到 PDF 转换实用程序方法出现错误,例如无法识别 DefaultFontProvider、ConverterProperties。

请为此提出解决方案

css 伪元素 itext7 html2pdf spring-thymeleaf

评论


答: 暂无答案