Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python을 사용하여 HTML을 PDF로 변환하는 방법

<시간/>

Python은 HTML 문서를 PDF로 변환하는 Pdfcrowd API v2를 제공합니다. 이 API는 사용이 매우 간편하며 몇 줄의 코드만으로 통합할 수 있습니다.

설치

Install the client library from PyPI
$ pip install pdfcrowd

웹페이지/HTML에서 PDF로 다음 3단계로 변환이 완료됩니다.

1단계 − 라이브러리 pdfkit 다운로드

$ pip install pdfkit

2단계 − 이제 wkhtmltopdf를 다운로드하세요.

우분투/데비안의 경우 -

sudo apt-get install wkhtmltopdf

Windows용 -

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

3단계 − 다운로드할 Python 코드 −

(i) 이미 저장된 HTML 페이지

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

(ii) 웹사이트 URL로 변환

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

(iii) PDF에 텍스트 저장

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