HTML转PDF

1. 安装必要的包:

pip install pdfkit

pip install wkhtmltopdf

apt-get install wkhtmltopdf

2. 关闭display配置:

vim ~/.bashrc

export QT_QPA_PLATFORM='offscreen'

source ~/.bashrc

3. 代码(需要注意html编码方式):

# -*- coding: utf-8 -*-
# @Time    : 2021/9/26 10:49
# @Author  : lijun
import pdfkit
import os
import re


def rectify_charset(html, charset='utf-8'):
    if '<meta charset=' in html:
        re.sub(r'<meta charset=(.*?)>', '<meta charset=\'' + charset + '\'>', html)
    else:
        if '<head>' in html and '</head>' in html:
            re.sub(r'<head>', '<head>\n<meta charset=\'' + charset + '\'>\n', html)
        else:
            html = '<head>\n<meta charset=\'' + charset + '\'>\n</head>\n' + html
    return html


def main():
    options = {
        'page-size': 'Letter',
        'margin-top': '0.75in',
        'margin-right': '0.75in',
        'margin-bottom': '0.75in',
        'margin-left': '0.75in',
        'encoding': "utf-8",
        'custom-header': [('Accept-Encoding', 'gzip')],
        'cookie': [
            ('cookie-name1', 'cookie-value1'),
            ('cookie-name2', 'cookie-value2'),
        ],
        'no-outline': None
    }

    base_path = './'
    in_html_name_list = os.listdir(base_path + '/test_data/my_html_0/')
    out_path = base_path + '/test_out/my_html_0/'

    for idx, html_name in enumerate(in_html_name_list):
        # pdfkit.from_file(
        #     base_path + '/test_data/my_html_0/' + html_name,
        #     out_path + html_name[::-1].split('.', 1)[-1][::-1] + '.pdf',
        #     options=options)

        html = ''.join(open(base_path + '/test_data/my_html_0/' + html_name, 'r', encoding='utf-8').readlines())
        html = rectify_charset(html, 'utf-8')
        pdfkit.from_string(
            html,
            out_path + html_name[::-1].split('.', 1)[-1][::-1] + '.pdf',
            options=options)

        # pdfkit.from_string(
        #     'hello',
        #     out_path + html_name[::-1].split('.', 1)[-1][::-1] + '.pdf',
        #     options=options)

        print('current processed num: ', idx + 1)


if __name__ == '__main__':
    main()

4.相关链接:

安装wkhtmltopdf:Installing wkhtmltopdf · JazzCore/python-pdfkit Wiki · GitHub

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值