node-signpdf icon indicating copy to clipboard operation
node-signpdf copied to clipboard

Simple Example Sign PDF

Open hscstudio opened this issue 2 years ago • 9 comments

I don't know, why You not create simple example for this library? Not all users know how use this library..

  1. How to import Your library? especially plainAddPlaceholder
  2. How create p12 certificate?
  3. How write signed pdf to file?
  4. How change "Signed by signedpdf [email protected]" in pdf file?

I read this README, but You only reference code tester I have look at issue but You not clear answer, for example (https://github.com/vbuch/node-signpdf/issues/126), but You not answer with clear answer :( You may need to add add 'dist' to the import path. something like 'node-sginpdf/dist/helpers/plainAddPlaceholder' why You dont answer with const { plainAddPlaceholder } = require('node-signpdf/dist/helpers'); I have googling but I don't get anything that help me

For other, if You confuse, You can read this simple example

const signer = require('node-signpdf').default
const fs = require('fs')
const { plainAddPlaceholder } = require('node-signpdf/dist/helpers');

const pdfSignedPath = `./signed.pdf`;
const pdfBuffer = fs.readFileSync(`./source.pdf`);
const certBuffer = fs.readFileSync(`./certificate.p12`);

let inputBuffer = plainAddPlaceholder({
    pdfBuffer,
    reason: 'Signed Certificate.',
    contactInfo: '[email protected]',
    name: 'Example',
    location: 'Jakarta',
    signatureLength: certBuffer.length,
});

const signedPdf = signer.sign(
  inputBuffer,
  certBuffer,
  { asn1StrictParsing : true },
);

fs.writeFileSync(pdfSignedPath, signedPdf);

source.pdf is file pdf that will signed signed.pdf is target of signed file certificate.p12 is Your certificate, generate this with openssl or You can use sample in folder 'resources' of this library

hscstudio avatar Mar 10 '22 12:03 hscstudio

This was actually extremely helpful for me, as I don't come from a node-js background and the unit tests were very confusing for me to understand as I couldn't figure out how to properly import the library and functions and run them in my own code.

Thank you! (both to the developer of the library and @hscstudio for the clean (and working!) example)

JMHAVANCE avatar Mar 11 '22 17:03 JMHAVANCE

Maybe this snippet code will help you get better understanding to signing PDF with this library

import { SignPdf } from 'node-signpdf';
import fs from 'fs';
import { Buffer } from 'buffer';
import { plainAddPlaceholder } from 'node-signpdf/dist/helpers/index.js';

const pathSignedPdf = 'path/to/your/signed.pdf'
const p12Buffer = fs.readFileSync('path/to/your/certificate.p12');
const pdfBuffer = fs.readFileSync('path/to/your/file/need/to/sign.pdf');
const pdfBufferToSign = plainAddPlaceholder({
    pdfBuffer
});
const signer = new SignPdf()
const signedPdf = signer.sign(pdfBufferToSign, p12Buffer, { passphrase: 'your/passphrase' });
const bufferPdf = Buffer.from(signedPdf)

fs.createWriteStream(pathSignedPdf).write(bufferPdf);

fdimarh avatar Mar 12 '22 17:03 fdimarh

How to use this module in code. Can anyone help me?

AkshayThakurcr avatar Mar 16 '22 11:03 AkshayThakurcr

¡Hello! I'm getting the following error while trying to run @hscstudio and @fdimarh example:

throw new _SignPdfError.default(`Expected xref at ${position} but found other content.`, _SignPdfError.default.TYPE_PARSE);
            ^

SignPdfError: Expected xref at NaN but found other content.
    at getXref`

¿What am I missing?

Screen Shot 2022-04-19 at 19 47 36

I tried to run it locally, and I got the same error message. Here is the Replit Link where you can reproduce the issue. Thank you for the help!

christianledgard avatar Apr 20 '22 00:04 christianledgard

Without reproducing, just looking at the code: the reason is that getLastTrailerPosition returned NaN. That's what the position variable is - the result of getLastTrailerPosition(). Why that happened requires a resource that I don't have - time. So I cannot, at least at the moment, help you any further in debugging.

Edit: I can just guess that you are using PDF features that are not supported. What is the version of you PDF? Does it include streams? That's the usual when there are issues with the trailer.

vbuch avatar Apr 20 '22 07:04 vbuch

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jul 31 '22 15:07 stale[bot]

Hi @vbuch, first of all, thanks for the wonderful library.

@christianledgard I'm getting the same error, have you managed to find the solution?

(node:13193) UnhandledPromiseRejectionWarning: Error: Expected xref at NaN but found other content.
    at getXref (/home/jack/Desktop/temp/nodejs-sign-test/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:36:13)
    at getFullXrefTable (/home/jack/Desktop/temp/nodejs-sign-test/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:88:25)
    at readRefTable (/home/jack/Desktop/temp/nodejs-sign-test/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readRefTable.js:108:25)
    at readPdf (/home/jack/Desktop/temp/nodejs-sign-test/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/readPdf.js:47:46)
    at plainAddPlaceholder (/home/jack/Desktop/temp/nodejs-sign-test/node_modules/node-signpdf/dist/helpers/plainAddPlaceholder/index.js:58:37)
....

The node.js snippet

(async()=>{
    //https://github.com/vbuch/node-signpdf/blob/develop/src/signpdf.test.js#L111
    let signer = new SignPdf();

    const p12Buffer = fs.readFileSync(
        `./certificate.p12`,
    );
    let pdfBuffer = fs.readFileSync(`./pdf-lib_image_embedding_example.pdf`);
    pdfBuffer = plainAddPlaceholder({
        pdfBuffer,
        reason: 'I have reviewed it.',
        signatureLength: 1612,
    });
    pdfBuffer = signer.sign(pdfBuffer, p12Buffer);
    fs.writeFileSync('signed_image.pdf', pdfBuffer);
[pdf-lib_image_embedding_example.pdf](https://github.com/vbuch/node-signpdf/files/9278681/pdf-lib_image_embedding_example.pdf)


})();

pdf-lib_image_embedding_example.pdf

The cert: https://github.com/vbuch/node-signpdf/blob/develop/resources/certificate.p12

bktan81 avatar Aug 08 '22 04:08 bktan81

Same error here "Uncaught Error: Could not find xref anywhere at or after NaN."

Please someone tell how to resolve it

artsnr1 avatar Aug 15 '22 07:08 artsnr1

Without reproducing, just looking at the code: the reason is that getLastTrailerPosition returned NaN. That's what the position variable is - the result of getLastTrailerPosition(). Why that happened requires a resource that I don't have - time. So I cannot, at least at the moment, help you any further in debugging.

Edit: I can just guess that you are using PDF features that are not supported. What is the version of you PDF? Does it include streams? That's the usual when there are issues with the trailer.

for Object streams added into pdf v 1.5 or greater, this plugin is failing to read pdf

debchy avatar Sep 21 '22 08:09 debchy

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 31 '22 23:12 stale[bot]

Found a solution using pdf-lib on Stackoverflow, the link is as follows: https://stackoverflow.com/questions/69973417/add-graphic-signature-and-digital-signature-to-a-pdf-in-nodejs

and the original answer: https://github.com/Hopding/pdf-lib/issues/112#issuecomment-1049456413

bktan81 avatar Feb 09 '23 05:02 bktan81

This issue has been automatically marked as stale because it has not had activity in the past 90 days. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 12 '23 13:08 stale[bot]

Since this issue is serving as a useful basic example, here's a type declarations file that I'm using to add TypeScript support for this package:

declare module 'node-signpdf';

interface PlainAddPlaceholderOptions {
  pdfBuffer: Buffer;
  reason: string;
  contactInfo?: string;
  name?: string;
  location?: string,
  signatureLength?: number;
  subFilter?: string;
}

function plainAddPlaceholder(options: PlainAddPlaceholderOptions): Buffer;

interface SignOptions {
  asn1StrictParsing?: boolean;
  passphrase?: string;
}

class SignPdf {
  sign(pdfBuffer: Buffer, p12CertBuffer: Buffer, options?: SignOptions): Buffer;
}

20manas avatar Aug 22 '23 11:08 20manas

@20manas 2.0.0 already has some (limited) TS types generated through JSDoc. Much more will be availale in 3+

vbuch avatar Oct 04 '23 15:10 vbuch

@hscstudio I will include your example in 3+

vbuch avatar Oct 04 '23 15:10 vbuch

https://github.com/vbuch/node-signpdf/releases/tag/v3.0.0

vbuch avatar Oct 09 '23 09:10 vbuch