如何使用 Python 將 HTML 轉換為 PDF


Python 提供了 Pdfcrowd API v2,可將 HTML 文件轉換為 PDF。此 API 非常易於使用,只需幾行程式碼即可完成整合。

安裝

Install the client library from PyPI
$ pip install pdfcrowd

從網頁/HTML 到 PDF 的轉換將在以下 3 個步驟中完成

步驟 1 − 下載 pdfkit 庫

$ pip install pdfkit

步驟 2 − 現在下載 wkhtmltopdf

對於 Ubuntu/Debian −

sudo apt-get install wkhtmltopdf

對於 Windows −

(a) Download link : WKHTMLTOPDF
(b) Set : PATH variable set binary folder in Environment variables.

步驟 3 − Python 中的下載程式碼 −

(一)已儲存的 HTML 頁面

import pdfkit 
pdfkit.from_file('my_test.html', my_'output.pdf')

(二)透過網站 URL 轉換

import pdfkit
pdfkit.from_url('https://www.google.co.in/','my_testpdf.pdf')

(三)將文字儲存在 PDF 中

import pdfkit
pdfkit.from_string('my_testpdf ABC','testpdf.pdf')

更新於: 2019-07-30

1K+ 瀏覽

開啟您的 職業

完成課程可獲得認證

入門
廣告
© . All rights reserved.