IronPDF 操作指南 創建 PDF How to Create PDF Files in Python Curtis Chau 更新日期:7月 22, 2025 Download IronPDF pip 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 使用 Python 自動化 PDF 文檔的創建,使開發人員能夠將生成 PDF 作為其應用程序的一部分。 此功能在各種場景中顯得非常有用,包括需要時生成發票、報告或其他類型的 PDF。 本指南專注於利用 IronPDF 以編程方式在 Python 腳本中創建 PDF 文件。 class="hsg-featured-snippet"> 如何在 Python 中創建 PDF 文件 安裝用於創建 PDF 文件的 Python 庫 利用RenderHtmlAsPdf方法將 HTML 字符串轉換為 PDF 文檔 使用RenderHtmlFileAsPdf方法從 HTML 文件直接生成 PDF 文件 利用RenderUrlAsPdf方法從 URL 創建 PDF 文件 將密碼保護的 PDF 文件導出到所需目錄 Python PDF 庫:IronPDF IronPDF 是一個強大的 Python 庫,專門用於從 HTML 創建 PDF 文檔。 其用戶友好的 API 使生成和自定義具有各種功能的 PDF 變得簡單,包括: 添加文本、圖像和其他類型的內容 選擇字體、顏色,並控制文檔佈局和格式。 IronPDF can be seamlessly integrated into .NET, Java, and Python applications, enabling versatile PDF generation across multiple platforms. 除了其強大的 PDF 生成功能外,IronPDF 還提供廣泛的功能。 這些功能包括文件格式轉換、高效文本和數據的從 PDF 中提取,以及通過密碼加密來保護 PDF 的能力。 在 Python 腳本中創建 PDF 文檔的步驟 先決條件 要在 Python 中使用 IronPDF,請確保計算機上已安裝以下先決條件軟件: .NET 6.0 SDK:要使用 IronPDF for Python,您需要在計算機上安裝 .NET 6.0 SDK,因為它依賴於 IronPDF .NET 庫。 從微軟官網下載 .NET 6.0 SDK。 Python:從官方 Python 網站:https://www.python.org/downloads/下載並安裝最新版本的 Python 3.x。 在安裝過程中,確保選擇將 Python 添加到系統 PATH 的選項,這樣就可以從命令行訪問它。 Pip:Pip 通常從 Python 3.4 開始隨 Python 安裝捆綁在一起。 但是,取決於您的 Python 安裝,您可能需要檢查 pip 是否已安裝或單獨安裝它。 IronPDF 庫:可以使用 pip 安裝 IronPDF 庫。使用以下命令來安裝 IronPDF: pip install ironpdf 請注意在某些系統上,Python 2.x 可能仍是默認版本。在這種情況下,您可能需要明確使用 pip3 而不是 pip 來確保您是使用 Python 3 的 Pip。 ### 編寫代碼前的重要步驟 首先,在 Python 腳本的頂部添加以下語句。 接下來,通過將許可證密鑰分配給 License 的 LicenseKey 屬性來配置 IronPDF(在任何其他代碼行之前)。 # Import statement for IronPDF for Python from ironpdf import * # Import statement for IronPDF for Python from ironpdf import * PYTHON [{i:(要創建沒有任何水印的 PDF,您需要有效的許可證密鑰。 獲得免費試用許可證密鑰。 否則,繼續下一步以便免費生成新的 PDF 文檔,帶水印。)}] # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" # Apply your license key License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01" PYTHON 將 HTML 字符串轉換為 PDF 文檔 Purchase a license key or obtain a free trial license key. 只需將 HTML 標記提供為 RenderHtmlAsPdf 方法的參數即可。 IronPDF 將進行轉換,生成一個 PdfDocument 實例。 一旦 HTML 字符串成功轉換為 PDF 文檔,使用 SaveAs 方法將 PDF 保存到本地系統上的路徑: 將創建一個名為 "htmlstring_to_pdf.pdf" 的 PDF 文件,保留原始 HTML 字符串的內容。 ## 在 Python 中從 HTML 文件生成 PDF # 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 要從存儲在本地的 HTML 文件中生成 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 在上述代碼片段中,使用了 RenderHtmlFileAsPdf 方法從 HTML 文件創建 PDF 文檔。您需要提供指定 HTML 文件在文件系統中的位置的字符串或路徑。 IronPDF 渲染 HTML 元素,包括任何相關的 CSS 和 JavaScript,就像瀏覽器一樣。 這確保了生成的 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 an existing HTML file using Python pdf = renderer.RenderHtmlFileAsPdf("example.html") # Export to a file or Stream pdf.SaveAs("htmlfile_to_pdf.pdf") PYTHON 最後,像之前的例子一樣,使用 SaveAs 方法將生成的 PDF 保存到系統上的特定位置。 在 Python 中從 URL 創建 PDF 要在 Python 中從網頁創建 PDF 文檔,可以使用 RenderUrlAsPdf 方法。 只需將所需網頁的 URL 作為參數提供給方法,如以下代碼片段所示: 更多有關將網頁轉換為 PDF 的信息可在 URL 到 PDF 代碼示例 頁面上找到。 探索 PDF 格式化選項 要自定義 PDF 文件的格式,您可以利用 RenderingOptions 屬性。 # 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 此類提供各種可配置的設置,以實現所需的 PDF 文檔佈局和外觀。 您可以修改的一些設置包括頁面方向、頁面大小、邊距大小等。 設置RenderingOptions 中的可用屬性,生成具有所需設置的 PDF 文檔。 請參閱此代碼示例以獲取更多有關如何使用 RenderingOptions 的信息。 ## 使用密碼保護 PDF 文件 要向 PDF 文件添加密碼保護,您可以利用 PdfDocument 對象的 SecuritySettings 屬性。 首先訪問 SecuritySettings 屬性,並將密碼分配給指定為字符串的 UserPassword 屬性。 例如,我們可以考慮保護在 "URL 到 PDF" 示例中創建的 PDF 文檔: PDF 文件已成功設置密碼保護。 嘗試打開文件時,將顯示密碼提示。 只需輸入正確的密碼即可訪問 PDF 文件的內容。 # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") # Set user password for PDF document security pdf.SecuritySettings.UserPassword = "sharable" # Save the password-protected PDF pdf.SaveAs("protected.pdf") PYTHON 本教程的完整源文件如下所示: 閱讀有關安全性和元數據設置的更多信息。 ## 完整源代碼 本教程的完整源文件如下所示: IronPDF 準確渲染所有圖像和文本,同時保留其格式。 生成的 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 庫在 Python 中創建 PDF 的過程。 使用 IronPDF,開發人員可以輕鬆生成和操作 PDF 文檔。 該庫提供了一個用戶友好的 API,簡化了從多個來源(包括 HTML 文件、XML 文檔、URL 等)創建 PDF 的過程。 無論您是要生成報告、發票或其他任何類型的文檔,IronPDF 都提供了完成任務所需的工具。 IronPDF 是一個商業庫,需要有效的許可證。 它有一個商業許可證,價格從 $799 開始。 要在生產環境中評估其功能,您可以利用免費試用。 下載軟件產品。 It has a commercial license which starts from $799. To evaluate its capabilities in a production environment, you can take advantage of the free trial. Download the software product. 常見問題解答 如何使用 Python 庫從 HTML 創建 PDF 文件? 您可以使用 IronPDF 庫中的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF 文檔。該方法允許高效地將 HTML 內容轉換為高品質的 PDF。 在 Python 中生成本地 HTML 文件的 PDF 文件有哪些步驟? 使用 IronPDF,您可以使用 RenderHtmlFileAsPdf 方法將本地 HTML 文件轉換為 PDF。只需將您的 HTML 文件路徑作為參數提供以生成 PDF。 IronPDF 能夠用於在 Python 中將網頁轉換為 PDF 文檔嗎? 是的,IronPDF 允許您通過 RenderUrlAsPdf 方法從網頁創建 PDF 文檔。輸入您希望轉換的網頁的 URL,IronPDF 將生成相應的 PDF。 在 Python 中使用 IronPDF 需要哪些先決條件? 要使用 IronPDF,請確保您已在機器上安裝 .NET 6.0 SDK、Python 3.x 和 pip。IronPDF 與 .NET 集成以提供其 PDF 生成功能。 如何使用 IronPDF 自訂 PDF 的佈局和外觀? IronPDF 提供一個 RenderingOptions 屬性,可讓您自訂 PDF 文檔的佈局和外觀,包括頁面大小、方向和邊距大小選項。 使用 IronPDF 是否可以使用密碼保護 PDF 文檔? 是的,IronPDF 允許您通過設定密碼來保護 PDF。訪問 PdfDocument 對象的 SecuritySettings 屬性並設定 UserPassword 以保護您的 PDF。 我需要許可證才能在 Python 專案中使用 IronPDF 嗎? IronPDF 是一個商業庫,需要有效的許可證密鑰。免費試用版可用於評估目的,但需要購買許可證以移除生成的 PDF 上的浮水印。 在哪裡可以找到更多使用 IronPDF 的文檔和示例? IronPDF 的詳細示例和綜合文檔可在 IronPDF 官方網站上找到,其中包括指南和代碼片段以協助有效地使用該庫。 如何安裝 IronPDF 以在 Python 中創建 PDF? IronPDF 可以通過 Python 包管理器 pip 安裝,使用命令 pip install ironpdf。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? 版本: 2025.9 剛剛發布 免費 Pip 安裝 查看許可證