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