|
| 1 | +/* |
| 2 | + * This example showcases how to create a digital signature for a PDF file using a custom timestamp client. |
| 3 | + * |
| 4 | + * $ ./pdf_sign_custom_client <FILE.PFX> <PASSWORD> <FILE.PEM> <INPUT_PDF_PATH> <OUTPUT_PDF_PATH> |
| 5 | + */ |
| 6 | +package main |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "crypto/x509" |
| 11 | + "encoding/pem" |
| 12 | + "fmt" |
| 13 | + "log" |
| 14 | + "net/http" |
| 15 | + "os" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/unidoc/unipdf/v4/annotator" |
| 19 | + "github.com/unidoc/unipdf/v4/common/license" |
| 20 | + "github.com/unidoc/unipdf/v4/core" |
| 21 | + "github.com/unidoc/unipdf/v4/model" |
| 22 | + "github.com/unidoc/unipdf/v4/model/sighandler" |
| 23 | + "github.com/unidoc/unipdf/v4/model/sigutil" |
| 24 | + "golang.org/x/crypto/pkcs12" |
| 25 | +) |
| 26 | + |
| 27 | +func init() { |
| 28 | + // Make sure to load your metered License API key prior to using the library. |
| 29 | + // If you need a key, you can sign up and create a free one at https://cloud.unidoc.io |
| 30 | + err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)) |
| 31 | + if err != nil { |
| 32 | + panic(err) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +const usagef = "Usage: %s PFX_FILE PASSWORD PEM_FILE INPUT_PDF_PATH OUTPUT_PDF_PATH\n" |
| 37 | + |
| 38 | +func main() { |
| 39 | + args := os.Args |
| 40 | + if len(args) < 6 { |
| 41 | + fmt.Printf(usagef, os.Args[0]) |
| 42 | + return |
| 43 | + } |
| 44 | + pfxPath := args[1] |
| 45 | + password := args[2] |
| 46 | + pemPath := args[3] |
| 47 | + inputPath := args[4] |
| 48 | + outputPath := args[5] |
| 49 | + |
| 50 | + // Get private key and X509 certificate from the PFX file. |
| 51 | + pfxData, err := os.ReadFile(pfxPath) |
| 52 | + if err != nil { |
| 53 | + log.Fatal("Fail: %v\n", err) |
| 54 | + } |
| 55 | + |
| 56 | + priv, cert, err := pkcs12.Decode(pfxData, password) |
| 57 | + if err != nil { |
| 58 | + log.Fatal("Fail: %v\n", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Get CA Certificate from the PEM file. |
| 62 | + caCertF, err := os.ReadFile(pemPath) |
| 63 | + if err != nil { |
| 64 | + log.Fatal("Fail: %v\n", err) |
| 65 | + } |
| 66 | + |
| 67 | + certDERBlock, _ := pem.Decode(caCertF) |
| 68 | + |
| 69 | + cacert, err := x509.ParseCertificate(certDERBlock.Bytes) |
| 70 | + |
| 71 | + if err != nil { |
| 72 | + log.Fatal("Fail: %v\n", err) |
| 73 | + } |
| 74 | + |
| 75 | + // Create reader. |
| 76 | + file, err := os.Open(inputPath) |
| 77 | + if err != nil { |
| 78 | + log.Fatal("Fail: %v\n", err) |
| 79 | + } |
| 80 | + defer file.Close() |
| 81 | + |
| 82 | + reader, err := model.NewPdfReader(file) |
| 83 | + if err != nil { |
| 84 | + log.Fatal("Fail: %v\n", err) |
| 85 | + } |
| 86 | + |
| 87 | + // Create appender. |
| 88 | + appender, err := model.NewPdfAppender(reader) |
| 89 | + if err != nil { |
| 90 | + log.Fatal("Fail: %v\n", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Set timestamp server. |
| 94 | + timestampServerURL := "https://freetsa.org/tsr" |
| 95 | + |
| 96 | + // Create PAdES signature handler. |
| 97 | + padEs := sighandler.NewEtsiPAdES(sighandler.LevelLT) |
| 98 | + padEs.SetCertificate(cert) |
| 99 | + padEs.SetPrivateKey(priv) |
| 100 | + padEs.SetCA(cacert) |
| 101 | + padEs.SetTimestampServerURL(timestampServerURL) |
| 102 | + padEs.SetAppender(appender) |
| 103 | + |
| 104 | + // Set a custom timestamp client with a custom HTTP client (with timeout). |
| 105 | + // This is optional. If not set, a default client will be used. |
| 106 | + // Here we set a timeout of 30 seconds for the HTTP client (the default is 5 seconds). |
| 107 | + padEs.SetTimestampClient(&sigutil.TimestampClient{ |
| 108 | + HTTPClient: &http.Client{ |
| 109 | + Timeout: 30 * time.Second, |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + var handler model.SignatureHandler |
| 114 | + handler = padEs |
| 115 | + |
| 116 | + // Create signature. |
| 117 | + signature := model.NewPdfSignature(handler) |
| 118 | + signature.SetName("PAdES B-LT Signature PDF") |
| 119 | + signature.SetReason("TestPAdESPDF") |
| 120 | + signature.SetDate(time.Now(), "") |
| 121 | + |
| 122 | + if err := signature.Initialize(); err != nil { |
| 123 | + log.Fatal("Fail: %v\n", err) |
| 124 | + } |
| 125 | + |
| 126 | + // Create signature field and appearance. |
| 127 | + opts := annotator.NewSignatureFieldOpts() |
| 128 | + opts.FontSize = 10 |
| 129 | + opts.Rect = []float64{10, 25, 75, 60} |
| 130 | + |
| 131 | + field, err := annotator.NewSignatureField( |
| 132 | + signature, |
| 133 | + []*annotator.SignatureLine{ |
| 134 | + annotator.NewSignatureLine("Name", "John Doe"), |
| 135 | + annotator.NewSignatureLine("Date", "2025.10.15"), |
| 136 | + annotator.NewSignatureLine("Reason", "PAdES signature with custom client"), |
| 137 | + }, |
| 138 | + opts, |
| 139 | + ) |
| 140 | + field.T = core.MakeString("Self signed PDF") |
| 141 | + |
| 142 | + if err = appender.Sign(1, field); err != nil { |
| 143 | + log.Fatal("Fail: %v\n", err) |
| 144 | + } |
| 145 | + |
| 146 | + // Write output to buffer. |
| 147 | + buffer := bytes.NewBuffer(nil) |
| 148 | + err = appender.Write(buffer) |
| 149 | + if err != nil { |
| 150 | + log.Fatal("Fail: %v\n", err) |
| 151 | + } |
| 152 | + |
| 153 | + // We need the second pass to correctly save DSS/VRI information. |
| 154 | + pdf2, err := model.NewPdfReader(bytes.NewReader(buffer.Bytes())) |
| 155 | + if err != nil { |
| 156 | + log.Fatal("Fail: %v\n", err) |
| 157 | + } |
| 158 | + |
| 159 | + appender2, err := model.NewPdfAppender(pdf2) |
| 160 | + if err != nil { |
| 161 | + log.Fatal("Fail: %v\n", err) |
| 162 | + } |
| 163 | + |
| 164 | + appender2.SetDSS(appender.GetDSS()) |
| 165 | + |
| 166 | + // Write output to the PDF file. |
| 167 | + err = appender2.WriteToFile(outputPath) |
| 168 | + if err != nil { |
| 169 | + log.Fatal("Fail: %v\n", err) |
| 170 | + } |
| 171 | + |
| 172 | + log.Printf("PDF file successfully signed. Output path: %s\n", outputPath) |
| 173 | +} |
0 commit comments