有沒有辦法在Java中將html檔案轉換為記憶體中的PDF檔案?
P粉819533564
P粉819533564 2024-02-26 14:02:31
0
1
356

我收到了一個 HTML 文件,並希望將其轉換為記憶體中 PDF 文件。在轉換過程中,我不想為此使用任何外部位置。我想要的只是將其保留在記憶體中

到目前為止,我已經嘗試了一些Java庫進行轉換,但它們總是在某個位置創建一個臨時文件,然後從中讀取/寫入。我不想在轉換期間進行任何 I/O 操作。

P粉819533564
P粉819533564

全部回覆(1)
P粉308783585

HTMLWorker 類別多年前已被棄用。 HTMLWorker 的目標是將小而簡單的 HTML 片段轉換為 iText 物件。它從來沒有打算將完整的 HTML 頁面轉換為 PDF,但這就是許多開發人員嘗試使用它的方式。這導致了許多挫折感,因為 HTMLWorker 不支援所有 HTML 標籤,不解析 CSS 檔案等等。為了避免這種挫敗感,HTMLWorker 已從最新版本的 iText 中刪除。

2011 年,iText Group 發布了 XML Worker 作為通用 XML 到 PDF 工具,建構於 iText 5 之上。預設實作將 XHTML(資料)和 CSS(樣式)轉換為 PDF,映射 HTML 標籤,例如

# , # , 和
  • # to iText 5 objects such as Paragraph, Image, 和 ListItem. We don't know of any implementations that used XML Worker for any other XML formats, but many developers used XML Worker in combination with jsoup as an#2PDF converter.

    XML Worker wasn't a URL2PDF tool though. XML Worker expected predictable HTML created for the sole purpose of converting that HTML to PDF. A common use case was the creation of invoices. Rather than programming the design in Java或 C#, developers chose to create a simple HTML template defining the structure of the document, 和some CSS defining the styles. They then populated the HTML with data, 和used XML Worker to create the invoices as PDF documents, throwing documents, throwing documents . We'll take a closer look at this use case in chapter 4, converting XML to HTML in memory using XSLT, then converting that HTML to PDF using the pdfHTML add-on.

    iText 5 最初創建時,它被設計為一種盡可能快地生成 PDF 的工具,一旦頁面完成就將其刷新到 OutputStream。 2000 年 iText 首次發佈時,一些非常有意義的設計選擇在 16 年後仍然出現在 iText 5 中。不幸的是,其中一些選擇使得將 XML Worker 的功能擴展至許多開發人員期望的品質水準變得非常困難(如果不是不可能的話)。如果我們真的想創建一個出色的 HTML 到 PDF 轉換器,我們必須從頭開始重寫 iText。我們做到了。

    2016 年,我們發布了 iText 7,這是 iText 的全新版本,不再與以前的版本相容,但在創建時考慮了 pdfHTML。新的渲染器框架花費了大量的工作。當使用 iText 7 建立文件時,會建立渲染器及其子渲染器樹。佈局是透過遍歷該樹創建的,這種方法更適合處理 HTML 到 PDF 的轉換。 iText 物件經過完全重新設計,以更好地匹配 HTML 標籤並允許「CSS 方式」設定樣式。

    For instance: in iText 5, you had a PdfPTable and a PdfPCell object to create a table and its cells. If you wanted every cell to contain text in a font different from the default font, you needed to set that the content of every separate cell. In iText 7, you have a ? terms of architectural design, especially if the goal is to convert HTML to PDF.

    But let's not dwell on the past, let's see what pdfHTML can do for us. In the first chapter, we'll take a look at different variations of the convertToPdf()/ConvertToPdf() method, 和wellariations of the convertToPdf()/ConvertToPdf() method, 和we'lldariations how the converter is configured.

    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!