Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách chuyển HTML sang PDF bằng Python


Python cung cấp Pdfcrowd API v2 chuyển HTML sang PDF. API này dễ dùng, tích hợp chỉ vài dòng code.

Cài đặt

Install the client library from PyPI
$ pip install pdfcrowd

Chuyển đổi hoàn thành trong 3 bước từ Webpage/HTML sang PDF:

Bước 1 - Tải thư viện pdfkit

$ pip install pdfkit

Bước 2 - Tải wkhtmltopdf

Ubuntu/Debian:

sudo apt-get install wkhtmltopdf

Windows:

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

Bước 3 - Code Python để tải:

(i) File HTML đã lưu

import pdfkit
pdfkit.from_file('my_test.html', 'my_output.pdf')

(ii) Chuyển đổi từ URL website

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

(iii) Lưu text vào PDF

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