简体   繁体   中英

Itext7 throwing error when i trying to convert html to pdf

I am using Itext7 for convert html to pdf, but when I try to transform a html to pdf this throws me a cannot invoke "com.itextpdf.layout.margincollapse.MarginsCollapse.joinMargin(float)" is null

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>

code:

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 Z4D236D9A2D102C5FE6AD1C 50DA4BEC50Z.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)

what am I doing wrong?

Update:

I changed the css and worked.

this:

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

to:

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

the problem was in the html, I changed the css and worked.

this:

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

to:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM