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 🙏

© 2026 – Pkg Stats / Ryan Hefner

pdf-merger-ts

v1.0.1

Published

TypeScript library to merge multiple PDF documents, or parts of them, to a new PDF document. Based on pdf-merger-js.

Readme

pdf-merger-ts

TypeScript rewrite of pdf-merger-js

All credit goes to the original author nbesli for creating the original library.

A TypeScript library to merge multiple PDF documents, or parts of them, to one new PDF document. Works in Node.js and directly in the browser.

The only dependency is pdf-lib so it can run in any JavaScript-only environment without any non-JavaScript dependencies.

Credits

This library is a TypeScript rewrite of pdf-merger-js by nbesli. The original library is an excellent JavaScript solution for PDF merging - this version adds full TypeScript support with type definitions included.

Installation

npm install --save pdf-merger-ts

Or for global CLI installation:

npm install -g pdf-merger-ts

Usage

CLI "pdf-merge"

Usage: pdf-merge [options] <inputFiles...>

merge multiple PDF documents, or parts of them, to a new PDF document

Options:
  -V, --version              output the version number
  -o, --output <outputFile>  Merged PDF output file path
  -v, --verbose              Print verbose output
  -s, --silent               do not print any output to stdout. Overwrites --verbose
  -h, --help                 display help for command

Example calls

Merge pages 1-2 from the first input with pages 1,2 and 5-7 from the second pdf document:

pdf-merge --output ./merged.pdf ./input1.pdf#1-2 ./input2.pdf#1,2,5-7

Node.js

The Node.js version has the following export functions:

  • saveAsBuffer exports a merged PDF as a Buffer.
  • save saves the PDF under the given filename.
  • setMetadata sets metadata for producer, author, title or creator.
  • reset resets the internal state of the document, to start again.

Example

import PDFMerger from 'pdf-merger-ts';

const merger = new PDFMerger();

await merger.add('pdf1.pdf');           // merge all pages
await merger.add('pdf2.pdf', 2);        // merge only page 2
await merger.add('pdf2.pdf', [1, 3]);   // merge pages 1 and 3
await merger.add('pdf2.pdf', '4, 7, 8'); // merge pages 4, 7 and 8
await merger.add('pdf3.pdf', '3 to 5'); // merge pages 3 to 5 (3,4,5)
await merger.add('pdf3.pdf', '3-5');    // merge pages 3 to 5 (3,4,5)

// Set metadata
await merger.setMetadata({
  producer: "pdf-merger-ts based script",
  author: "John Doe",
  creator: "John Doe",
  title: "My Document"
});

await merger.save('merged.pdf');

// Or export as Buffer:
// const mergedPdfBuffer = await merger.saveAsBuffer();

Browser

The browser version has the following export functions:

  • saveAsBuffer exports a merged PDF as a Uint8Array.
  • saveAsBlob exports a merged PDF as a Blob.
  • save starts a file download directly in the browser.
  • setMetadata sets metadata for producer, author, title or creator.
  • reset resets the internal state of the document, to start again.

React Example

import PDFMerger from 'pdf-merger-ts/browser';
import React, { useEffect, useState } from 'react';

// files: Array of PDF File or Blob objects
const Merger = ({ files }: { files: File[] }) => {
  const [mergedPdfUrl, setMergedPdfUrl] = useState<string>();

  useEffect(() => {
    const render = async () => {
      const merger = new PDFMerger();

      for (const file of files) {
        await merger.add(file);
      }

      await merger.setMetadata({
        producer: "pdf-merger-ts based script"
      });

      const mergedPdf = await merger.saveAsBlob();
      const url = URL.createObjectURL(mergedPdf);

      setMergedPdfUrl(url);
    };

    render().catch(console.error);

    return () => {
      if (mergedPdfUrl) URL.revokeObjectURL(mergedPdfUrl);
    };
  }, [files]);

  return !mergedPdfUrl ? (
    <div>Loading...</div>
  ) : (
    <iframe
      height={1000}
      src={mergedPdfUrl}
      title="pdf-viewer"
      width="100%"
    />
  );
};

TypeScript Support

This library includes full TypeScript type definitions. The main types are:

import PDFMerger, { 
  Metadata,      // PDF metadata interface
  PdfInput,      // Valid input types
  PageSelection  // Page selection options
} from 'pdf-merger-ts';

API Differences from pdf-merger-js

This library maintains API compatibility with pdf-merger-js. You can switch by simply changing your import:

- import PDFMerger from 'pdf-merger-js';
+ import PDFMerger from 'pdf-merger-ts';

Similar Libraries

License

MIT - See LICENSE file for details.


This library is based on pdf-merger-js by nbesli. Thank you for the excellent work!