0

driver.find_element_by_tag_name('body').screenshot(str(int(id)) +'.png')

It is working great with png but does anyone tried saving it as PDF ?

Eegii Enkhtaivan
  • 223
  • 1
  • 10
  • https://stackoverflow.com/questions/33692179/export-as-pdf-using-selenium-webdriver-screenshot You can't save it directly as pdf but you can convert it. – Arundeep Chohan Sep 12 '20 at 21:56
  • I don’t believe you can save it as a PDF. Is there a specific reason you need this? –  Sep 12 '20 at 21:56

2 Answers2

0

You can use the Pillow library to quickly convert an image to PDF. For some reason I got a TypeError using:

driver.find_element_by_tag_name('body').screenshot(str(int(id)) +'.png')

So, I just did it the way I know how.

pip install Pillow

Then

from PIL import Image

browser = webdriver.Firefox()
browser.get('https://google.com')
browser.save_screenshot("image.png")

image = Image.open(r'PATH\filename.png') # get image
img_convert = image.convert('RGB')
img_convert.save(r'PATH\filename.pdf' # save pdf
bendub89
  • 50
  • 6
0

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