| OLD | NEW |
| (Empty) | |
| 1 // PDF Rasterizer |
| 2 package pdf |
| 3 |
| 4 import ( |
| 5 "fmt" |
| 6 "os" |
| 7 "os/exec" |
| 8 "path/filepath" |
| 9 "time" |
| 10 |
| 11 "go.skia.org/infra/go/fileutil" |
| 12 "go.skia.org/infra/go/util" |
| 13 ) |
| 14 |
| 15 const pdfiumExecutable = "pdfium_test" |
| 16 |
| 17 type Pdfium struct{} |
| 18 |
| 19 func (Pdfium) String() string { return "Pdfium" } |
| 20 |
| 21 func (Pdfium) Enabled() bool { |
| 22 return commandFound(pdfiumExecutable) |
| 23 } |
| 24 |
| 25 // Rasterize assumes that filepath.Dir(pdfInputPath) is writable |
| 26 func (Pdfium) Rasterize(pdfInputPath, pngOutputPath string) error { |
| 27 if !(Pdfium{}).Enabled() { |
| 28 return fmt.Errorf("pdfium_test is missing") |
| 29 } |
| 30 |
| 31 // Check input |
| 32 if !fileutil.FileExists(pdfInputPath) { |
| 33 return fmt.Errorf("Path '%s' does not exist", pdfInputPath) |
| 34 } |
| 35 |
| 36 // Remove any files created by pdfiumExecutable |
| 37 defer func() { |
| 38 // Assume pdfInputPath has glob characters. |
| 39 matches, _ := filepath.Glob(fmt.Sprintf("%s.*.png", pdfInputPath
)) |
| 40 for _, match := range matches { |
| 41 util.Remove(match) |
| 42 } |
| 43 }() |
| 44 |
| 45 command := exec.Command(pdfiumExecutable, "--png", pdfInputPath) |
| 46 if err := command.Start(); err != nil { |
| 47 return err |
| 48 } |
| 49 go func() { |
| 50 time.Sleep(5 * time.Second) |
| 51 _ = command.Process.Kill() |
| 52 }() |
| 53 if err := command.Wait(); err != nil { |
| 54 return err |
| 55 } |
| 56 |
| 57 firstPagePath := fmt.Sprintf("%s.0.png", pdfInputPath) |
| 58 if !fileutil.FileExists(firstPagePath) { |
| 59 return fmt.Errorf("First rasterized page (%s) not found.", first
PagePath) |
| 60 } |
| 61 if err := os.Rename(firstPagePath, pngOutputPath); err != nil { |
| 62 return err |
| 63 } |
| 64 return nil |
| 65 } |
| OLD | NEW |