Giter Club home page Giter Club logo

Comments (2)

ngvhob avatar ngvhob commented on June 13, 2024

UPDATE :
On adding the visual tag implementation I tried to solve the issue but it still remains same no green tick.

const multer = require("multer");
const fs = require("fs");
const path = require("path");
const signpdf = require("@signpdf/signpdf").default;
const { P12Signer } = require("@signpdf/signer-p12");
const { pdflibAddPlaceholder } = require("@signpdf/placeholder-pdf-lib");
const { PDFDocument, rgb, StandardFonts } = require("pdf-lib");
// const { SUBFILTER_ETSI_CADES_DETACHED } = require("@signpdf/utils");
const storage = multer.diskStorage({
  destination: "uploads/",
  filename: function (req, file, cb) {
    cb(
      null,
      file.fieldname + "-" + Date.now() + path.extname(file.originalname)
    );
  },
});

function topLeftToBottomLeft(coords, page) {
  const pageHeight = page.getHeight();
  return [
    coords[0],
    pageHeight - coords[1],
    coords[2], // x2
    pageHeight - coords[3],
  ];
}

async function addVisual(pdfDoc) {
  const page = pdfDoc.getPages()[0]; // Get the first page
  var margin = 30;
  var padding = 10;
  var label = "Signed with @signpdf";

  const helveticaFont = await pdfDoc.embedFont(StandardFonts.Helvetica);
  const fontSize = 10;
  const x = page.getWidth() - margin;
  const y = page.getHeight() - margin;

  page.drawText(label, {
    x: x,
    y: y,
    size: fontSize,
    font: helveticaFont,
    color: rgb(0, 0, 0),
  });

  return [
    x - padding,
    y - padding,
    x + fontSize + padding,
    y + fontSize + padding,
  ];
}

exports.upload = multer({ storage: storage }).single("file");

exports.signDocument = async (req, res, next) => {
  try {
    const pdfPath = req.file.path;
    const pfxPath = "assets/docsinger.pfx";
    const password = "Mawai@123";
    let pdfBuffer, signedPdfBuffer;
    pdfBuffer = fs.readFileSync(pdfPath);
    let pfxBuffer = fs.readFileSync(pfxPath);
    let pdfDoc = await PDFDocument.load(pdfBuffer);
    const page = pdfDoc.getPages()[0];
    const widgetRect = await addVisual(pdfDoc);
    pdflibAddPlaceholder({
      pdfDoc,
      pdfBuffer,
      reason: "SwiftDocuSign",
      contactInfo: "[email protected]",
      name: "Jeba Prince",
      location: "SwiftDocuSign",
      date: new Date(),
      signatureLength: 15000,
      widgetRect: topLeftToBottomLeft(widgetRect, page),
    });
    let pdfBufferWithPlaceholder = Buffer.from(await pdfDoc.save());
    const p12Signer = new P12Signer(pfxBuffer, {
      passphrase: password,
    });

    try {
      signedPdfBuffer = await signpdf.sign(pdfBufferWithPlaceholder, p12Signer);
      console.log("Signing done!");
    } catch (error) {
      throw new Error(error);
    }
    try {
      fs.writeFileSync(pdfPath, signedPdfBuffer);
    } catch (error) {
      throw new Error("Failed to write signed PDF to file");
    }
    // Send response
    res.status(200).send({
      success: true,
      message: "PDF signed successfully",
    });
  } catch (error) {
    // Handle errors
    console.error("Error signing PDF:", error);
    res.status(500).send({
      success: false,
      error: error.message,
    });
  }
};

from node-signpdf.

vbuch avatar vbuch commented on June 13, 2024

https://stackoverflow.com/a/40391641/2528232

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.