python打印网页成pdf_python 网页转pdf

主要使用的是wkhtmltopdf的Python封装——pdfkit

centos环境

安装:Install python-pdfkit

pip install pdfkit

安装:Install wkhtmltopdf

yum intsall wkhtmltopdf

windows环境下安装wkhtmltopdf参考这篇文章:

importpdfkit

pdfkit.from_string('hello,python','out.pdf') #通过文本直接进行转换

pdfkit.from_url('http://baidu.com','out.pdf') #通过网址进行转换

pdfkit.from_file('test.html', 'out.pdf') #通过html文件进行转换

我们也可以传递一个url或者文件名列表:

pdfkit.from_url(['google.com', 'yandex.ru', 'engadget.com'], 'out.pdf') pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

也可以传递一个打开的文件:

with open('file.html') as f:

pdfkit.from_file(f,'out.pdf')

如果想对生成的PDF作进一步处理,我们可以将其读取到一个变量中:

#设置输出文件为False,将结果赋给一个变量

pdf = pdfkit.form_url('http://google.com', False)

我们可以制定所有的 wkhtmltopdf 选项 http://wkhtmltopdf.org/usage/wkhtmltopdf.txt. 我们可以移除选项名字前面的 '--' .如果选项没有值, 使用None, Falseor * 作为字典值:

options ={'page-size': 'Letter','margin-top': '0.75in','margin-right': '0.75in','margin-bottom': '0.75in','margin-left': '0.75in','encoding': "UTF-8",'no-outline': None

}

pdfkit.from_url('http://google.com', 'out.pdf', options=options)

默认情况下, PDFKit 将会显示所有的 wkhtmltopdf 输出. 如果不想看到这些信息,你需要传递一个 quiet 选项:

options ={'quiet': ''}

pdfkit.from_url('google.com', 'out.pdf', options=options)

由于wkhtmltopdf的命令语法 , TOC 和 Cover 选项必须分开指定:

toc ={'xsl-style-sheet': 'toc.xsl'}

cover= 'cover.html'pdfkit.from_file('file.html', options=options, toc=toc, cover=cover)

当我们转换文件、或字符串的时候,可以通过css选项指定扩展的 CSS 文件。

#单个 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)

也可以通过HTML中的meta tags传递任意选项:

body = """

Hello World!

"""pdfkit.from_string(body,'out.pdf') #with --page-size=Legal and --orientation=Landscape

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值