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

@exzeo/pdf-diff

v2.0.1

Published

PDF utilities for comparing PDF files

Downloads

6,059

Readme

pdf-diff

Compares two PDFs and provides results on how they differ, including a diff image.

Install

npm install exframe-pdf-diff

Usage

compare

compare function takes two PDFs as input and returns a Promise that is fulfilled with the difference results when the comparison is complete.

const { compare } = require('exframe-pdf-diff');

(async () => {
  const result = await compare('./one.pdf', './two.pdf', {
    outputDirectory: './output'),
  });

  console.log(result);
})();


Output:
{
  baseline: { pageCount: 2 },
  actual: { pageCount: 2 },
  diffs: [
    {
      pageNumber: 1,
      numDiffPixels: 32563,
      percentDiffPixels: 0.06718120749983494,
      diffImageFileName: '/diff_1.png'
    },
    {
      pageNumber: 2,
      numDiffPixels: 0,
      percentDiffPixels: 0,
      diffImageFileName: '/diff_2.png'
    }
  ]
}

chaiPlugin

chaiPlugin is a function that can be passed to chai.use for convenient test assertions when verifying PDFs.

const chai = require('chai');
const { expect } = chai;
chai.use(require('exframe-pdf-diff').chaiPlugin);

describe('generated PDF', () => {
  it('matches baseline', async () => {
    await expect('./new.pdf').to.equalPdf('./baseline.pdf', {
      outputDirectory: './output'),
    });
  });
});

API

compare(pdf1, pdf2, options?)

Returns a Promise that is either fulfilled with the difference results when the comparison is completed successfully, or rejected with an error.

pdf1|pdf2

Type: Buffer | String | Stream

PDF used in comparison. A provided String is assumed to be a file name. A Buffer and Stream is assumed to contain binary PDF data. Note: Stream input will be read into a memory buffer prior to loading the document.

options

Type: object

pageRange

Type: String
Default: '1-' (all pages)

One or more pages to compare. Accepts a comma-separated list (i.e. '1,3,7'), a range (i.e. '1-3'), or combination ('1,5,8-').

outputDirectory

Type: String
Default: (no output files)

When present, will be used to write files for the following cases:

  • Diff images will be written for each page where the actual result does not equal the baseline
  • The actual PDF will be written if the page count differs from the baseline PDF page count
debug

Type: Boolean
Default: false

When true, all intermediate and diff images will be written to the outputDirectory for each compared page, regardless of whether they matched.

Note: This option is ignored if outputDirectory is not set to a valid path.

chaiPlugin

All input and options for the chai plugin are the same as the compare function.