driver = webdriver.PhantomJS('phantomjs')
driver.command_executor._commands['executePhantomScript'
    ] = 'POST', '/session/$sessionId/phantom/execute'
driver.get('http://stackoverflow.com')
pageFormat = 'this.paperSize = {format: "A4", orientation: "portrait" };'
execute(pageFormat, [])
render = 'this.render("test.pdf")'
execute(render, [])
How to run webpage code with PhantomJS via GhostDriver (selenium)