Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2100)

Unified Diff: go/pdf/pdfium.go

Issue 1216483002: golden/pdfxform a pdf rasterization server (Closed) Base URL: https://skia.googlesource.com/buildbot@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: go/pdf/pdfium.go
diff --git a/go/pdf/pdfium.go b/go/pdf/pdfium.go
new file mode 100644
index 0000000000000000000000000000000000000000..bd6c56e306d3c1918558e4cf19a1764cf8630ce3
--- /dev/null
+++ b/go/pdf/pdfium.go
@@ -0,0 +1,65 @@
+// PDF Rasterizer
+package pdf
+
+import (
+ "fmt"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "time"
+
+ "go.skia.org/infra/go/fileutil"
+ "go.skia.org/infra/go/util"
+)
+
+const pdfiumExecutable = "pdfium_test"
+
+type Pdfium struct{}
+
+func (Pdfium) String() string { return "Pdfium" }
+
+func (Pdfium) Enabled() bool {
+ return commandFound(pdfiumExecutable)
+}
+
+// Rasterize assumes that filepath.Dir(pdfInputPath) is writable
+func (Pdfium) Rasterize(pdfInputPath, pngOutputPath string) error {
+ if !(Pdfium{}).Enabled() {
+ return fmt.Errorf("pdfium_test is missing")
+ }
+
+ // Check input
+ if !fileutil.FileExists(pdfInputPath) {
+ return fmt.Errorf("Path '%s' does not exist", pdfInputPath)
+ }
+
+ // Remove any files created by pdfiumExecutable
+ defer func() {
+ // Assume pdfInputPath has glob characters.
+ matches, _ := filepath.Glob(fmt.Sprintf("%s.*.png", pdfInputPath))
+ for _, match := range matches {
+ util.Remove(match)
+ }
+ }()
+
+ command := exec.Command(pdfiumExecutable, "--png", pdfInputPath)
+ if err := command.Start(); err != nil {
+ return err
+ }
+ go func() {
+ time.Sleep(5 * time.Second)
+ _ = command.Process.Kill()
+ }()
+ if err := command.Wait(); err != nil {
+ return err
+ }
+
+ firstPagePath := fmt.Sprintf("%s.0.png", pdfInputPath)
+ if !fileutil.FileExists(firstPagePath) {
+ return fmt.Errorf("First rasterized page (%s) not found.", firstPagePath)
+ }
+ if err := os.Rename(firstPagePath, pngOutputPath); err != nil {
+ return err
+ }
+ return nil
+}
« no previous file with comments | « go/pdf/pdf_test.go ('k') | go/pdf/poppler.go » ('j') | golden/go/pdfxform/pdfxform.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698