Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.76k stars 647 forks source link

How to Create Byte-Range in 1.x.x #203

Closed HediMateur closed 4 years ago

HediMateur commented 4 years ago

Hello,

I am trying to add a placeholder into a pdf for digital signature. I am using node-signPdf to sign the file and this is my code.

const signatureDict = pdfDoc.context.obj({
    Type: 'Sig',
    Filter: 'Adobe.PPKLite',
    SubFilter: 'adbe.pkcs7.detached',
    ByteRange: [0, '**********', '**********','**********'],
    Contents: Buffer.from(String.fromCharCode(0).repeat(1500)),
    Reason: 'We need your signature for reasons...',
    M: new Date()
  });
const pages = pdfDoc.getPages();
const signatureDictRef = pdfDoc.context.register(signatureDict);
const widgetDict = pdfDoc.context.obj(
    {
      Type: 'Annot',
      Subtype: 'Widget',
      FT: 'Sig',
      Rect: [0, 0, 0, 0],
      V: signatureDictRef,
      T: 'Signature1',
      F: 4,
      P: pdfDoc.catalog.Pages().Kids().get(0),
    }
  );
const widgetDictRef = pdfDoc.context.register(widgetDict);

pages[0].node.set(PDFName.of('Annots'), pdfDoc.context.obj([widgetDictRef]));
const formDict = pdfDoc.context.obj(
    {
      SigFlags: 3,
      Fields: pdfDoc.context.obj([widgetDictRef]),
    }
  );
pages[0].doc.catalog.set(PDFName.of('AcroForm'), formDict);
const modifiedPdfBytes = await pdfDoc.save({
    useObjectStreams: false,
  });
pdfBuffer = Buffer.from(modifiedPdfBytes)
pdfbyte = await signPDF.sign(pdfBuffer, p12Buffer, {passphrase: 'password'})

But I keep getting Could not find ByteRange placeholder: /ByteRange [0 /********** /********** /**********]. Can someone help me please. Thank you.

Hopding commented 4 years ago

Hello @HediMateur! I've provided an updated example for pdf-lib@1.3.0 here: https://github.com/Hopding/pdf-lib/issues/112#issuecomment-569085380.

I hope this helps. Please let me know if you have any additional questions!