Skip to content

alexsetta/pdfsigner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDFSigner

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.

Features

  • Add image stamps to PDFs.
  • Normalize PDFs to ensure compatibility.
  • Digitally sign PDFs using PFX certificates.

Installation

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-pkcs12

Clone the repository or copy the code into your project.

Usage Example

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!")
}

Configuration

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 as PFX_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.

Main Methods

Sign

Digitally signs a PDF. Parameters:

  • inPdf: Path to the input PDF file.
  • outPdf: Path to the output PDF file.
  • conf: Configuration of type Config.

addImageSignature

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.

normalizePDF

Normalizes the PDF to ensure compatibility. Parameters:

  • inFile: Path to the input PDF file.
  • outFile: Path to the output PDF file.

Tests

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 ./...

Requirements

  • Go 1.18 or higher.
  • Digital certificate in PFX format.
  • Image for the stamp.
  • Environment variable PFX_PASSWORD configured with the certificate password.

Directory Structure

  • ./files/: Directory to store input files, output files, certificates, and images.

License

This project is licensed under the terms of the MIT license.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages