Python 創建 PDF 文件

如何在 Python 中建立 PDF 文件

This article was translated from English: Does it need improvement?
Translated
View the article in English

使用IronPDF庫,只需幾行程式碼即可將 HTML 字串、HTML 文件或 URL 轉換為 PDF 文檔,從而在 Python 中建立 PDF 文件。 IronPDF可自動處理渲染、格式化和安全功能。

快速入門:使用 Python 建立 PDF

:title=Quickstart
# 1. Install IronPDF: pip install ironpdf
# 2. Import the library
from ironpdf import *
# 3. Create renderer
renderer = ChromePdfRenderer()
# 4. Convert HTML to PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
# 5. Save the PDF
pdf.SaveAs("output.pdf")
:title=Quickstart
# 1. Install IronPDF: pip install ironpdf
# 2. Import the library
from ironpdf import *
# 3. Create renderer
renderer = ChromePdfRenderer()
# 4. Convert HTML to PDF
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1>")
# 5. Save the PDF
pdf.SaveAs("output.pdf")
PYTHON

使用 Python 自動建立 PDF 可以讓開發人員在應用程式中產生 PDF。 此功能可用於根據需要產生發票、報告或其他文件類型。

本操作指南重點在於如何使用IronPDF在 Python 腳本中以程式設計方式建立 PDF 檔案。

什麼是IronPDF Python PDF 函式庫?

IronPDF是一個 Python 庫,旨在從 HTML 建立 PDF 文件。 其 API 可以輕鬆產生和自訂具有各種功能的 PDF,包括:

  1. 新增文字、圖像和其他內容類型
  2. 選擇字體、顏色,以及控製文件版面和格式。

IronPDF可以整合到.NETJavaPython應用程式中,從而實現跨多個平台的 PDF 生成。

除了產生 PDF 文件外, IronPDF還提供其他功能。 這些功能包括文件格式轉換、從 PDF 提取文字和數據,以及透過密碼加密保護 PDF 的功能。 對於進階用例,請探索如何合併多個 PDF壓縮 PDF 檔案或以程式設計方式填寫 PDF 表單

如何使用 Python 腳本建立 PDF 文件?

我需要哪些先決條件?

若要使用IronPDF for Python,請確保您的電腦已安裝以下軟體:

  1. .NET 6.0 SDK: 由於IronPDF Python 依賴IronPDF .NET函式庫,因此需要此函式庫。 從微軟官方網站下載。
  2. Python: 從 https://www.python.org/downloads/ 下載並安裝 Python 3.x。 安裝過程中選擇將 Python 新增至 PATH 的選項。
  3. Pip: 通常與 Python 3.4+ 捆綁在一起。 驗證安裝情況,如有需要,可單獨安裝。
  4. 使用以下命令透過 pip 安裝:
 pip install ironpdf

請注意在某些系統中,Python 2.x 可能仍然是預設版本。 在這種情況下,您可能需要明確使用 pip3 命令而不是 pip,以確保您使用的是 Python 3 的 Pip。如果您遇到安裝錯誤,請參閱我們的OSError 問題故障排除指南

創建PDF之前需要進行哪些程式碼設定?

首先,將以下語句加入你的 Python 腳本頂部:

# Import statement for IronPDF for Python
from ironpdf import *
# Import statement for IronPDF for Python
from ironpdf import *
PYTHON

接下來,透過將授權金鑰指派給 License 的 LicenseKey 屬性(在任何其他程式碼行之前),使用有效的授權金鑰配置IronPDF 。 有關如何在專案中實施許可證密鑰的詳細說明,請存取我們的許可證密鑰設定指南

# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
PYTHON

請注意要建立不帶任何浮水印的 PDF,您需要有效的許可證金鑰。 Purchase a license key or obtain a free trial license key. 否則,請繼續執行下一步,免費產生帶有浮水印的新 PDF 文件。

如何將HTML字串轉換為PDF文件?

我應該使用哪種方法進行HTML字串轉換?

使用 RenderHtmlAsPdf 方法從 HTML 字串產生新的 PDF 文件。 該方法支援 CSS 樣式、 JavaScript執行和自訂字體。

將 HTML 標記作為參數提供給 RenderHtmlAsPdf 方法。 IronPDF將執行轉換,產生 PdfDocument 實例。

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from an HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from an HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
PYTHON

如何保存生成的PDF文件?

將 HTML 字串轉換為 PDF 文件後,使用 SaveAs 方法將 PDF 儲存到本機系統上的某個路徑:

# Export to a file or Stream
pdf.SaveAs("htmlstring_to_pdf.pdf")
# Export to a file or Stream
pdf.SaveAs("htmlstring_to_pdf.pdf")
PYTHON

將建立一個名為 "htmlstring_to_pdf.pdf" 的 PDF 文件,並保留原始 HTML 字串的內容。 有關更進階的 HTML 渲染技術,包括使用複雜的佈局和JavaScript框架,請參閱我們全面的HTML 轉 PDF 教學

如何在Python中從HTML檔案產生PDF?

如何轉換本地HTML檔案?

若要使用 Python 從本機儲存的 HTML 文件產生 PDF 文檔,請依照下列程式碼操作。 RenderHtmlFileAsPdf 方法提供了一種直接轉換現有 HTML 文件的方法:

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")

# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")
# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")

# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")
PYTHON

IronPDF為何能保留HTML格式?

在上面的程式碼中,RenderHtmlFileAsPdf 方法從 HTML 檔案建立 PDF 文件。請提供一個字串或路徑,指定 HTML 檔案在檔案系統中的位置。

IronPDF會像網頁瀏覽器一樣渲染 HTML 元素,包括任何相關的 CSS 和JavaScript。 這樣可以確保產生的 PDF 文件中的內容準確呈現。 該程式庫支援包括 CSS3、HTML5 和JavaScript框架在內的現代 Web 標準,使其成為將複雜的 Web 文件轉換為 PDF 格式的理想選擇。

使用 SaveAs 方法將產生的 PDF 儲存到系統上的特定位置,類似於前面的範例。

如何在Python中根據URL創建PDF?

什麼方法可以將網頁轉換為PDF?

若要使用 Python 從網頁建立 PDF 文檔,請使用 RenderUrlAsPdf 方法。 將所需網頁的 URL 作為參數傳遞給此方法:

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")

# Export to a file or Stream
pdf.SaveAs("url.pdf")
# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")

# Export to a file or Stream
pdf.SaveAs("url.pdf")
PYTHON

URL渲染功能支援現代Web技術,包括動態JavaScript內容、AJAX呼叫和響應式佈局。 IronPDF會等待頁面完全載入後再轉換,確保所有內容都能準確捕捉。

哪裡可以找到更多URL轉換範例?

有關將網頁轉換為 PDF 的更多信息,請訪問PDF 程式碼範例 URL頁面。 對於需要身份驗證的網站,請參閱我們的網站和系統登入指南。

如何自訂PDF格式選項?

我可以在渲染選項中修改哪些設定?

若要自訂 PDF 檔案的格式,請使用 RenderingOptions 屬性。 此類別提供各種可配置設置,以實現所需的 PDF 文件佈局和外觀。 設定項目包括頁面方向、頁面大小、頁邊距大小等。 設定 RenderingOptions 中可用的屬性,以產生具有所需設定的 PDF 文件。 有關如何使用 RenderingOptions 的更多信息,請參閱此程式碼範例

其他自訂選項包括:

  • 設定自訂頁面邊距
  • 配置縱向和橫向螢幕方向
  • 自訂 PDF 頁面尺寸
  • 管理頁碼和分頁符

如何使用密碼保護PDF檔案?

如何新增密碼保護?

若要為 PDF 檔案新增密碼保護,請使用 SecuritySettings 物件的 PdfDocument 屬性。 存取 SecuritySettings 屬性,並將密碼指派給 UserPassword 屬性,密碼以字串形式指定。 如需了解更多進階安全功能,包括加密和數位簽名,請造訪我們的安全性和元資料範例頁面

例如,考慮保護在"URL 到 PDF"範例中建立的 PDF 文件:

# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"

# Configure additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False

# Save the password-protected PDF
pdf.SaveAs("protected.pdf")
# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"

# Configure additional security settings
pdf.SecuritySettings.OwnerPassword = "admin123"
pdf.SecuritySettings.AllowUserPrinting = True
pdf.SecuritySettings.AllowUserCopyPasteContent = False

# Save the password-protected PDF
pdf.SaveAs("protected.pdf")
PYTHON

密碼保護在實務上是如何運作的?

該PDF檔案已設定密碼保護。 嘗試開啟檔案時,會出現密碼提示。 請輸入正確的密碼以存取 PDF 檔案的內容。所有者密碼提供額外的管理權限,讓您稍後修改安全設定。

閱讀更多有關其他安全性和元資料設定的信息,並探索PDF 加密和解密功能。

完整的原始碼是什麼?

本教學的完整原始檔如下所示:

# Import statement for IronPDF for Python
from ironpdf import *

# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
# Export to a file or Stream
pdf.SaveAs("htmlstring_to_pdf.pdf")

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Export to a file or Stream
pdf.SaveAs("url.pdf")

# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")
# Import statement for IronPDF for Python
from ironpdf import *

# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This is an example HTML string.</p>")
# Export to a file or Stream
pdf.SaveAs("htmlstring_to_pdf.pdf")

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")

# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com")
# Export to a file or Stream
pdf.SaveAs("url.pdf")

# Set user password for PDF document security
pdf.SecuritySettings.UserPassword = "sharable"
# Save the password-protected PDF
pdf.SaveAs("protected.pdf")
PYTHON

IronPDF能夠準確渲染所有圖像和文本,同時保留其格式。 產生的 PDF 檔案中,按鈕等互動元素仍可點擊,文字方塊仍可編輯。

有哪些關鍵要點?

在本操作指南中,我們探討了使用IronPDF庫在 Python 中建立 PDF的過程。 借助IronPDF,開發人員可以有效率地產生和處理 PDF 文件。

該程式庫提供了一個 API,可以簡化從各種來源(包括 HTML 檔案、XML 文件、URL 等)建立 PDF 的過程。 無論您是產生報告、發票或任何其他類型的文檔, IronPDF都提供了完成任務所需的必要工具。

IronPDF是一個商業庫,需要有效的許可證才能使用。 它擁有商業許可證,許可證號碼從 $799 開始。 要評估其在生產環境中的功能,您可以利用免費試用許可證金鑰。

下載軟體產品。

常見問題解答

如何安裝用於建立 PDF 檔案的 Python 函式庫?

使用 pip 安裝 IronPDF,指令為: pip install ironpdf。首先確保您的系統已安裝 Python 3.x 和 .NET 6.0 SDK,因為 IronPDF Python 依賴於 IronPDF for .NET 函式庫。

在 Python 中將 HTML 轉換為 PDF 的最簡單方法是什麼?

最簡單的方法是使用 IronPDF 的 RenderHtmlAsPdf 方法。只要匯入 IronPDF、建立 ChromePdfRenderer 實例,並使用 renderer.RenderHtmlAsPdf('Your HTML content') 將 HTML 字串直接轉換為 PDF 文件即可。

我可以從現有的 HTML 檔案建立 PDF 嗎?

是的,IronPDF 提供了 RenderHtmlFileAsPdf 方法,可讓您直接從儲存於系統中的 HTML 檔案產生 PDF 檔案,並維持所有格式與樣式。

如何在 Python 中將網頁 URL 轉換為 PDF?

使用 IronPDF 的 RenderUrlAsPdf 方法從任何 URL 建立 PDF 檔案。該函式庫會完全呈現網頁在瀏覽器中的樣子,並將其轉換為 PDF 文件。

用 Python 創建 PDF 需要哪些先決條件?

您需要 Python 3.x、pip 包管理器、.NET 6.0 SDK(因為 IronPDF Python 依赖于 .NET 庫)以及通過 pip 安装的 IronPDF 庫。

我可以在生成的 PDF 中加入安全功能嗎?

是的,IronPDF 允許您匯出受密碼保護的 PDF 檔案,並透過密碼加密保護您的文件,確保您所產生的 PDF 檔案受到保護。

我可以在 PDF 中加入哪些類型的內容?

使用 IronPDF,您可以在 PDF 中添加文本、圖片和其他內容類型。您也可以自訂字型、顏色,並控制文件版面與格式。

是否可以程式化地合併或壓縮 PDF 檔案?

是的,IronPDF 提供基本 PDF 生成之外的進階功能,包括合併多個 PDF、壓縮 PDF 檔案以及以程式化方式填入 PDF 表單。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明?
執行範例 觀看您的 HTML 變成 PDF。