2017-09-21 66 views
0

我有一個現有的Java項目,我需要創建一個PDF。我需要HTMLConvertor將HTML轉換爲PDF,然後使用PDFReader和PDFStamper將內容添加到各個字段(getAcroFields)。無法導入HTMLConverter

所以我使用iText 5.5.12,我無法導入HTMLConverter。 這個版本不支持嗎? 我無法找到有關發佈的文檔。 我試過以下,但沒有工作。我嘗試添加iText 7作爲Maven的新庫,但版本直到5.5.12可用。

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

在此先感謝!

回答

4

你寫:

我試圖iText的7添加從Maven的新庫,但版本5.5.12之前可用。

這是錯誤的。見iText的網站上安裝說明:在您的POM文件https://developers.itextpdf.com/itext7/download-and-install-information/Java

複製粘貼:

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

HtmlConverter類是附加到iText的7。不適用於iText 5.事實上,iText從零開始被重寫以使HtmlConverter成爲可能。對於歷史概況見introduction to the HTML to PDF tutorial

在2016年,我們發佈了iText的7,iText的一個全新的版本,並沒有 與以前的版本兼容較長,但與 pdfHTML而創建的。大量的工作花費在新的Renderer框架上。當使用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

+0

有沒有其他方法可以讀取HTML模板,爲字段添加值並呈現PDF? 我可以使用XMLWorkerHelper嗎? 這個賢者的答案:https://stackoverflow.com/questions/35921388/how-to-convert-an-html-string-to-a-pdf-inputstream 建議一種方式。 請幫助,我只是無法弄清楚。 – raita

+0

查看[歷史](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml)。你可以使用XML Worker,但是(1)它不像pdfHTML那樣完整,(2)你想得到它的支持(它似乎你需要*支持),(3)它也被作爲AGPL發佈,所以如果您在商業環境中使用它,您可能還需要商業許可證。既然你是iText的* new *,爲什麼你會故意選擇使用舊版本?這沒有多大意義,是嗎? –