Giter Club home page Giter Club logo

Comments (1)

dhensby avatar dhensby commented on June 3, 2024

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: '[email protected]',
        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');
          });
      });
    });
  },

from node-signpdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.