Skip to content

Commit

Permalink
add PDF/A-3 examples (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
3ace committed May 2, 2024
1 parent ea53d68 commit 69dfa68
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
github.com/unidoc/pkcs7 v0.2.0
github.com/unidoc/unichart v0.3.0
github.com/unidoc/unipdf/v3 v3.57.0
github.com/unidoc/unipdf/v3 v3.58.0
github.com/wcharczuk/go-chart/v2 v2.1.0
golang.org/x/crypto v0.22.0
golang.org/x/image v0.15.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a h1:RLtvUhe4DsUDl6
github.com/unidoc/timestamp v0.0.0-20200412005513-91597fd3793a/go.mod h1:j+qMWZVpZFTvDey3zxUkSgPJZEX33tDgU/QIA0IzCUw=
github.com/unidoc/unichart v0.3.0 h1:VX1j5yzhjrR3f2flC03Yat6/WF3h7Z+DLEvJLoTGhoc=
github.com/unidoc/unichart v0.3.0/go.mod h1:8JnLNKSOl8yQt1jXewNgYFHhFm5M6/ZiaydncFDpakA=
github.com/unidoc/unipdf/v3 v3.57.0 h1:C6t0MMC5MI336gqapHckRg+szYce1EomPjTlE/XHjQA=
github.com/unidoc/unipdf/v3 v3.57.0/go.mod h1:HEGsUAyg0cI46ofB2D4b6FzBXzVM2P1mHvQ5R+HxONs=
github.com/unidoc/unipdf/v3 v3.58.0 h1:c2yWEw1FLxwoVCjcuUTeOAQn/HIHsh+zq+wlVFGwgKc=
github.com/unidoc/unipdf/v3 v3.58.0/go.mod h1:HEGsUAyg0cI46ofB2D4b6FzBXzVM2P1mHvQ5R+HxONs=
github.com/unidoc/unitype v0.4.0 h1:/TMZ3wgwfWWX64mU5x2O9no9UmoBqYCB089LYYqHyQQ=
github.com/unidoc/unitype v0.4.0/go.mod h1:HV5zuUeqMKA4QgYQq3KDlJY/P96XF90BQB+6czK6LVA=
github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
Expand Down
65 changes: 65 additions & 0 deletions pdfa/pdfa3_apply_standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* PDF/A-3 optimization (compression) example.
*
* Run as: go run pdfa3_apply_standard.go <input.pdf> <output.pdf>
*/

package main

import (
"fmt"
"log"
"os"
"time"

"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/model"
"github.com/unidoc/unipdf/v3/model/pdfa"
)

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
args := os.Args
if len(args) < 3 {
fmt.Printf("Usage: %s INPUT_PDF_PATH OUTPUT_PDF_PATH", os.Args[0])
return
}
inputPath := args[1]
outputPath := args[2]

// Initialize starting time.
start := time.Now()

// Create reader.
reader, file, err := model.NewPdfReaderFromFile(inputPath, nil)
if err != nil {
log.Fatalf("Fail: %v\n", err)
}
defer file.Close()

// Generate a PDFWriter from PDFReader.
pdfWriter, err := reader.ToWriter(nil)
if err != nil {
log.Fatalf("Fail: %v\n", err)
}

// Apply standard PDF/A-3B.
pdfWriter.ApplyStandard(pdfa.NewProfile3B(nil))

// Create output file.
err = pdfWriter.WriteToFile(outputPath)
if err != nil {
log.Fatalf("Fail: %v\n", err)
}

duration := float64(time.Since(start)) / float64(time.Millisecond)
fmt.Printf("Processing time: %.2f ms\n", duration)
}
68 changes: 68 additions & 0 deletions pdfa/pdfa3_validate_standard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* PDF/A-3 optimization (compression) example.
*
* Run as: go run pdfa3_validate_standard.go <input.pdf>
*/

package main

import (
"fmt"
"log"
"os"
"time"

"github.com/unidoc/unipdf/v3/common/license"
"github.com/unidoc/unipdf/v3/model"
"github.com/unidoc/unipdf/v3/model/pdfa"
)

func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
panic(err)
}
}

func main() {
args := os.Args
if len(args) < 2 {
fmt.Printf("Usage: %s INPUT_PDF_PATH", os.Args[0])
return
}
inputPath := args[1]

// Initialize starting time.
start := time.Now()

// Create reader.
inputFile, err := os.Open(inputPath)
if err != nil {
log.Fatalf("Fail: %v\n", err)
}
defer inputFile.Close()

detailedReader, err := model.NewCompliancePdfReader(inputFile)
if err != nil {
log.Fatalf("Fail: %v\n", err)
}

// Apply standard PDF/A-3.
standards := []model.StandardImplementer{
pdfa.NewProfile3A(nil),
pdfa.NewProfile3B(nil),
pdfa.NewProfile3U(nil),
}

// Iterate over input standards and check if the document passes its requirements.
for _, standard := range standards {
if err = standard.ValidateStandard(detailedReader); err != nil {
fmt.Printf("Input document didn't pass the standard: %s - %v\n", standard.StandardName(), err)
}
}

duration := float64(time.Since(start)) / float64(time.Millisecond)
fmt.Printf("Processing time: %.2f ms\n", duration)
}

0 comments on commit 69dfa68

Please sign in to comment.