About
æ³è¦å°HTML转为PDFï¼å¯ä»¥ä½¿ç¨å端çæä»¶ï¼ä½èº«ä¸ºä¸ä¸ªPythonçï¼å端åèçä¸è¦ä¸è¦çï¼åªè½æ³æäº.....
ç»è¿ä¸çªå¦æ¤è¿è¬åï¼æå³å®ä½¿ç¨
wkhtmltopdf
è¿ä¸ªé常强大çå·¥å
·ã
åç»è¿ä¸çªå¦æ¤è¿è¬åï¼æåå¤ä½¿ç¨
wkhtmltopdf
å°è£
çPythonå
PDFKit
ã
install wkhtmltopdf
https://www.cnblogs.com/Neeo/articles/11566990.html
install pdfkit
pip install pdfkit
usage wkhtmltopdf
API说æ
æä»¬å¸¸ç¨PDFKit
çä¸ä¸ªAPIï¼
from_urlï¼å°è¿ç¨URL页é¢å¯¼åºä¸ºPDFã
from_fileï¼å°HTMLæä»¶å¯¼åºä¸ºPDFã
from_stringï¼å°å符串导åºä¸ºPDFã
import pdfkit
pdfkit.from_url('https://www.google.com.hk','out1.pdf')
pdfkit.from_file('123.html','out2.pdf')
pdfkit.from_string('Hello!','out3.pdf')
from_url
def from_url(url, output_path, options=None, toc=None, cover=None,
configuration=None, cover_first=False):
Convert file of files from URLs to PDF document
:param url: å°ä¸ä¸ªæå¤ä¸ªurl页é¢å¯¼åºPDF
:param output_path: 导åºPDFçæä»¶è·¯å¾ï¼å¦æä¸ºFalseï¼å°ä»¥å符串形å¼è¿åã
:param options: å¯éç optionsåæ°ï¼æ¯å¦è®¾ç½®ç¼ç
:param toc: (å¯é)为PDFæä»¶çæç®å½
:param cover: (å¯é)ï¼ä½¿ç¨HTMLæä»¶ä½ä¸ºå°é¢ãå®ä¼å¸¦é¡µçå页èçTOCä¹åæå
¥
:param configuration: (å¯é) ä¸äºé
ç½®ï¼æ¥èª pdfkit.configuration.Configuration()
:param configuration_first: (optional) if True, cover always precedes TOC
Returns: True on success
r = PDFKit(url, 'url', options=options, toc=toc, cover=cover,
configuration=configuration, cover_first=cover_first)
return r.to_pdf(output_path)
import pdfkit
# éè¦æå®wkhtmltopdf.exeçè·¯å¾ï¼å°±ç®ä½ æ·»å äºpath.....
config_pdf = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_url(url='https://www.cnblogs.com/Neeo/articles/11566990.html', output_path='p3.pdf', configuration=config_pdf)
from_file
def from_file(input, output_path, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False):
Convert HTML file or files to PDF document
:param input: path to HTML file or list with paths or file-like object
:param output_path: path to output PDF file. False means file will be returned as string.
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) string with path to css file which will be added to a single input file
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param configuration_first: (optional) if True, cover always precedes TOC
Returns: True on success
r = PDFKit(input, 'file', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first)
return r.to_pdf(output_path)
import pdfkit
config_pdf = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_file(input='h.html', output_path='p2.pdf', configuration=config_pdf)
å¯ä»¥æå¤ä¸ªæä»¶ï¼
import pdfkit
options = {
"encoding": "UTF-8",
"custom-header": [('Accept-Encoding', 'gzip')],
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
# 'encoding': "UTF-8",
'no-outline': False
config_pdf = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_file(input=['h.html', 'w.html'], output_path='p2.pdf', configuration=config_pdf, options=options)
å¯ä»¥æ·»å cssæä»¶ï¼
css='example.css'
pdfkit.from_file('file.html', options=options, css=css)
# Multiple CSS files
css=['example.css','example2.css']
pdfkit.from_file('file.html', options=options, css=css)
from_string
def from_string(input, output_path, options=None, toc=None, cover=None, css=None,
configuration=None, cover_first=False):
Convert given string or strings to PDF document
:param input: 带ææéææ¬çå符串ãå¯ä»¥æ¯åå§ææ¬æhtmlæä»¶
:param output_path: è¾åºPDFæä»¶çè·¯å¾ãFalse表示æä»¶å°ä½ä¸ºå符串è¿å
:param options: (optional) dict with wkhtmltopdf options, with or w/o '--'
:param toc: (optional) dict with toc-specific wkhtmltopdf options, with or w/o '--'
:param cover: (optional) string with url/filename with a cover html page
:param css: (optional) å°æ·»å å°è¾å
¥å符串çcssæä»¶çè·¯å¾
:param configuration: (optional) instance of pdfkit.configuration.Configuration()
:param configuration_first: (optional) if True, cover always precedes TOC
Returns: True on success
r = PDFKit(input, 'string', options=options, toc=toc, cover=cover, css=css,
configuration=configuration, cover_first=cover_first)
return r.to_pdf(output_path)
import pdfkit
config_pdf = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_string(input='hello pdfkit wkhtmltopdf', output_path='p4.pdf', configuration=config_pdf
æ¬¢è¿æ§æ£ï¼that's all
see alsoï¼[PDFä¹pdfkit](https://www.cnblogs.com/niejinmei/p/8157680.html) | [Pythonæåç½é¡µå¹¶ä¿å为PDF]()