How to Track Web Page Changes with Automated Screenshots
In this tutorial we'll take a look at website change tracking using Python, Playwright and Wand. We'll build a tracking tool and schedule it to send us emails on detected changes.
Screenshots are a convenient way to capture exactly what you see on a screen, but they aren’t always the ideal format for long-term storage or professional documentation.
That’s where PDF conversion comes in. By turning your screenshots into PDF files, you gain the ability to compile, organize, and share multiple images in a single, universally accessible format.
This article will guide you through the process of how to turn a screenshot into a PDF using Python and Node.js and tools like Selenium.
Converting a screenshot to a PDF involves transforming a digital image file (usually in formats like PNG, JPEG, or BMP) into a Portable Document Format (PDF) file. A screenshot captures a static image of what is displayed on a screen, such as a webpage, an application, or a specific user interface.
Converting screenshots to PDFs offers more than just format flexibility. Here’s a closer look at the key advantages:
While screenshots in formats like PNG or JPEG are great for quick sharing, PDFs are often a better choice for professional or long-term use. Here’s why:
Converting screenshots to PDF ensures your visual content is well-organized, professional, and universally accessible.
Converting screenshots to PDFs offers practical applications across various fields:
By converting screenshots to PDFs, you turn simple visual captures into structured, professional documents tailored to your specific needs.
To automate the process of converting screenshots into PDFs, you’ll need reliable libraries that simplify PDF generation and provide robust features. Here’s a closer look at some popular choices in Python and Node.js, along with their capabilities:
Python offers several libraries tailored for generating and manipulating PDFs, each with unique strengths. Here’s a breakdown of the most popular options and their use cases:
pdfkit
A reliable choice for converting HTML content into PDFs, especially when working with styled templates and dynamic web content.
wkhtmltopdf
tool, which converts HTML content to PDF.FPDF
Perfect for creating PDFs from scratch when you need precise control over the layout and design of the document.
PyPDF2
An essential tool for manipulating existing PDF files, ideal for workflows involving tasks like merging or splitting documents.
FPDF
for workflows involving both creation and modification.These libraries make PDF handling in Python efficient, catering to a variety of professional and creative needs.
Node.js provides a variety of libraries to streamline PDF generation and manipulation, offering options for both simple tasks and advanced workflows. Below are the key tools and their use cases:
pdfkit
A versatile library for transforming HTML or web content into polished, styled PDFs with professional-grade features.
wkhtmltopdf
for HTML-to-PDF conversion.pdfmake
Ideal for generating PDFs programmatically with structured content and customizable layouts for tables, text, and other elements.
Puppeteer
A powerful tool for capturing live web pages and rendering them as pixel-perfect PDFs, complete with styles and dynamic content.
These libraries cater to various PDF-related tasks in Node.js, from basic HTML-to-PDF conversion to complex, dynamic document generation.
Here’s a summary table to help you choose the right library for PDF generation based on your specific needs:
Requirement | Best Python Library | Best Node.js Library | Key Features |
---|---|---|---|
HTML-to-PDF Conversion | pdfkit |
pdfkit |
Converts styled HTML to PDF, supports CSS and JavaScript rendering. |
Dynamic Web Page to PDF | pyppeteer |
Puppeteer |
Captures live web pages, renders JavaScript, and preserves page styles and layouts. |
Custom PDF Creation | FPDF |
pdfmake |
Enables manual layout control with text, images, tables, and shapes. |
Manipulating Existing PDFs | PyPDF2 |
Not Available | Splits, merges, rotates, and extracts content from PDF files. |
Dynamic Document Layout | Limited | pdfmake |
Generates structured layouts with tables, headers, and multi-language text support. |
Complex Document Styling | pdfkit |
Puppeteer , pdfkit |
Suitable for professional-grade reports with precise margins, headers, and footers. |
Automated Workflows for Screenshots | pyppeteer |
Puppeteer |
Combines screenshot capture with PDF generation in a seamless automated pipeline. |
By leveraging these libraries, you can efficiently create, customize, and manage PDFs in your automation workflows. Each library has unique strengths, so your choice depends on your specific use case and project requirements.
To automate screenshot capture, scraping libraries like Selenium for Python and Puppeteer for Node.js are excellent choices. They allow you to programmatically interact with web pages and save screenshots for further processing. Here’s how to use each library:
Selenium is a powerful web automation tool that supports browser-based interactions. Below is an example of using Selenium to capture a screenshot:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# Configure Chrome to run in headless mode (no visible UI)
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)
# Navigate to the target URL
driver.get("https://web-scraping.dev/product/1")
# Capture screenshot and save as a PNG file
driver.save_screenshot("screenshot.png")
# Close the browser
driver.quit()
In the example, we configure Chrome to run in headless mode, enabling it to operate without a visible user interface. The script navigates to the target URL (https://web-scraping.dev/product/1
) and uses the save_screenshot
method to capture and save the visible webpage as a PNG file.
Puppeteer is a Node.js library that controls a headless version of Chromium for automating tasks like screenshot capture. Here’s how to use Puppeteer:
const puppeteer = require("puppeteer");
(async () => {
// Launch a headless Chromium browser
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
// Navigate to the target URL
await page.goto("https://web-scraping.dev/product/1");
// Capture screenshot and save as a PNG file
await page.screenshot({ path: "screenshot.png" });
// Close the browser
await browser.close();
})();
The example demonstrates launching a headless browser, opening a new page, and navigating to the same URL. The page.screenshot
method captures a screenshot and saves it as a PNG file.
Both tools allow you to automate screenshot capture efficiently, with flexibility to navigate and interact with dynamic web pages.
ScrapFly provides web scraping, screenshot, and extraction APIs for data collection at scale.
What if we want to take a screenshot of multiple pages as a single PDF. For that we can capture multiple URLs and then later stictch everything together.
const puppeteer = require("puppeteer");
(async () => {
const browser = await puppeteer.launch({ headless: true });
const urls = [
"https://web-scraping.dev/product/1",
"https://web-scraping.dev/product/2",
];
for (let i = 0; i < urls.length; i++) {
const page = await browser.newPage();
await page.goto(urls[i]);
await page.screenshot({ path: `page_${i}.png` });
await page.close();
}
await browser.close();
})();
This approach is handy when you need automated screenshots for many pages without manually restarting the browser each time.
After capturing screenshots, the next step is to convert them into PDF files. Whether you're working with a single image or multiple screenshots, Python and Node.js offer simple, efficient libraries to handle the process. Below are clear examples for both languages:
Converting a single screenshot to a PDF using Python is straightforward with the Pillow library.
from PIL import Image
# Open the screenshot
image = Image.open("screenshot.png")
# Convert the image to RGB mode and save as PDF
image.convert("RGB").save("output.pdf")
print("PDF created successfully!")
This script opens a screenshot, converts it to RGB mode (as the PDF format does not support transparency), and saves it as output.pdf
. It’s a simple and effective method for creating a single-page PDF.
In Node.js, the pdfkit
library makes it easy to convert a single image into a PDF.
const PDFDocument = require("pdfkit");
const fs = require("fs");
const doc = new PDFDocument();
const writeStream = fs.createWriteStream("output.pdf");
// Add the image to the PDF
doc.image("screenshot.png", {
fit: [500, 500], // Adjust size as needed
align: "center",
valign: "center",
});
doc.end();
writeStream.on("finish", () => {
console.log("PDF created successfully!");
});
doc.pipe(writeStream);
This code initializes a PDF document, adds an image with adjustable dimensions, and saves the output as output.pdf
. The fit
option ensures the image fits within the page boundaries.
Combining multiple screenshots into a single PDF file is simple with Pillow.
from PIL import Image
# List of image files
images = ["screenshot1.png", "screenshot2.png"]
# Convert and append images into a single PDF
pdf_images = [Image.open(img).convert("RGB") for img in images]
pdf_images[0].save("combined_output.pdf", save_all=True, append_images=pdf_images[1:])
print("Combined PDF created successfully!")
The script loads multiple images, converts them to RGB, and combines them into a single multi-page PDF file. The save_all=True
option ensures all the images are appended into one PDF.
Node.js also provides an efficient way to combine multiple screenshots into a single PDF using pdfkit
.
const PDFDocument = require("pdfkit");
const fs = require("fs");
const doc = new PDFDocument();
const writeStream = fs.createWriteStream("combined_output.pdf");
const images = ["screenshot1.png", "screenshot2.png"];
images.forEach((imgPath) => {
doc.addPage().image(imgPath, {
fit: [500, 500],
align: "center",
valign: "center",
});
});
doc.end();
writeStream.on("finish", () => {
console.log("Combined PDF created successfully!");
});
doc.pipe(writeStream);
This script iterates through an array of image paths, adds each image as a new page in the PDF, and saves the result as combined_output.pdf
. The fit
option ensures the images are sized appropriately for each page.
These methods allow you to efficiently convert screenshots into PDFs, making them easier to manage, share, and present.
To wrap this introduction up let's take a look at some frequently asked questions regarding screenshot to pdf.
Yes, you can. Both Python (using Pillow) and Node.js (using pdfkit
) support combining multiple
screenshots into a single, multi-page PDF file. This is particularly useful for organizing sequences of
images, like a tutorial or report.
It depends on the settings you use. Libraries like Pillow
and pdfkit
ensure minimal quality loss if the original image resolution is preserved. However, resizing or compressing images may reduce quality.
No, the conversion process can be done offline as long as the necessary libraries are installed and the screenshots are saved locally. However, capturing screenshots from live webpages requires an internet connection.
Converting screenshots to PDFs is a useful and efficient way to document and share visual content. Whether you're using Python with Selenium and Pillow or Node.js with Puppeteer and pdfkit, the process is straightforward and highly customizable.
With the right tools and techniques, you can easily streamline your documentation process.