The API documentation says the returned string is in format PKCS#7 so we added the header -----BEGIN PKCS7----- and footer -----END PKCS7-----. Then we do forge.pkcs7.messageFromPem and this give us a p7 object. Then I tried to sign the PDF using some code from this library:
// Check if the PDF has a good enough placeholder to fit the signature.
const raw = forge.asn1.toDer(p7.toAsn1()).getBytes();
// placeholderLength represents the length of the HEXified symbols but we're
// checking the actual lengths.
if (raw.length * 2 > placeholderLength) {
throw new SignPdfError(
`Signature exceeds placeholder length: ${
raw.length * 2
} > ${placeholderLength}`,
SignPdfError.TYPE_INPUT
);
}
let signature = Buffer.from(raw, "binary").toString("hex");
// Pad the signature with zeroes so the it is the same length as the placeholder
signature += Buffer.from(
String.fromCharCode(0).repeat(placeholderLength / 2 - raw.length)
).toString("hex");
// Place it in the document.
pdf = Buffer.concat([
pdf.slice(0, byteRange[1]),
Buffer.from(`<${signature}>`),
pdf.slice(byteRange[1]),
]);
And it seems to be adding the signature just right on the PDF
Hello everyone.
In my company we are developing a feature that involves signing pdf's. We sent the pdf to an external API which responds with something like:
The API documentation says the returned string is in format PKCS#7 so we added the header
-----BEGIN PKCS7-----
and footer-----END PKCS7-----
. Then we doforge.pkcs7.messageFromPem
and this give us a p7 object. Then I tried to sign the PDF using some code from this library:And it seems to be adding the signature just right on the PDF
But when I open the PDF with Adobe Reader its says:
At least one signature is invalid. There are errors in the formatting or information contained in this signature.
This is my first time working with pdf signatures so any guidance is appreciated! Thanks.