无法导入HTMLConverter

时间:2017-09-21 06:52:30

标签: java pdf intellij-idea itext itext7

我有一个现有的Java项目,我需要在其中创建一个PDF。我需要HTMLConvertor将HTML转换为PDF,然后使用PDFReader和PDFStamper将内容添加到各个字段(getAcroFields)。

所以我使用iText 5.5.12,我无法导入HTMLConverter。 这个版本不支持吗? 我无法找到有关这些版本的文档。 我尝试了以下但没有工作。我尝试将iText 7添加为Maven中的新库,但版本直到5.5.12可用。

我是Intellij的新手,那么如何安装支持HTMLConverter的版本iText?

提前致谢!

2 个答案:

答案 0 :(得分:5)

你写道:

  

我尝试将iText 7添加为Maven的新库,但版本直到5.5.12可用。

那是错的。请参阅iText网站上的安装说明:https://developers.itextpdf.com/itext7/download-and-install-information/Java

在POM文件中复制粘贴:

<dependencies>

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

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

    <!-- always needed -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for forms -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for PDF/A -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for digital signatures -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>sign</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for barcodes -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>barcodes</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for Asian fonts -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>font-asian</artifactId>
        <version>7.0.4</version>
    </dependency>

    <!-- only needed for hyphenation -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>hyph</artifactId>
        <version>7.0.4</version>
    </dependency>

</dependencies>

为了能够使用pdfHTML插件,您需要添加:

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

这是暂时的,因为目前pdfHTML是封闭源而不是Maven Central,但它将在未来几周内很快成为开源。

您需要pdfHTML的许可证密钥,无论是30天试用还是购买,然后将其添加到您的POM:

<dependencies>


  <!-- pdfHTML -->
  <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>html2pdf</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>

答案 1 :(得分:3)

HtmlConverter类是iText 7的附加组件。它不适用于iText 5.事实上,iText是从头开始重写的,以使HtmlConverter成为可能。有关历史概述,请参阅introduction to the HTML to PDF tutorial

  

2016年,我们发布了iText 7,这是iText的全新版本   与以前的版本更长时间兼容,但这是使用   记住pdfHTML。在新的渲染器上花了很多功   框架。使用iText 7创建文档时,可以使用   渲染器及其儿童渲染器已建成。布局由创建   遍历那棵树,这种方法更适合   处理HTML到PDF的转换。 iText对象完全是   重新设计以更好地匹配HTML标记并允许设置样式“   CSS方式。“

您可以按照下载说明下载iText 7:iText 7 for Java

至于pdfHTML,第一个版本是在关于太多第三方开发人员不忠于iText开发人员这一事实的长期争论之后开发的。他们假装不知道您通常不能在作为封闭源软件分发的应用程序(作为二进制或SaaS上下文)中免费使用AGPL软件。决定使一些附加组件闭源以创建意识,并避免我们必须起诉(参见this presentation中的幻灯片8到62)。

与此同时,我们已经获得批准制作pdfHTML开源(AGPL),但新版本发布前需要几周时间。与此同时,您可以在购买许可证之前注册获得30-day trial