IronPDF 操作指南 創建 PDF 如何在 Python 中建立 PDF 文件 Curtis Chau 更新:1月 10, 2026 下載 IronPDF pip 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 This article was translated from English: Does it need improvement? Translated View the article in English 使用 IronPDF for Python 函式庫在 Python 中建立 PDF 檔案,只需幾行程式碼即可將 HTML 字串、HTML 檔案或 URL 轉換為 PDF 文件。 IronPDF 會自動處理渲染、格式化和安全功能。 Quickstart: Create PDF in Python :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 Scripts 中以程式化的方式建立 PDF 檔案。 如何在 Python 中創建 PDF 文件 安裝 Python 庫以建立 PDF 文件 使用RenderHtmlAsPdf方法將 HTML 字串轉換為 PDF 文檔 使用RenderHtmlFileAsPdf方法直接從 HTML 檔案產生 PDF 文件 利用RenderUrlAsPdf方法從 URL 建立 PDF 文件 將受密碼保護的 PDF 文件匯出到所需目錄 什麼是 IronPDF Python PDF Library? IronPDF 是專為從 HTML 建立 PDF 文件而設計的 Python 函式庫。 其 API 可讓您直接產生並自訂 PDF 的各種功能,包括 1.新增文字、圖片及其他內容類型 2. 選擇字體、顏色,以及控製文件版面和格式。 IronPDF 可集成到 [.NET](/)、[Java](/java/) 和 [Python](/python/) 应用程序中,从而实现跨平台生成 PDF。 除了 PDF 生成之外,IronPDF 還提供其他功能。 這些功能包括檔案格式轉換、從 PDF 中提取文字和資料,以及透過密碼加密保護 PDF 的能力。 對於進階使用案例,請探索如何 [合併多個 PDF](https://ironpdf.com/python/how-to/python-merge-pdf/)、[壓縮 PDF 檔案](https://ironpdf.com/python/how-to/python-compress-pdf/),或 [以程式化方式填入 PDF 表單](https://ironpdf.com/python/how-to/python-fill-pdf-form/)。 在 Python 腳本中建立 PDF 文件的步驟是什麼? -->。 <!--說明:顯示逐步過程的截圖 --> 我需要哪些先決條件? 要使用 IronPDF for Python,請確保您的電腦已安裝下列軟體: 1.`.NET 6.0 SDK`:必須使用,因為 IronPDF Python 依賴於 IronPDF for .NET 函式庫。 從 [ Microsoft 官方網站](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)下載。 2.`Python`:從 https://www.python.org/downloads/ 下載並安裝 Python 3.x。 在安裝過程中,選擇將 Python 加入 PATH 的選項。 3.`Pip`:通常與 Python 3.4+ 綁定。 若有需要,可驗證安裝或另行安裝。 4.`IronPDF Library`:使用 pip 以下列指令安裝: ```shell :ProductInstall ``` 在某些系統中,Python 2.x 可能仍然是預設版本。 在這種情況下,您可能需要明確地使用 pip3 指令而不是 pip,以確保您使用的是 Python 3 的 Pip。如果您遇到安裝錯誤,請參考我們的 [OSError 問題排除指南](https://ironpdf.com/python/troubleshooting/could-not-install-package/)。)}] IronPDF for Python 在建立 PDF 之前需要進行哪些程式碼設定? 首先,在您的 Python 腳本頂端加入以下語句: ```python # Import statement for IronPDF for Python from ironpdf import * ``` 接下來,透過將許可證密鑰指派給 License 的 LicenseKey 屬性(在任何其他程式碼行之前),使用有效的許可證密鑰配置 IronPDF。 如需在專案中實作授權金鑰的詳細說明,請造訪我們的 [ 授權金鑰設定指南](https://ironpdf.com/python/get-started/license-keys/)。 ```python # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" ``` [{i:(要建立不含浮水印的 PDF,您需要有效的許可證金鑰。 [Purchase](/python/licensing/) a license key or obtain a free trial license key. 否則,請繼續執行下一步,免費產生帶有浮水印的新 PDF 文件。 如何將 HTML 字串轉換成 PDF 文件? --> HTML 字串轉換應使用何種方法? 使用`RenderHtmlAsPdf`方法從 HTML 字串產生新的 PDF 文件。 此方法支援 CSS 造型、JavaScript 執行和自訂字型。 提供 HTML 標記作為 `RenderHtmlAsPdf` 方法的參數。 IronPDF 將執行轉換,產生`PdfDocument`實例。 ```python # Instantiate Renderer renderer = ChromePdfRenderer() # Create a PDF from an HTML string using Python pdf = renderer.RenderHtmlAsPdf("This is an example HTML string.") ``` 如何保存生成的 PDF? 一旦 HTML 字串轉換成 PDF 文件,請使用 `SaveAs` 方法將 PDF 儲存到本機系統上的路徑: ```python # Export to a file or Stream pdf.SaveAs("htmlstring_to_pdf.pdf") ``` 將建立一個名為`"htmlstring_to_pdf.pdf"`的PDF文件,並保留原始HTML字串的內容。 如需瞭解更多進階的 HTML 渲染技術,包括使用複雜的佈局和 JavaScript 框架,請參閱我們全面的 [HTML to PDF 教程](https://ironpdf.com/python/tutorials/html-to-pdf/)。 如何用 Python 從 HTML 檔生成 PDF? <!--說明:說明程式碼概念的圖表或截圖 --> 轉換本機 HTML 檔案的流程為何? 若要從本機儲存於 Python 的 HTML 檔案產生 PDF 文件,請遵循下列程式碼。 [RenderHtmlFileAsPdf 方法](https://ironpdf.com/python/examples/file-to-pdf/)提供了轉換現有 HTML 文件的直接方法: ```python # 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") ``` 為什麼 IronPDF 會保留 HTML 格式? 在上面的程式碼中,`RenderHtmlFileAsPdf` 方法會從 HTML 檔案建立 PDF 文件。提供一個字串或路徑,指定 HTML 檔案在檔案系統中的位置。 IronPDF 會像網頁瀏覽器一樣渲染 HTML 元素,包括任何相關的 CSS 和 JavaScript。 這樣可以確保產生的 PDF 文件中的內容準確呈現。 該函式庫支援 CSS3、HTML5 和 JavaScript 框架等現代網路標準,因此非常適合將複雜的網路文件轉換為 PDF 格式。 使用 `SaveAs` 方法將產生的 PDF 儲存到您系統上的特定位置,類似於之前的範例。 如何在 Python 中從 URL 建立 PDF? 什麼方法可將網頁轉換為 PDF? 若要在 Python 中從網頁建立 PDF 文件,請使用 `RenderUrlAsPdf` 方法。 提供所需網頁的 URL 作為方法的參數: ```python # 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") ``` URL 渲染功能支援現代網路技術,包括動態 JavaScript 內容、AJAX 呼叫和回應式佈局。 IronPDF 在轉換之前會等待頁面完全載入,以確保準確擷取所有內容。 我在哪裡可以找到更多 URL 轉換範例? 有關網頁轉換為 PDF 的更多資訊,請參閱 [URL 轉換為 PDF 的程式碼範例](https://ironpdf.com/python/examples/converting-a-url-to-a-pdf/)頁面。 對於需要驗證的網站,請探索我們的 [ 網站和系統登入指南](https://ironpdf.com/python/examples/ironpdf-website-and-system-logins/)。 如何自訂 PDF 格式化選項? 我可以在 RenderingOptions 中修改哪些設定? 若要自訂 PDF 檔案的格式,請使用 `RenderingOptions` 屬性。 此類別提供各種可配置設置,以實現所需的 PDF 文件佈局和外觀。 設定包括頁面方向、頁面大小、頁邊距大小等。 設定`RenderingOptions`中的可用屬性,以產生具有所需設定的 PDF 文件。 有關如何使用 `RenderingOptions` 的詳細資訊,請參閱此 [Code Example](https://ironpdf.com/python/examples/pdf-generation-settings/)。 其他自訂選項包括 - 設定自訂頁邊空白 - 配置縱向和橫向 - 定義自訂 PDF 頁面大小 - 管理頁碼和分頁符號 如何使用密碼保護 PDF 檔案? 新增密碼保護的流程是什麼? 若要為 PDF 檔案加入密碼保護,請使用 `PdfDocument` 物件的 `SecuritySettings` 屬性。 存取 `SecuritySettings` 屬性,並將密碼指定給 `UserPassword` 屬性,指定為字串。 如需瞭解更多進階的安全功能,包括加密和數位簽章,請造訪我們的 [ 安全與元資料範例頁面](https://ironpdf.com/python/examples/security-and-metadata/)。 例如,考慮保護"URL 至 PDF"範例中建立的 PDF 文件: ```python # 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") ``` 密碼保護在實務中如何運作? PDF 檔案已設定密碼保護。 嘗試開啟檔案時,會出現密碼提示。 輸入正確的密碼以存取 PDF 檔案的內容。擁有者密碼可提供額外的管理權限,讓您稍後修改安全設定。 閱讀更多有關 [ 附加安全性和元資料設定](https://ironpdf.com/python/examples/security-and-metadata/)的資訊,並探索 [ IronPDF for Python 的加密和解密](https://ironpdf.com/python/examples/encryption-and-decryption/)功能。 什麼是完整的原始碼? 本教學的完整原始檔如下所示: ```python # 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("This is an example HTML string.") # 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") ``` IronPDF能夠準確渲染所有圖像和文本,同時保留其格式。 在產生的 PDF 檔案中,按鈕等互動元素仍可點擊,文字方塊仍可編輯。 關鍵要點是什麼? 在這份 How-To Guide 中,我們探討了 [ 使用 IronPDF for Python 函式庫在 Python 中建立 PDF](https://ironpdf.com/python/how-to/python-create-pdf/) 的過程。 透過 IronPDF,開發人員可以有效率地產生和處理 PDF 文件。 該函式庫提供一個 API,可簡化從各種來源(包括 HTML 檔案、XML 文件、URL 等)建立 PDF 的作業。 無論您是要產生報表、發票或任何其他文件類型,IronPDF 都能提供完成任務所需的工具。 IronPDF 是一個商業庫,需要有效的許可證才能使用。 它具有商業授權,起始價格為 $799 。 若要評估其在生產環境中的功能,您可以利用免費試用授權金鑰。 *[下載軟體產品。](https://ironpdf.com/downloads/python-create-pdf.zip)* 常見問題解答 如何安裝用於建立 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 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Version: 2025.9 剛發表 免費安裝 pip 檢視授權