Skip to content

Commit 47ee72d

Browse files
authored
[US-1207] PDF/A-4 validation and optimization examples (#293)
* PDF/A-4 examples. * Updated unipdf dependency.
1 parent 6aa396d commit 47ee72d

11 files changed

+167
-18
lines changed

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@ require (
1313
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
1414
github.com/unidoc/pkcs7 v0.3.0
1515
github.com/unidoc/unichart v0.5.1
16-
github.com/unidoc/unipdf/v4 v4.4.0
16+
github.com/unidoc/unipdf/v4 v4.5.0
1717
golang.org/x/crypto v0.41.0
1818
golang.org/x/image v0.30.0
1919
golang.org/x/text v0.28.0
2020
google.golang.org/api v0.191.0
21-
google.golang.org/genproto v0.0.0-20240730163845-b1a4ccb954bf
2221
google.golang.org/protobuf v1.34.2
2322
gopkg.in/gographics/imagick.v2 v2.7.0
2423
software.sslmate.com/src/go-pkcs12 v0.6.0
@@ -38,6 +37,7 @@ require (
3837
github.com/felixge/httpsnoop v1.0.4 // indirect
3938
github.com/go-logr/logr v1.4.2 // indirect
4039
github.com/go-logr/stdr v1.2.2 // indirect
40+
github.com/go-text/typesetting v0.3.0 // indirect
4141
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
4242
github.com/google/s2a-go v0.1.8 // indirect
4343
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
@@ -49,7 +49,7 @@ require (
4949
github.com/pkg/errors v0.9.1 // indirect
5050
github.com/pmezard/go-difflib v1.0.0 // indirect
5151
github.com/sirupsen/logrus v1.9.3 // indirect
52-
github.com/stretchr/testify v1.10.0 // indirect
52+
github.com/stretchr/testify v1.11.1 // indirect
5353
github.com/thales-e-security/pool v0.0.2 // indirect
5454
github.com/unidoc/freetype v0.2.3 // indirect
5555
github.com/unidoc/garabic v0.0.0-20220702200334-8c7cb25baa11 // indirect
@@ -67,6 +67,7 @@ require (
6767
golang.org/x/sys v0.35.0 // indirect
6868
golang.org/x/time v0.6.0 // indirect
6969
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
70+
google.golang.org/genproto v0.0.0-20240730163845-b1a4ccb954bf // indirect
7071
google.golang.org/genproto/googleapis/api v0.0.0-20240725223205-93522f1f2a9f // indirect
7172
google.golang.org/genproto/googleapis/rpc v0.0.0-20240730163845-b1a4ccb954bf // indirect
7273
google.golang.org/grpc v1.64.1 // indirect

go.sum

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
4949
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
5050
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
5151
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
52+
github.com/go-text/typesetting v0.3.0 h1:OWCgYpp8njoxSRpwrdd1bQOxdjOXDj9Rqart9ML4iF4=
53+
github.com/go-text/typesetting v0.3.0/go.mod h1:qjZLkhRgOEYMhU9eHBr3AR4sfnGJvOXNLt8yRAySFuY=
54+
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066 h1:qCuYC+94v2xrb1PoS4NIDe7DGYtLnU2wWiQe9a1B1c0=
55+
github.com/go-text/typesetting-utils v0.0.0-20241103174707-87a29e9e6066/go.mod h1:DDxDdQEnB70R8owOx3LVpEFvpMK9eeH1o2r0yZhFI9o=
5256
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
5357
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
5458
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
@@ -115,8 +119,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
115119
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
116120
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
117121
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
118-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
119-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
122+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
123+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
120124
github.com/thales-e-security/pool v0.0.2 h1:RAPs4q2EbWsTit6tpzuvTFlgFRJ3S8Evf5gtvVDbmPg=
121125
github.com/thales-e-security/pool v0.0.2/go.mod h1:qtpMm2+thHtqhLzTwgDBj/OuNnMpupY8mv0Phz0gjhU=
122126
github.com/trimmer-io/go-xmp v1.0.0 h1:zY8bolSga5kOjBAaHS6hrdxLgEoYuT875xTy0QDwZWs=
@@ -134,10 +138,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6
134138
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
135139
github.com/unidoc/unichart v0.5.1 h1:qnYavwBV5sg9NUF59KbMOqJdh2kA454nVxdDTPPtSz8=
136140
github.com/unidoc/unichart v0.5.1/go.mod h1:/8yJsL49OqBOyG53JFVZOwwDXDquo/ZRMkfz9fNsVgc=
137-
github.com/unidoc/unipdf/v4 v4.3.0 h1:eA4zjRHTULtV5thy3MausfFYDP1i59qGdsfxe709oUY=
138-
github.com/unidoc/unipdf/v4 v4.3.0/go.mod h1:oR0EX7TmS7KaAuzFQPA9t9HjbU4f2NbWMvzXNqtXo70=
139-
github.com/unidoc/unipdf/v4 v4.4.0 h1:JvfRBjQgaT1FkdUiZXmrGLp5RFhk9GB/NfHEzsiU4i0=
140-
github.com/unidoc/unipdf/v4 v4.4.0/go.mod h1:oR0EX7TmS7KaAuzFQPA9t9HjbU4f2NbWMvzXNqtXo70=
141+
github.com/unidoc/unipdf/v4 v4.5.0 h1:8dM19G/d/ElKbjnhQfYnT8cgaD1FV8nK/VhR+CdJl+U=
142+
github.com/unidoc/unipdf/v4 v4.5.0/go.mod h1:fAmjZMazN2eq83dVNc8BEsH+RQoBylbdWmpXiL/qrPo=
141143
github.com/unidoc/unitype v0.5.1 h1:UwTX15K6bktwKocWVvLoijIeu4JAVEAIeFqMOjvxqQs=
142144
github.com/unidoc/unitype v0.5.1/go.mod h1:3dxbRL+f1otNqFQIRHho8fxdg3CcUKrqS8w1SXTsqcI=
143145
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=

pdfa/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# PDF/A validation and optimization examples
2+
3+
The examples explain how to validate PDF file for compatibility with PDF/A standards and to optimize PDF file to make it compatible with PDF/A standards.
4+
## Examples
5+
6+
- [pdf_apply_standard.go](pdf_apply_standard.go) The example showcases PDF file optimization according to PDF/A-1 standard.
7+
- [pdf_validate_standard.go](pdf_validate_standard.go) The example showcases PDF file validation according to PDF/A-1 standard.
8+
- [pdfa2_apply_standard.go](pdfa2_apply_standard.go) The example showcases PDF file optimization according to PDF/A-2 standard.
9+
- [pdfa2_validate_standard.go](pdfa2_validate_standard.go) The example showcases PDF file validation according to PDF/A-2 standard.
10+
- [pdfa3_apply_standard.go](pdfa3_apply_standard.go) The example showcases PDF file optimization according to PDF/A-3 standard.
11+
- [pdfa3_validate_standard.go](pdfa3_validate_standard.go) The example showcases PDF file validation according to PDF/A-3 standard.
12+
- [pdfa4_apply_standard.go](pdfa4_apply_standard.go) The example showcases PDF file optimization according to PDF/A-4 standard.
13+
- [pdfa4_validate_standard.go](pdfa4_validate_standard.go) The example showcases PDF file validation according to PDF/A-4 standard.

pdfa/pdf_apply_standard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF optimization (compression) example.
2+
* PDF optimization (PDF/A standard applying) example.
33
*
44
* Run as: go run pdf_apply_standard.go <input.pdf> <output.pdf>
55
*/

pdfa/pdf_validate_standard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF optimization (compression) example.
2+
* PDF/A-1 validation example.
33
*
44
* Run as: go run pdfa_validate_standard.go <input.pdf>
55
*/
@@ -49,7 +49,7 @@ func main() {
4949
log.Fatalf("Fail: %v\n", err)
5050
}
5151

52-
// Apply standard PDF/A-1B.
52+
// Validate standard PDF/A-1.
5353
standards := []model.StandardImplementer{
5454
pdfa.NewProfile1A(nil),
5555
pdfa.NewProfile1B(nil),

pdfa/pdfa2_apply_standard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF/A-2 optimization (compression) example.
2+
* PDF/A-2 optimization (standard applying) example.
33
*
44
* Run as: go run pdfa2_apply_standard.go <input.pdf> <output.pdf>
55
*/

pdfa/pdfa2_validate_standard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF/A-2 optimization (compression) example.
2+
* PDF/A-2 validation example.
33
*
44
* Run as: go run pdfa2_validate_standard.go <input.pdf>
55
*/
@@ -49,7 +49,7 @@ func main() {
4949
log.Fatalf("Fail: %v\n", err)
5050
}
5151

52-
// Apply standard PDF/A-2.
52+
// Validate standard PDF/A-2.
5353
standards := []model.StandardImplementer{
5454
pdfa.NewProfile2A(nil),
5555
pdfa.NewProfile2B(nil),

pdfa/pdfa3_apply_standard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF/A-3 optimization (compression) example.
2+
* PDF/A-3 optimization (standard applying) example.
33
*
44
* Run as: go run pdfa3_apply_standard.go <input.pdf> <output.pdf>
55
*/

pdfa/pdfa3_validate_standard.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* PDF/A-3 optimization (compression) example.
2+
* PDF/A-3 validation example.
33
*
44
* Run as: go run pdfa3_validate_standard.go <input.pdf>
55
*/
@@ -49,7 +49,7 @@ func main() {
4949
log.Fatalf("Fail: %v\n", err)
5050
}
5151

52-
// Apply standard PDF/A-3.
52+
// Validate standard PDF/A-3.
5353
standards := []model.StandardImplementer{
5454
pdfa.NewProfile3A(nil),
5555
pdfa.NewProfile3B(nil),

pdfa/pdfa4_apply_standard.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* PDF/A-4 optimization (standard applying) example.
3+
*
4+
* Run as: go run pdfa4_apply_standard.go <input.pdf> <output.pdf>
5+
*/
6+
7+
package main
8+
9+
import (
10+
"fmt"
11+
"log"
12+
"os"
13+
"time"
14+
15+
"github.com/unidoc/unipdf/v4/common/license"
16+
"github.com/unidoc/unipdf/v4/model"
17+
"github.com/unidoc/unipdf/v4/model/pdfa"
18+
)
19+
20+
func init() {
21+
// Make sure to load your metered License API key prior to using the library.
22+
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
23+
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
24+
if err != nil {
25+
panic(err)
26+
}
27+
}
28+
29+
func main() {
30+
args := os.Args
31+
if len(args) < 3 {
32+
fmt.Printf("Usage: %s INPUT_PDF_PATH OUTPUT_PDF_PATH", os.Args[0])
33+
return
34+
}
35+
inputPath := args[1]
36+
outputPath := args[2]
37+
38+
// Initialize starting time.
39+
start := time.Now()
40+
41+
// Create reader.
42+
reader, file, err := model.NewPdfReaderFromFile(inputPath, nil)
43+
if err != nil {
44+
log.Fatalf("Fail: %v\n", err)
45+
}
46+
defer file.Close()
47+
48+
// Generate a PDFWriter from PDFReader.
49+
pdfWriter, err := reader.ToWriter(nil)
50+
if err != nil {
51+
log.Fatalf("Fail: %v\n", err)
52+
}
53+
54+
// Apply standard PDF/A-4.
55+
pdfWriter.ApplyStandard(pdfa.NewProfile4(nil))
56+
57+
// Create output file.
58+
err = pdfWriter.WriteToFile(outputPath)
59+
if err != nil {
60+
log.Fatalf("Fail: %v\n", err)
61+
}
62+
63+
duration := float64(time.Since(start)) / float64(time.Millisecond)
64+
fmt.Printf("Processing time: %.2f ms\n", duration)
65+
}

0 commit comments

Comments
 (0)