当我尝试将 html 转换为 pdf 时出现 Itext7 抛出错误

Itext7 throwing error when i trying to convert html to pdf

我正在使用 Itext7 将 html 转换为 pdf,但是当我尝试将 html 转换为 pdf 时,这让我无法调用“com.itextpdf.layout.margincollapse.MarginsCollapse.joinMargin(float)”空

POM:

<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.15</version>
        <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>3.0.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.1.15</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/styled-xml-parser -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>styled-xml-parser</artifactId>
        <version>7.1.15</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.1.15</version>
    </dependency>
    <!-- always needed -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.1.15</version>
    </dependency>

代码:

public static byte[] htmlToPdf(String html) throws IOException, DocumentException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfWriter pdfWriter = new PdfWriter(baos);
    ConverterProperties converterProperties = new ConverterProperties();
    PdfDocument pdfDocument = new PdfDocument(pdfWriter);
    pdfDocument.setDefaultPageSize(new PageSize(PageSize.A3));
    Document document = HtmlConverter.convertToDocument(html, pdfDocument, converterProperties);
    document.close();

    return baos.toByteArray();

}

java.lang.NullPointerException: Cannot invoke "com.itextpdf.layout.margincollapse.MarginsCollapse.joinMargin(float)" because "ownCollapseAfter" is null at com.itextpdf.layout.margincollapse.MarginsCollapseHandler.endMarginsCollapse(MarginsCollapseHandler.java:256) at com.itextpdf.layout.renderer.BlockRenderer.layout(BlockRenderer.java:359) at com.itextpdf.layout.renderer.RootRenderer.addChild(RootRenderer.java:136) at com.itextpdf.html2pdf.attach.impl.layout.HtmlDocumentRenderer.addChild(HtmlDocumentRenderer.java:176) at com.itextpdf.layout.RootElement.createAndAddRendererSubTree(RootElement.java:377) at com.itextpdf.layout.RootElement.add(RootElement.java:106) at com.itextpdf.layout.Document.add(Document.java:160) at com.itextpdf.html2pdf.attach.impl.tags.HtmlTagWorker.processBlockChild(HtmlTagWorker.java:189) at com.itextpdf.html2pdf.attach.impl.tags.HtmlTagWorker.processTagChild(HtmlTagWorker.java:155) at com.itextpdf.html2pdf.attach.impl.tags.BodyTagWorker.processTagChild(BodyTagWorker.java:127) at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:356) at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:338) at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.visit(DefaultHtmlProcessor.java:338) at com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor.processDocument(DefaultHtmlProcessor.java:253) at com.itextpdf.html2pdf.attach.Attacher.attach(Attacher.java:78) at com.itextpdf.html2pdf.HtmlConverter.convertToDocument(HtmlConverter.java:325) at utiles.PDF.htmlToPdf(PDF.java:22)

我做错了什么?

更新:

我更改了 css 并且工作了。

这个:

.header {
     display: flex; 
     flex-direction: row;
     justify-content: center;
     align-items: center;
    
    }

至:

.header {
      text-align: center;
     align-items: center;     
     margin: 0 0 20px 20px;
    
    }

问题出在 html,我更改了 css 并成功了。

这个:

.header {
  display: flex; 
  flex-direction: row;
  justify-content: center;
  align-items: center;    
}

至:

.header {
  text-align: center;
  align-items: center;     
  margin: 0 0 20px 20px;

}