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

sign-pdf-lib

v2.0.3

Published

Signs PDF files or just add a placeholder for signature.

Downloads

741

Readme

sign-pdf-lib

Signs pdf files. Uses pdf-lib to add signature to pdf files and node-forge to sign documents and verify the integrity of signed documents.

Installation

npm i sign-pdf-lib

Digital signer usage

Preparation

Instantiate digital signer:

const settings: SignatureSettings {
    signatureLength: ...,
    rangePlaceHolder: ...,

    signatureComputer: {
        certificate: await fse.readFile(...),
        password: '...'
    }
}
const pdfSigner = new PdfDigitalSigner(settings);

or

const settings: SignatureSettings {
    signatureLength: ...,
    rangePlaceHolder: ...,

    psignatureComputer: {
        emCertificate: await fse.readFile(..., 'ascii'),
        pemKey: await fse.readFile(..., 'ascii'),
        certificatePassword: '...'
    }
}
const pdfSigner = new PdfDigitalSigner(settings);

Digital signature parameters preparation:

const parameters: SignDigitalParameters = {
    pageNumber: 1,

    signature: {
        name: 'Test Signer',
        location: 'Timisoara',
        reason: 'Signing',
        contactInfo: '[email protected]'
    },

    visual: {
        rectangle: {
            left: 50,
            top: 641,
            right: 264,
            bottom: 711
        },
        background: await fse.readFile(...),
        texts: [{
            lines: [
                'JOHN',
                'DOE'
            ]}, {
            lines: [
                'Digitally signed by',
                'JOHN DOE',
                'Date: 2023.11.03',
                '20:28:46 +02\'00\''
            ]}
        ]
    }
};

IMPORTANT: if coordinates are negative, they are considered from right or bottom.

For non visual signatures, just omit visual field:

const parameters: SignDigitalParameters = {
    pageNumber: 1,

    signature: {
        name: 'Test Signer',
        location: 'Timisoara',
        reason: 'Signing',
        contactInfo: '[email protected]'
    }
};

If you want a specific name for signature, specify it:

const parameters: SignDigitalParameters = {
    pageNumber: 1,
    name: 'Signature2',
    ...
};

Sign PDF

const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signAsync(pdf, parameters);

Add signature placeholder

const pdf = await fse.readFile(...);
const placeholderPdf = await pdfSigner.addPlaceholderAsync(pdf, parameters);

Digital signature field parameters preparation:

const parameters: AddFieldParameters = {
    pageNumber: 1,

    rectangle: {
        left: 50,
        top: 641,
        right: 264,
        bottom: 711
    }
};

IMPORTANT: if coordinate are negative, they are considered from right or bottom.

If you want a specific name for signature, specify it:

const parameters: AddFieldParameters = {
    pageNumber: 1,
    name: 'Signature2',
    ...
};

Add signature field

const pdf = await fse.readFile(...);
const fieldPdf = await pdfSigner.addFieldAsync(pdf, parameters);

Get signature field list

const pdf = await fse.readFile(...);
const fields = await pdfSigner.getFieldsAsync(pdf);

Field signature parameters preparation:

const parameters: SignFieldParameters = {
    fieldName: 'Signature1,

    signature: {
        name: 'Test Signer',
        location: 'Timisoara',
        reason: 'Signing',
        contactInfo: '[email protected]'
    },

    visual: {
        background: await fse.readFile(...),
        texts: [{
            lines: [
                'JOHN',
                'DOE'
            ]}, {
            lines: [
                'Digitally signed by',
                'JOHN DOE',
                'Date: 2023.11.03',
                '20:28:46 +02\'00\''
            ]}
        ]
    ]
};

Sign field

const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signFieldAsync(pdf, parameters);

Verify signatures

const pdf = await fse.readFile(...);
const checks = await pdfSigner.verifySignaturesAsync(pdf);

IMPORTANT!: This function checks only the integrity of signatures (if the document has been changed after it has been signed).

Visual signer usage

Preparation

Instantiate visual signer:

const pdfSigner = new PdfVisualSigner();

Visual signature parameters preparation:

const parameters: SignVisualParameters = {
    pageNumber: 1,
    rectangle: {
        left: 50,
        top: 641,
        right: 264,
        bottom: 711
    },

    background: await fse.readFile(...),
    texts: [{
        lines: [
            'JOHN',
            'DOE'
        ]}, {
        lines: [
            'Digitally signed by',
            'JOHN DOE',
            'Date: 2023.11.03',
            '20:28:46 +02\'00\''
        ]}
    ]
};

IMPORTANT: if coordinates are negative, they are considered from right or bottom.

If the signature is placed relatively to the bottom pf the page use reverseY to fix the problem:

const parameters: SignVisualParameters = {
    ...
    reverseY: true,
    ...
};

Visual sign PDF

const pdf = await fse.readFile(...);
const signedPdf = await pdfSigner.signAsync(pdf, parameters);