打印页面为pdf selenium python

时间:2016-11-27 12:53:10

标签: python html selenium pdf webpage

如何将网页打印为带有页眉和页脚的PDF,使其看起来像是从谷歌浏览器打印出来的。

这是我到目前为止使用PhantomJS尝试过的,但它没有页眉和页脚。

from selenium import webdriver
import selenium

def execute(script, args):
    driver.execute('executePhantomScript', {'script': script, 'args' : args })

driver = webdriver.PhantomJS('phantomjs')

# hack while the python interface lags
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')

driver.get('http://stackoverflow.com')

# set page format
# inside the execution script, webpage is "this"
pageFormat = '''this.paperSize = {format: "A4", orientation: "portrait" };'''
execute(pageFormat, [])

# render current page
render = '''this.render("test.pdf")'''
execute(render, [])

1 个答案:

答案 0 :(得分:0)

pdfkit可能会帮助你。它将从html页面呈现PDF。