-
-
Save Twometer/d6611dee04132251531fbc2962190388 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"crypto" | |
"crypto/x509" | |
pdfRead "github.com/digitorus/pdf" | |
pdfSign "github.com/digitorus/pdfsign/sign" | |
"github.com/signintech/gopdf" | |
"os" | |
"time" | |
) | |
func createPdfWithLink() []byte { | |
pdfEngine := &gopdf.GoPdf{} | |
pdfEngine.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) | |
pdfEngine.AddTTFFont("default", "./regular.ttf") | |
pdfEngine.SetFont("default", "", 16) | |
pdfEngine.AddPage() | |
pdfEngine.Text("Hello, world!") | |
pdfEngine.AddExternalLink("https://google.com", 0, 0, 32, 32) | |
return pdfEngine.GetBytesPdf() | |
} | |
func createPdfWithoutLink() []byte { | |
pdfEngine := &gopdf.GoPdf{} | |
pdfEngine.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) | |
pdfEngine.AddTTFFont("default", "./regular.ttf") | |
pdfEngine.SetFont("default", "", 16) | |
pdfEngine.AddPage() | |
pdfEngine.Text("Hello, world!") | |
return pdfEngine.GetBytesPdf() | |
} | |
func signPdf(pdf []byte, certs CertificateStore) []byte { | |
pdfLen := int64(len(pdf)) | |
pdfReader, err := pdfRead.NewReader(bytes.NewReader(pdf), pdfLen) | |
if err != nil { | |
panic(err) | |
} | |
pdfWriter := bytes.NewBuffer(make([]byte, 0, pdfLen)) | |
err = pdfSign.Sign(bytes.NewReader(pdf), pdfWriter, pdfReader, pdfLen, pdfSign.SignData{ | |
Signature: pdfSign.SignDataSignature{ | |
CertType: pdfSign.CertificationSignature, | |
DocMDPPerm: pdfSign.AllowFillingExistingFormFieldsAndSignaturesAndCRUDAnnotationsPerms, | |
Info: pdfSign.SignDataSignatureInfo{ | |
Name: "Test Signing", | |
Reason: "Demonstrating signing failures", | |
Location: "The Internet", | |
ContactInfo: "Twometer", | |
Date: time.Now(), | |
}, | |
}, | |
DigestAlgorithm: crypto.SHA384, | |
Signer: certs.key, | |
Certificate: certs.cert, | |
CertificateChains: [][]*x509.Certificate{certs.chain}, | |
}) | |
if err != nil { | |
panic(err) | |
} | |
return pdfWriter.Bytes() | |
} | |
func main() { | |
// This is just some internal code for loading PFX files | |
certs, err := CertificateStoreFromFile("./test_certs.pfx", "test") | |
if err != nil { | |
panic(err) | |
} | |
workingPdf := signPdf(createPdfWithoutLink(), certs) | |
failingPdf := signPdf(createPdfWithLink(), certs) | |
os.WriteFile("./working.pdf", workingPdf, 0744) | |
os.WriteFile("./failing.pdf", failingPdf, 0744) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment