/* * PDF optimization (PDF/A standard applying) example. * * Run as: go run pdf_apply_standard.go */ package main import ( "fmt" "log" "os" "time" "github.com/unidoc/unipdf/v4/common/license" "github.com/unidoc/unipdf/v4/model" "github.com/unidoc/unipdf/v4/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-1B. pdfWriter.ApplyStandard(pdfa.NewProfile1B(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) }