Python + Selenium + PhantomJS render to PDF

前端 未结 4 597
-上瘾入骨i
-上瘾入骨i 2020-12-08 10:52

Is it possible to use PhantomJS\'s rendering to PDF capabilities when PhantomJS is being used in combination with Selenium and Python? (ie. mimic page.ren

相关标签:
4条回答
  • 2020-12-08 11:18

    Here is a solution using selenium and special command for GhostDriver (it should work since GhostDriver 1.1.0 and PhantomJS 1.9.6, tested with 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")
    

    see also my answer to the same question here.

    0 讨论(0)
  • 2020-12-08 11:25

    You could use selenium.selenium.capture_screenshot('file.png') but that will give you a screen shot as a png not a pdf. There does not seem to be a way to get a screenshot as a pdf.

    Here are the docs for capture_screenshot: http://selenium.googlecode.com/git/docs/api/py/selenium/selenium.selenium.html?highlight=screenshot#selenium.selenium.selenium.capture_screenshot

    0 讨论(0)
  • 2020-12-08 11:31

    Tried pdfkit? It can render PDF files from html pages.

    0 讨论(0)
  • 2020-12-08 11:34

    @rejected, I know you mentioned not wanting to use subprocesses, but...

    You may actually be able to leverage subprocess communication more than you anticipated. Theoretically, you could take Ariya's stdin/stdout example and extend it to be a relatively generic wrapper script. It might first accept a page to load, then listen for (& execute) your test actions on that page. Eventually, you could kick off the .render or even make a generic capture for error handling:

    try {
      // load page & execute stdin commands
    } catch (e) {
      page.render(page + '-error-state.pdf');
    }
    
    0 讨论(0)
提交回复
热议问题