0
votes

I need to generate a Pdf document from some data that is located in a database and I came across iTEXT 7. I just started my first Maven project on Netbeans (started with a basic hello world) and this is the output:

Building mavenproject2 1.0-SNAPSHOT

Exception in thread "main" java.lang.NullPointerException
 at com.mycompany.mavenproject2.class2.main(class2.java:18) BUILD FAILURE [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch. Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles: [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

CODE :

package com.mycompany.mavenproject2;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import java.io.*;

public class class2 {
    public static void main(String args[]) throws FileNotFoundException 
    {
        File file = new File("hello.pdf");
        file.getParentFile().mkdirs();
        PdfWriter writer = new PdfWriter("hello.pdf");
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        document.add(new Paragraph("Hello World!"));
        document.close();
    }
}

pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <repositories>
        <repository>
            <id>itext</id>
            <name>iText Repository - releases</name>
            <url>https://repo.itextsupport.com/releases</url>
        </repository>
    </repositories>

    <dependencies>

        <!-- add all iText Core modules -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.0.3</version>
            <type>pom</type>
        </dependency>

        <!-- pdfSweep -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>cleanup</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfCalligraph -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>typography</artifactId>
            <version>1.0.2</version>
        </dependency>

        <!-- pdfInvoice -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>zugferd</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- pdfHTML -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!-- pdfXFA -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>pdfxfa</artifactId>
            <version>1.0.1</version>
        </dependency>

        <!-- iText 7 License Key Library -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-licensekey</artifactId>
            <version>2.0.4</version>
        </dependency>
    </dependencies>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>
1
With that short piece of code, you don't need all of the iText add-ons. You only use kernel and layout so that is all you need in your POM. Because they are on Maven Central, you don't need to add the iText repo either.Amedee Van Gasse
If you have purchased a license, then you also need the license key library, and then you still need the iText repository.Amedee Van Gasse
Delete the cleanup, typography, zugferd, html2pdf, pdfxfa dependencies. Replace core with kernel and layout. And use version 7.0.4 which was released 2 weeks ago.Amedee Van Gasse
Thank you @AmedeeVanGasse for your help. I got rid of all the extra dependencies and put in kernel, io and layout. I wasn't aware that I needed to purchase a license, I just included the repository that was mentioned on the iText website. I just applied for an AGPL License. Hope that comes along.SaberSz
You only need to purchase a license when you distribute your software as closed source (commercial). If you do not distribute your software, or if you distribute it as open source with the AGPL license, then you do not need to buy a license. You don't need to apply for an AGPL license, it is the default.Amedee Van Gasse

1 Answers

1
votes

This is the POM file I use for simple projects:

<repositories>
    <repository>
        <id>itext-releases</id>
        <name>iText Repository - releases</name>
        <url>https://repo.itextsupport.com/releases</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>     

...

<dependencies>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext-licensekey</artifactId>
        <version>2.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.0.4</version>
    </dependency>

    ...

</dependencies>

As suggested in the comments, it includes the releases (needed for license-key) and the dependency to the license-key repository.