[英]Print page as pdf selenium python
How can I print a webpage as a PDF with headers and footers so it looks like it has been printed from google chrome.如何将带有页眉和页脚的网页打印为 PDF,使其看起来像是从 google chrome 打印出来的。
This is what I have tried so far using PhantomJS but it doesn't have the headers and footers.到目前为止,这是我使用 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, [])
This can be done using Selenium
v4.1.3
by following pyppeteer
examples.这可以通过遵循
pyppeteer
示例使用Selenium
v4.1.3
来完成。
You will need to be able to send commands to chromium and call Page.printToPDF Devtools API as shown in the following snip:您将需要能够向 Chromium 发送命令并调用Page.printToPDF Devtools API,如以下片段所示:
# ...
result = send_cmd(driver, "Page.printToPDF", params={
'landscape': True
,'margin':{'top':'1cm', 'right':'1cm', 'bottom':'1cm', 'left':'1cm'}
,'format': 'A4'
,'displayHeaderFooter': True
,'headerTemplate': '<span style="font-size:8px; margin-left: 5px">Page 1 of 1</span>'
,'footerTemplate': f'<span style="font-size:8px; margin-left: 5px">Generated on {datetime.now().strftime("%-m/%-d/%Y at %H:%M")}</span>'
,'scale': 1.65
,'pageRanges': '1'
})
with open(out_path_full, 'wb') as file:
file.write(base64.b64decode(result['data']))
# ...
def send_cmd(driver, cmd, params={}):
response = driver.command_executor._request(
'POST'
,f"{driver.command_executor._url}/session/{driver.session_id}/chromium/send_command_and_get_result"
,json.dumps({'cmd': cmd, 'params': params}))
if response.get('status'): raise Exception(response.get('value'))
return response.get('value')
I've included a full example in my GitHub Repo .我在我的GitHub Repo中包含了一个完整的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.