pdfsign icon indicating copy to clipboard operation
pdfsign copied to clipboard

Visual signature support

Open zosocanuck opened this issue 2 years ago • 10 comments

I am testing this library/utility and noticed support for visual signatures, however when attempting to sign a sample PDF, the visual signature doesn't appear at least in the latest version of Adobe. Has this been tested?

zosocanuck avatar Nov 16 '23 22:11 zosocanuck

There is a test case for visual signatures:

sign/pdfvisualsignature_test.go

I don't have tested myself this recently. Can you do a test based on the unit test (including) the file?

vanbroup avatar Nov 17 '23 18:11 vanbroup

It doesn't show because the Rect is 0 0 0 0.

However even if I change the Rect I see nothing, I think because there is no reference to any other visual object...?

(when I change the rect, Acrobat says it's "visible signature" but then shows nothing)

karelbilek avatar Nov 23 '23 10:11 karelbilek

Ahhh that's because it's AcroForm stuff, so I would also need to set appearance and /AP object... ugh no. AcroForms = hell. :D

karelbilek avatar Nov 23 '23 10:11 karelbilek

If you have a working example it would be great to add this to the documentation!

vanbroup avatar Nov 23 '23 13:11 vanbroup

I don't think it can be done just with this library; if I look at other PDFs with visual signatures (made by Acrobat I think), it has AP appearence streams, and I am not sure this library can do it.

Maybe if it is somehow used with conjunction with pdfcpu https://github.com/pdfcpu/pdfcpu ... but that gets me thinking it would be maybe better to add the signing functionality into pdfcpu

karelbilek avatar Nov 23 '23 13:11 karelbilek

@karelbilek I’ve just released a new version with some improvements and new signature types. Unfortunately, it doesn’t include support for visible signatures yet. However, I believe we can add basic support for features like an image or handwritten name in the future. Before we can do this, I need to refactor some code to make it easier to add new objects to the document. Please understand that this is not a high priority and it might take some time before I can work on it.

vanbroup avatar Nov 14 '24 13:11 vanbroup

@karelbilek I have just pushed an update with initial support for visual signature appearance.

	err = SignFile(inputFilePath, outputFilePath, pdf.SignData{
		Signature: pdf.SignDataSignature{
			Info: pdf.SignDataSignatureInfo{
				Name:        "John Doe",
				Location:    "Somewhere",
				Reason:      "Test with visible signature",
				ContactInfo: "None",
			},
			CertType:   pdf.ApprovalSignature,
			DocMDPPerm: pdf.AllowFillingExistingFormFieldsAndSignaturesPerms,
		},
		Appearance: pdf.Appearance{
			Visible:     true,
			LowerLeftX:  350,
			LowerLeftY:  75,
			UpperRightX: 600,
			UpperRightY: 100,
		},
		DigestAlgorithm: crypto.SHA512,
		Signer:          pkey,
		Certificate:     cert,
	})

This is an initial implementation, known limitations include:

  • The signature shown is the SignData.SignDataSignatureInfo.Name
    • How flexible does this need to be?
    • Should we include "Digitally signed by", Date/Time, Location, Reason and ContactInfo when present?
  • The signature appearance is currently text based.
    • [ ] Add support for custom (background) images
  • The font is currently hard-coded in appearance.go
    • [ ] If the default font would be a nice handwitten style font.
    • [ ] It would be great if you could load a custom font and re-use when already present
  • The signature will not be shown in documents using an xref stream
    • [ ] The updatedXrefEntries entries (page object) needs to be added to xref stream similar like currently done in the xref table

I'm also considering to change the API to something like this:

 file := pdf.New()
 file.Load(reader)
 file.SetVisualSignature(rectangle, bytes)
 file.Sign(signer, certificate, options)
 file.TimeStamp(url, options)

vanbroup avatar Dec 16 '24 14:12 vanbroup

OH WOW thanks!

karelbilek avatar Dec 16 '24 14:12 karelbilek

Why is my existing content in the PDF being removed when I use the following code for signature Appearance?

Appearance: pdf.Appearance{ Visible: true, LowerLeftX: 350, LowerLeftY: 75, UpperRightX: 600, UpperRightY: 100, }

Harshpawar18 avatar Jan 28 '25 11:01 Harshpawar18

Why is my existing content in the PDF being removed when I use the following code for signature Appearance?

Appearance: pdf.Appearance{ Visible: true, LowerLeftX: 350, LowerLeftY: 75, UpperRightX: 600, UpperRightY: 100, }

As this is an incremental update nothing is removed, only new elements are added.

Please always check the source file (or try a different one) as after a signature is applied documents will no longer be automatically repaired.

vanbroup avatar Jan 29 '25 06:01 vanbroup