vbuch / node-signpdf

Simple signing of PDFs in node.
MIT License
676 stars 174 forks source link

PDF expected as Buffer. #244

Closed htt828 closed 3 months ago

htt828 commented 3 months ago

this is my code:

async signPdf3(uploadPath, fileName) {
    const PDFDocument = require('pdf-lib').PDFDocument;
    const pdflibAddPlaceholder = require('@signpdf/placeholder-pdf-lib').pdflibAddPlaceholder;
    const signpdf = require('@signpdf/signpdf').default;
    const P12Signer = require('@signpdf/signer-p12').P12Signer;
    // contributing.pdf is the file that is going to be signed
    const sourcePath = path.join(uploadPath, fileName);
    const pdfBuffer = fs.readFileSync(sourcePath);

    // certificate.p12 is the certificate that is going to be used to sign
    const certificatePath = path.join(__dirname, './allInOne2.p12');
    const certificateBuffer = fs.readFileSync(certificatePath);
    const signer = new P12Signer(certificateBuffer);

    // Load the document into PDF-LIB
    PDFDocument.load(pdfBuffer).then(function(pdfDoc) {
      // Add a placeholder for a signature.
      pdflibAddPlaceholder({
        pdfDoc,
        reason: 'The user is declaring consent through JavaScript.',
        contactInfo: 'signpdf@example.com',
        name: 'John Doe',
        location: 'Free Text Str., Free World',
      });

      // Get the modified PDFDocument bytes
      pdfDoc.save().then(function(pdfWithPlaceholderBytes) {
        // And finally sign the document.
        signpdf
          .sign(pdfWithPlaceholderBytes, signer)
          .then(function(signedPdf) {
            // signedPdf is a Buffer of an electronically signed PDF. Store it.
            const targetPath = path.join(uploadPath, fileName);
            fs.writeFileSync(targetPath, signedPdf);
            console.log('删除陈工3243434');
          });
      });
    });
  },

but error:

PDF expected as Buffer.
    at SignPdf.sign (/Volumes/D盘/workspace/jcqlc/node_modules/@signpdf/signpdf/dist/signpdf.js:28:13)
dhensby commented 3 months ago

I think the check of the P12Signer is a bit too strict, but the issue is that the pdfDoc.save() method is returning a UInt8Array instead of a Buffer. You can fix this like so:

async signPdf3(uploadPath, fileName) {
    const PDFDocument = require('pdf-lib').PDFDocument;
    const pdflibAddPlaceholder = require('@signpdf/placeholder-pdf-lib').pdflibAddPlaceholder;
    const signpdf = require('@signpdf/signpdf').default;
    const P12Signer = require('@signpdf/signer-p12').P12Signer;
    // contributing.pdf is the file that is going to be signed
    const sourcePath = path.join(uploadPath, fileName);
    const pdfBuffer = fs.readFileSync(sourcePath);

    // certificate.p12 is the certificate that is going to be used to sign
    const certificatePath = path.join(__dirname, './allInOne2.p12');
    const certificateBuffer = fs.readFileSync(certificatePath);
    const signer = new P12Signer(certificateBuffer);

    // Load the document into PDF-LIB
    PDFDocument.load(pdfBuffer).then(function(pdfDoc) {
      // Add a placeholder for a signature.
      pdflibAddPlaceholder({
        pdfDoc,
        reason: 'The user is declaring consent through JavaScript.',
        contactInfo: 'signpdf@example.com',
        name: 'John Doe',
        location: 'Free Text Str., Free World',
      });

      // Get the modified PDFDocument bytes
      pdfDoc.save().then(function(pdfWithPlaceholderBytes) {
        // And finally sign the document.
        signpdf
-          .sign(pdfWithPlaceholderBytes, signer)
+          .sign(Buffer.from(pdfWithPlaceholderBytes), signer)
          .then(function(signedPdf) {
            // signedPdf is a Buffer of an electronically signed PDF. Store it.
            const targetPath = path.join(uploadPath, fileName);
            fs.writeFileSync(targetPath, signedPdf);
            console.log('删除陈工3243434');
          });
      });
    });
  },