keepcalculator

06 Numbers and Powers

Test Generated PDFs with Automated Screenshots

Card No. 06·62098 · Updated · Standard formula, shown below

This guide shows you how to automatically capture screenshots of generated PDFs to verify their layout, content, and rendering across different pages. You’ll learn a practical workflow using free, scriptable tools, so you can catch visual regressions before they reach users.

What You’ll Need

  • A PDF file you generate (from any source: report, invoice, export, etc.)
  • A computer with Node.js installed (for the example scripts)
  • Basic familiarity with the command line
  • A PDF rendering tool like Poppler (provides pdftoppm) or Ghostscript – install via your package manager (e.g., apt install poppler-utils on Ubuntu, brew install poppler on macOS)
  • Optional: a screenshot comparison tool like pixelmatch (npm package) for automated diffing

Step-by-Step

  1. Set up your project directory. Create a folder for your test scripts and screenshots. Inside, create subfolders: pdfs/ (for input PDFs), screenshots/ (for output images), and diffs/ (for comparison results).
  2. Install required tools. If you haven’t already, install Poppler (for pdftoppm) and initialize a Node.js project with npm init -y. Then install pixelmatch and pngjs for image comparison: npm install pixelmatch pngjs.
  3. Generate a test PDF. Use your application’s existing PDF generation code to produce a sample PDF. Save it to pdfs/sample.pdf. If you don’t have a generator yet, create a simple one with a library like pdfkit (npm) to have something to test.
  4. Convert PDF pages to images. Use pdftoppm to render each page as a PNG. The command: pdftoppm -png -r 150 pdfs/sample.pdf screenshots/page. This creates page-1.png, page-2.png, etc., at 150 DPI. Adjust the DPI for higher or lower resolution.
  5. Write a screenshot script. Create a Node.js script (e.g., capture.js) that runs the conversion command for all pages. Use child_process.execSync to call pdftoppm. This script will be your reusable capture step.
  6. Create a baseline. Run your capture script on a known-good PDF version. Save those screenshots in a baseline/ folder. These are your reference images.
  7. Capture new screenshots. After making changes to your PDF generation code, run the capture script again to produce new screenshots in a current/ folder.
  8. Compare images automatically. Write a comparison script that uses pixelmatch to compare each corresponding page from baseline/ and current/. Output a diff image to diffs/ and log the pixel difference count. If the difference exceeds a threshold (e.g., 0.1%), fail the test.
  9. Integrate into your test runner. Call your capture and compare scripts from your existing test suite (e.g., Jest, Mocha, or a simple shell script). Ensure the test fails when differences are detected, so regressions are caught automatically.
  10. Review diffs manually when needed. If a test fails, open the diff image to see exactly what changed. This helps you decide if the change is intentional (update baseline) or a bug (fix the code).

Common Mistakes

  • Using different DPI between baseline and current. Always use the same resolution, or the comparison will show false differences.
  • Ignoring font rendering differences. Fonts may render slightly differently across operating systems. Run tests on the same OS or use a Docker container for consistency.
  • Forgetting to update baseline after intentional changes. If you deliberately change the layout, update the baseline screenshots to avoid permanent test failures.
  • Comparing entire pages when only a small section changes. This can be noisy. Consider cropping regions of interest (e.g., using pdftoppm with -x -y -W -H options) for focused checks.

Tips

  • Use a low DPI for speed. 72–100 DPI is enough for layout checks and is faster. Use 150+ only when you need to verify fine details.
  • Name screenshots by page number. This makes it easier to identify which page failed.
  • Set a pixel difference threshold. A small tolerance (e.g., 0.05%) avoids flaky tests due to anti-aliasing.
  • Store baseline screenshots in version control. This gives you a history of visual changes.
  • Parallelize capture. If you have many pages, run pdftoppm with the -j option (number of jobs) to speed up rendering.

Troubleshooting

  • Command not found: pdftoppm – Poppler is not installed. Install it via your package manager (e.g., apt install poppler-utils on Debian/Ubuntu, brew install poppler on macOS).
  • Screenshots are blank or black – The PDF may be encrypted or use unsupported features. Try using Ghostscript instead: gs -sDEVICE=png16m -r150 -o page-%d.png file.pdf.
  • Comparison always fails with large differences – Check if the PDF contains dynamic content like timestamps or page numbers. Mask those areas or use a more tolerant threshold. Also ensure the same PDF version is used.
  • Node script cannot find pdftoppm – Make sure the binary is in your PATH. Alternatively, provide the full path in the script.
  • Images are huge and slow to compare – Reduce DPI or use a smaller page size (e.g., crop to the content area).

Frequently Asked Questions

Can I test PDFs without installing anything?

You can use online PDF-to-image converters, but they are not scriptable. For automated testing, local tools like Poppler are necessary.

What if my PDF has multiple pages?

The pdftoppm command generates one image per page automatically. Your comparison script should loop through all pages.

How do I handle PDFs with dynamic content like dates?

Either mask those regions in the comparison, or generate PDFs with fixed test data for screenshot tests.

Can I use this with CI/CD pipelines?

Yes. Install Poppler and Node.js in your CI environment, then run the same scripts. Store baseline screenshots in the repository.

Is it possible to compare only specific parts of a page?

Yes, use pdftoppm with the -x, -y, -W, -H options to crop a region, or use an image editor to crop after conversion.

What if my PDF uses non-standard fonts?

Ensure the fonts are embedded in the PDF. If not, install the fonts on the test machine to avoid rendering differences.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →