Search code examples
javaitextitext7

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 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)

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;
    
    }

Solution

  • 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;
    
    }