Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UP-478] Pdf sanitization example #228

Merged
merged 4 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/unidoc/globalsign-dss v0.0.0-20220330092912-b69d85b63736
github.com/unidoc/pkcs7 v0.1.1-0.20220329190817-dd59b9eba14c
github.com/unidoc/unichart v0.1.0
github.com/unidoc/unipdf/v3 v3.46.0
github.com/unidoc/unipdf/v3 v3.48.0
github.com/wcharczuk/go-chart/v2 v2.1.0
go.opencensus.io v0.24.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,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.1.0 h1:GoJ/rxSoOYZsqlG3yOJpKkwgfsIQgb9hHX7bILZHcCg=
github.com/unidoc/unichart v0.1.0/go.mod h1:9sJXeqxIIsU2D07tmhpDMoND0mBFRGfKBJnXZMsJnzk=
github.com/unidoc/unipdf/v3 v3.46.0 h1:FjYaPgHPt2Guk7KeRsFnxbR9Ftp0qZsVaif0+lu8/f0=
github.com/unidoc/unipdf/v3 v3.46.0/go.mod h1:g42g9gaGCT2hLoNK+r/RZdNVnvhF1X6qx6wpTKJwg2E=
github.com/unidoc/unipdf/v3 v3.48.0 h1:aI/7HAvZApdpT5zJ7GlCbaNwrqhM9R1oRbNQOVrjsYI=
github.com/unidoc/unipdf/v3 v3.48.0/go.mod h1:g42g9gaGCT2hLoNK+r/RZdNVnvhF1X6qx6wpTKJwg2E=
github.com/unidoc/unitype v0.2.1 h1:x0jMn7pB/tNrjEVjy3Ukpxo++HOBQaTCXcTYFA6BH3w=
github.com/unidoc/unitype v0.2.1/go.mod h1:mafyug7zYmDOusqa7G0dJV45qp4b6TDAN+pHN7ZUIBU=
github.com/wcharczuk/go-chart/v2 v2.1.0 h1:tY2slqVQ6bN+yHSnDYwZebLQFkphK4WNrVwnt7CJZ2I=
Expand Down
Binary file added sanitize/outputs/JSPopupCalendarSanitized.pdf
Binary file not shown.
83 changes: 83 additions & 0 deletions sanitize/pdf_sanitize_document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* This example demonstrates the usage of Sanitizer package to sanitize document.
*
* Run as: go run pdf_sanitize_document.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/sanitize"
)

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: pdf_sanitize_document.go <INPUT_PDF_PATH> <OUTPUT_PDF_PATH>\n", os.Args[0])
return
}
inputPath := args[1]
outputPath := args[2]

// Initialize starting time.
start := time.Now()
pdfReader, f, err := model.NewPdfReaderFromFile(inputPath, nil)
if err != nil {
log.Fatalf("Failed to create reader: %v", err)
}
defer f.Close()

pdfWriter, err := pdfReader.ToWriter(nil)
if err != nil {
log.Fatalf("Failed to get writer object: %v", err)
}

// Define sanitization options and set writer's optimizer
opts := sanitize.SanitizationOpts{
JavaScript: true,
URI: true,
GoToR: true,
GoTo: true,
RenditionJS: true,
OpenAction: true,
Launch: true,
}
pdfWriter.SetOptimizer(sanitize.New(opts))

// Write to file.
pdfWriter.WriteToFile(outputPath)

// Measure processing time.
duration := float64(time.Since(start)) / float64(time.Millisecond)

inputFileInfo, err := os.Stat(inputPath)
if err != nil {
fmt.Printf("Failed to get inputFile info %v", err)
}

outputFileInfo, err := os.Stat(outputPath)
if err != nil {
fmt.Printf("Failed to get outputFile info %v", err)
}

// Print information.
fmt.Printf("Input file size %d bytes\n", inputFileInfo.Size())
fmt.Printf("Output file size %d bytest\n", outputFileInfo.Size())
fmt.Printf("Processing time %.2f ms", duration)

}
Binary file added sanitize/test-files/JSPopupCalendar.pdf
Binary file not shown.
Loading