在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PDF(便攜式文件格式)是在線傳送和接收數據的最受歡迎的數位檔案格式。它主要用於保護數據格式和使用加密密碼來確保數據的安全。 .pdf 副檔名不依賴於軟體應用程式、硬體或操作系統。
在本文中,我們將使用 Python 編程語言創建一個 PDF 文件。 有許多線上選項可供選擇,但這裡我們將使用 Python 庫來創建 PDF 文件。 以下是兩個著名的用於在 Python 中生成單頁或多頁 PDF 文件的庫:
Reportlab
PDFKit
從上述提到的 PDF python 函式庫中,我們可以使用任意一個來生成 PDF。
讓我們逐一查看這兩個庫。
Reportlab 庫是一個免費的開放源代碼 PDF 工具包,可用於輕鬆創建 PDF 文件。 它提供了一系列繪圖工具,用於在多個頁面的特定位置添加圖像和文本。 您也可以使用encryptCanvas方法創建加密的 PDF 文件。
要安裝 Reportlab,需要使用 pip 套件管理工具。 它會自動使用 pip 命令下載並安裝 request 套件。 只需在 Windows CMD 或 PowerShell 中输入以下命令:
pip3 install reportlabpip3 install reportlab注意:安裝 Python 時,必須將其添加到環境變數路徑,以便能夠在任何地方使用 cmd 或 PowerShell 執行上述命令。 建議使用 Pip3 來使用 Python 3+,因為它是更新的版本。
要使用 Reportlab 庫,我們需要在一個 Python 文件中編寫程式碼並執行它以創建 PDF 文件。
在 Windows 搜索欄中搜尋並打開 Python 預設的 IDLE Shell,然後按 Ctrl + N 或從「檔案」選項卡中選擇「新檔案」。 這將開啟文字編輯器以撰寫代碼。
接下來,將檔案保存為適當的名稱。 我將其命名為"createpdf.py"。 該文件看起來像這樣:

以下程式碼將繪製文件元素並在幾秒鐘內生成 PDF:
# importing modules
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from reportlab.lib import colors
# initializing variables with values
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Create PDF'
subTitle = 'Using ReportLab !!'
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)
# saving the pdf
pdf.save()py生成的 PDF 輸出如下:

在匯入所有模組和套件後,我們首先初始化將寫入 PDF 檔案的所有內容。
fileName = 'sample.pdf'
documentTitle = 'sample'
title = 'Create PDF'
subTitle = 'Using ReportLab !!'py現在,我們設置了畫布名稱、文件標題、居中的標題和副標題,並使用了適當的字體和大小。
# creating a pdf object
pdf = canvas.Canvas(fileName)
# setting the title of the document
pdf.setTitle(documentTitle)
# creating the title by setting it's font
# and putting it on the canvas
pdf.setFont('abc', 36)
pdf.drawCentredString(300, 770, title)
# creating the subtitle by setting it's font,
# colour and putting it on the canvas
pdf.setFillColorRGB(0, 0, 255)
pdf.setFont("Courier-Bold", 24)
pdf.drawCentredString(290, 720, subTitle)py最後,我們在所有內容都繪製在畫布上時保存 PDF 文件。
# saving the pdf
pdf.save()py我們也可以使用drawString和drawText方法,在 Python 中使用 Reportlab 創建一個簡單的 PDF 文件。
PDFKit 庫是用 Python 創建 PDF 文件的最佳方法之一。 它可以使用HTML代碼創建PDF文件。 您可以將簡單和複雜的 HTML 文件或來自 URL 的 HTML 渲染為像素完美的可列印 PDF 頁面。 然而,PDFKit 整合了 wkhtmltopdf 功能以將 HTML 轉換為 PDF。 若要在 Windows、Linux 和 Mac 上安裝 wkhtmltopdf,請造訪此連結。
注意:在 Windows 作業系統中,wkhtmltopdf 將安裝在程式檔案中。
使用以下命令安裝 PDFKit:
pip3 install pdfkitpip3 install pdfkit使用 PDFKit 創建 PDF 文件非常簡單,只需一行代碼即可完成。
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')pyimport pdfkit
pdfkit.from_file('index.html', 'out.pdf')py您可以將 HTML 範本作為字串傳入,以將其轉換為 PDF 輸出檔案。
html_string = """
<html>
<head>
<meta name="pdfkit-page-size" content="Legal"/>
<meta name="pdfkit-orientation" content="Landscape"/>
</head>
Hello World!
</html>
"""
pdfkit.from_string(html_string, 'out.pdf')pyPDFKit 允許您使用 HTML 模板在 Python 中輕鬆創建 PDF。
IronPDF 是一個在 .NET 專案中 建立 PDF 檔案 的實用工具。 此函式庫的一個常見用途是“HTML 到 PDF” 渲染,其中使用 HTML 作為設計語言來渲染 PDF 文件。
IronPDF 使用 .NET Chromium 引擎將 HTML 頁面渲染成 PDF 文件。 使用 HTML 轉換為 PDF,無需使用複雜的 API 來定位或設計 PDF。 IronPDF 也支援所有標準網頁技術:HTML、ASPX、JS、CSS 和影像。
它還使您能夠使用HTML 5、CSS、JavaScript和圖像創建.NET PDF庫。 您可以輕鬆地編輯、蓋章以及添加頁眉和頁腳到 PDF 文件中。 此外,它使读取 PDF 文本和提取图像变得非常容易。
要開始使用IronPDF,您需要安裝NuGet套件:
pip install ironpdf
以下範例幫助您直接從 URL 建立 PDF:
from ironpdf import *
# 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")py以下代碼將幫助您從HTML代碼以及任何CSS或JavaScript創建PDF文件:
from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
# Export to a file or Stream
pdf.SaveAs("output.pdf")
# Advanced Example with HTML Assets
# Load external html assets: Images, CSS and JavaScript.
# An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", r"C:\site\assets")
myAdvancedPdf.SaveAs("html-with-assets.pdf")py從上述程式碼可以看出,它相當簡單和清晰。 只需很少的程式碼行數即可從 HTML 程式碼建立 PDF 檔案。 這是一個快速、可靠且節省時間的解決方案,提供準確的結果。
下載 IronPDF 並免費試用。 試用期結束後,授權費用起始於$749。