Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (83 loc) · 2.4 KB
/
Copy pathmain.go
File metadata and controls
106 lines (83 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"codeberg.org/go-pdf/fpdf"
)
const (
font = "Helvetica"
xMargin = 20.0
caCertURL = "https://dvv.fineid.fi/en/certificate-authority"
validationURL = "https://dvv.fineid.fi/en/validation"
)
func sha256String(data []byte) string {
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}
func generatePDFWithAttachment(attachmentPath string, outputPath string) error {
attachmentData, err := os.ReadFile(attachmentPath)
if err != nil {
return fmt.Errorf("failed to read attachment: %w", err)
}
attachmentName := filepath.Base(attachmentPath)
hashStr := sha256String(attachmentData)
pdf := fpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont(font, "", 12)
pdf.SetXY(xMargin, 50)
content := fmt.Sprintf(`This is a PDF wrapper for signing the attached file.
Attachment name:
%s
Attachment SHA256 checksum:
%s`, attachmentName, hashStr)
pdf.MultiCell(0, 10, content, "", "", false)
pdf.SetXY(xMargin, pdf.GetY()+10)
pdf.SetTextColor(0, 0, 0)
pdf.Write(12, "DVV CA Certificates ")
pdf.SetTextColor(0, 0, 255)
pdf.WriteLinkString(12, caCertURL, caCertURL)
pdf.Ln(15)
pdf.SetX(xMargin)
pdf.SetTextColor(0, 0, 0)
pdf.Write(12, "Online PDF Signature Validator ")
pdf.SetTextColor(0, 0, 255)
pdf.WriteLinkString(12, validationURL, validationURL)
attach := fpdf.Attachment{
Content: attachmentData,
Filename: attachmentName,
Description: "Attachment: " + attachmentName,
}
pdf.SetAttachments([]fpdf.Attachment{attach})
pdf.AddAttachmentAnnotation(&attach, 20, 215, 10, 10)
if err := pdf.OutputFileAndClose(outputPath); err != nil {
return err
}
return nil
}
func main() {
if len(os.Args) < 2 {
log.Println("Usage: pdfgen <attachment_file> [output_pdf]")
log.Println(" attachment_file: The file to embed in the PDF")
log.Println(" output_pdf: Optional output PDF filename (default: input filename with .pdf extension)")
return
}
attachmentPath := os.Args[1]
// Determine output PDF path
outputPath := attachmentPath + ".pdf"
if len(os.Args) >= 3 {
outputPath = os.Args[2]
}
// Ensure output has .pdf extension
if !strings.HasSuffix(strings.ToLower(outputPath), ".pdf") {
outputPath += ".pdf"
}
if err := generatePDFWithAttachment(attachmentPath, outputPath); err != nil {
log.Fatal(err)
}
log.Printf("PDF created: %s", outputPath)
}