Python - pdfkit

Python 转为PDF三种方式

注:首先查看上一篇安装wkhtmltopdf

1. 安装pdfkit

pip install pdfkit

2. 常用PDFKit 的三个API

1). from_url: 将远程URL页面导出为PDF

     示例:

import pdfkit

# 需要指定wkhtmltopdf.exe的路径
config_pdf = pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe')
pdfkit.from_url(url='https://www.cnblogs.com/',output_path='p3.pdf', configuration=config_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)

2). from_file 将HTML文件导出为PDF

    示例:

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)


# 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

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)

3). from_string 将字符串导出为PDF

    示例:

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)


# 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)
posted @ 2021-03-15 15:58  Mr-刘  阅读(556)  评论(0编辑  收藏  举报