2016-11-27 147 views
0

如何以页眉和页脚的PDF格式打印网页,使其看起来像是通过谷歌浏览器打印的。Print page as pdf selenium python

这是我迄今为止使用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, []) 

回答

0

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