ここGhostDriver のためのセレンとの特別なコマンドを使用したソリューションは、(それがGhostDriver 1.1.0とPhantomJS 1.9以降動作するはずです。 PhantomJS 1.9.8でテスト6):
#!/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にレンダリングするだけですか? –