Export as PDF using Selenium Webdriver Screenshot

后端 未结 3 560
梦毁少年i
梦毁少年i 2020-12-10 15:23

Does anyone know if it\'s possible to export HTML to PDF using the screenshot feature in Selenium Firefox WebDriver? I have a webpage which has print specific css which I ne

相关标签:
3条回答
  • 2020-12-10 16:07

    Webdriver doesn't support "Export As PDF" function.

    When you are not bound to Firefox and Webdriver, phantomjs could be an alternative. Phantomjs is a headless browser with the ability to take screenshots as PDF. The browser can be controlled directly by javascript.

    Example: http://phantomjs.org/screen-capture.html

    0 讨论(0)
  • 2020-12-10 16:09

    A quick and easy way is to build an HTML file and embed the images as base64 data. You can then use any converter to get the document as a PDF.

    An example with Python:

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("https://www.google.co.uk");
    
    # open new file
    file = open(r"C:\temp\captures.html", "w")
    file.write("<!DOCTYPE html><html><head></head><body width=\"600px\">")
    
    # write image
    file.write("<img src=\"data:image/png;base64,")
    file.write(driver.get_screenshot_as_base64())
    file.write("\">")
    
    # close file
    file.write("</body></html>")
    file.close()
    
    driver.quit()
    
    0 讨论(0)
  • 2020-12-10 16:17

    Screenshots in Selenium are saved as PNG. And PNG and PDF are different kind of formats. So Selenium cannot save your HTML page image directly as a PDF.

    But, you could try to insert the PNG screenshot that Selenium takes and add it to a PDF.

    Check this answer. Basically, you will need a library (like itext) and do something like:

    // Take screenshot
    driver.get("http://www.yourwebpage.com");
    File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshot, new File("screenshot.png"));
    
    // Create the PDF
    Document document = new Document(PageSize.A4, 20, 20, 20, 20);
    PdfWriter.getInstance(document, new FileOutputStream("my_web.pdf"));
    document.open();
    Image image = Image.getInstance(getClass().getResource("screenshot.png"));
    document.add(image);
    document.close();
    

    Hope it helps!

    EDIT

    Since webs can be pretty high, you will probably need to check the documentation to see how you want to set your image in a PDF file.

    0 讨论(0)
提交回复
热议问题