The PDFSigner package allows adding digital signatures and image stamps to PDF files. It uses libraries like pdfcpu and pdfsign to manipulate and sign PDFs.
- Add image stamps to PDFs.
- Normalize PDFs to ensure compatibility.
- Digitally sign PDFs using PFX certificates.
Make sure you have Go installed on your machine. To install the package and its dependencies, run:
go get github.com/digitorus/pdfsign
go get github.com/pdfcpu/pdfcpu
go get software.sslmate.com/src/go-pkcs12Clone the repository or copy the code into your project.
Below is an example of how to use the package to sign a PDF:
package main
import (
"log"
"os"
"github.com/alexsetta/pdfsigner"
)
func main() {
config := pdfsigner.Config{
PFXPath: "./files/certificate.pfx",
Password: os.Getenv("PFX_PASSWORD"), // Use environment variable for the password
ImgPath: "./files/stamp.png",
Debug: true,
Name: "Your Name",
Location: "Your Location",
Reason: "Reason for Signing",
ContactInfo: "youremail@example.com",
}
err := pdfsigner.Sign("./files/input.pdf", "./files/signed_output.pdf", config)
if err != nil {
log.Fatalf("Error signing PDF: %v", err)
}
log.Println("PDF signed successfully!")
}The Config structure is used to configure the signing process:
PFXPath: Path to the PFX file containing the digital certificate.Password: Password for the PFX file (it is recommended to use an environment variable, such asPFX_PASSWORD).ImgPath: Path to the stamp image (optional).Debug: Flag to enable debug logs.Name: Signer's name.Location: Signing location.Reason: Reason for signing.ContactInfo: Signer's contact information.
Digitally signs a PDF. Parameters:
inPdf: Path to the input PDF file.outPdf: Path to the output PDF file.conf: Configuration of typeConfig.
Adds an image stamp to the PDF. Parameters:
inPdf: Path to the input PDF file.outPdf: Path to the output PDF file.imgPath: Path to the stamp image.
Normalizes the PDF to ensure compatibility. Parameters:
inFile: Path to the input PDF file.outFile: Path to the output PDF file.
The package includes a test file PDFSigner_test.go to validate the signing functionality. Make sure to configure the input and output files correctly in the ./files/ directory.
To run the tests, use the command:
go test ./...- Go 1.18 or higher.
- Digital certificate in PFX format.
- Image for the stamp.
- Environment variable
PFX_PASSWORDconfigured with the certificate password.
./files/: Directory to store input files, output files, certificates, and images.
This project is licensed under the terms of the MIT license.