Вот решение, используя селен и специальную команду для GhostDriver (она должна работать так GhostDriver 1.1.0 и PhantomJS 1,9. 6, протестирована с PhantomJS 1.9.8):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Download a webpage as a PDF."""
from selenium import webdriver
def download(driver, target_path):
"""Download the currently displayed page to target_path."""
def execute(script, args):
driver.execute('executePhantomScript',
{'script': script, 'args': args})
# hack while the python interface lags
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
# set page format
# inside the execution script, webpage is "this"
page_format = 'this.paperSize = {format: "A4", orientation: "portrait" };'
execute(page_format, [])
# render current page
render = '''this.render("{}")'''.format(target_path)
execute(render, [])
if __name__ == '__main__':
driver = webdriver.PhantomJS('phantomjs')
driver.get('http://stackoverflow.com')
download(driver, "save_me.pdf")
смотри также мой ответ на тот же вопрос here.
Вы посмотрели на Pypdf2? http://www.blog.pythonlibrary.org/tag/python-pdf-series/ – Amit
@Amit: Довольно широко, поскольку я использую его все время. Даже сами Phaseit сказали, что «PyPDF2 не знает HTML». Он не будет надежно отображать любой HTML. – Rejected
@Rejected вам нужен скриншот, который будет происходить в точном состоянии во время тестирования? Или вы просто хотите загрузить страницу и визуализировать в PDF? –