Export as PDF using Selenium Webdriver Screenshot

折月煮酒 提交于 2019-11-29 07:17:52
makeMonday

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.

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()

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!