npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pdf-visual-comparer

v1.3.5

Published

Enable the visual comparison of two PDFs via pdfjs, canvas and pixelmatch.

Downloads

968

Readme

pdf-visual-comparer

This is a Node.js package that allows you to compare two PDF files and calculate the number of different pixels between their pages. Also provides a custom matcher for playwright.

Installation

To use this package, first install it via npm:

npm install pdf-visual-comparer

Usage

To use the determineDifferentPixelsCount function, import it from the pdf-visual-comparer package and pass in the file paths or buffers of the PDF files you want to compare:

import determineDifferentPixelsCount  from 'pdf-visual-comparer';

const firstPdfSrc = '/path/to/first.pdf';
const secondPdfSrc = '/path/to/second.pdf';

determineDifferentPixelsCount(firstPdfSrc, secondPdfSrc)
  .then((diffPixelsCount) => {
    console.log(`Number of different pixels between PDFs: ${diffPixelsCount}`);
  })
  .catch((error) => {
    console.error(error);
  });

You can also pass in TypedArray or ArrayBuffer objects as the PDF sources.

Playwright Custom Matcher

The custom playwright matcher compares two given PDFs and fails a playwright test if the PDFs have different pixels. In that case a screenshot of the pages with different pixels are attached. In any case both PDFs will be attached to the test result.

To use the toMatchPdf function inside a playwright test, you need to add the following:

Add the global typings reference inside your tsconfig.json. A global.d.ts file with the following content.

{
    "typeRoots": [
        "pdf-visual-compare/typings/global.d.ts"
    ]
}

Extend the playwright expect with toMatchPdf.

import { defineConfig, devices, expect } from '@playwright/test';
import toMatchPdf from 'pdf-visual-comparer/playwright';

expect.extend({
    toMatchPdf
});

Import it from the pdf-visual-comparer/playwright package.

import { expect, test } from '@playwright/test';
import toMatchPdf from 'pdf-visual-comparer/playwright';

test('should match given pdfs', async () => {
    await expect('/path/to/first.pdf').toMatchPdf('/path/to/second.pdf');
});

Pass the testInfo object to toMatchPdf to attach the following to the test result:

  • the expected PDF
  • the actual PDF
  • if two compared pages have different pixels:
    • a screenshot of each page of the expected and actual PDF
    • a screenshot with a visual diff of each page of the PDFs
import { expect, test } from '@playwright/test';
import toMatchPdf from 'pdf-visual-comparer/playwright';

test('should match given pdfs', async ({page}, testInfo) => {
    await expect('/path/to/first.pdf').toMatchPdf('/path/to/second.pdf', testInfo);
});

Pass a threshold to toMatchPdf to allow the amount of pixels to vary between the actual and the expected PDF:

import { expect, test } from '@playwright/test';
import toMatchPdf from 'pdf-visual-comparer/playwright';

test('should match given pdfs with threshold', async ({page}, testInfo) => {
    const threshold = 100;
    await expect('/path/to/first.pdf').toMatchPdf('/path/to/second.pdf', testInfo, threshold);
});

Contributing

Contributions are welcome! If you find a bug or want to add a new feature, please open an issue or pull request on the GitHub repository.

License

This package is licensed under the MIT License. See the LICENSE file for details.