HTML 檔案轉換為 PDF

Convert an entire HTML file into a pixel-perfect PDF with IronPDF for Python’s RenderHtmlFileAsPdf method.

開始

To convert an HTML file to PDF, you can use the RenderHtmlFileAsPdf method. This method requires only the path to the local HTML file and returns a PdfDocument object. 此物件包含 HTML 檔案的內容,其呈現方式與在 Web 瀏覽器中檢視時相同。 請確保您的 HTML 程式碼包含有效的標記,以避免渲染問題。

以下是一個簡單的範例,說明如何使用此方法:

理解程式碼

  • ChromePdfRenderer :此物件用作與渲染過程互動的點。 它允許透過設定頁首、頁尾等屬性來調整 PDF 輸出。

  • ChromePdfRenderer: This method takes a path to an HTML file and converts it into a PdfDocument. 產生的 PDF 檔案將與 HTML 檔案在網頁瀏覽器中的顯示效果非常接近。

  • save_as: This method saves the newly created PDF document to the specified path on your file system.

自訂 PDF 輸出

ChromePdfRenderer comes with various options for customization, including setting headers, footers, margins, backgrounds, page numbers, etc. For more advanced features and customization, see this code example and learn how to customize PDF output.

透過本教學學習如何將HTML轉換為PDF!

準備好開始了嗎?
Version: 2025.9 剛發表