-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (75 loc) · 2.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"fmt"
"log"
"os"
"strconv"
"gopkg.in/gographics/imagick.v3/imagick"
)
func main() {
if len(os.Args) == 1 {
fmt.Println("Usage: pdftojpg <pdf file> <output> <number of page>")
os.Exit(1)
}
pdfName := os.Args[1]
imageName := os.Args[2]
pagesStr := os.Args[3]
fmt.Printf("Converting %s to %s\n", pdfName, imageName)
if pdfName == "" || imageName == "" || pagesStr == "" {
log.Fatal("No pdf file or output file or number of pages specified")
os.Exit(1)
}
pages, err := strconv.ParseInt(pagesStr, 10, 32)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
if err := ConvertPdfToJpg(pdfName, imageName, pages); err != nil {
log.Fatal(err)
os.Exit(1)
}
}
// ConvertPdfToJpg will take a filename of a pdf file and convert the file into an
// image which will be saved back to the same location. It will save the image as a
// high resolution jpg file with minimal compression.
func ConvertPdfToJpg(pdfName string, imageName string, pages int64) error {
// Setup
imagick.Initialize()
defer imagick.Terminate()
mw := imagick.NewMagickWand()
defer mw.Destroy()
// Must be *before* ReadImageFile
// Make sure our image is high quality
if err := mw.SetResolution(100, 100); err != nil {
return err
}
// Load the image file into imagick
if err := mw.ReadImage(pdfName); err != nil {
return err
}
// Must be *after* ReadImageFile
// Flatten image and remove alpha channel, to prevent alpha turning black in jpg
if err := mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE); err != nil {
return err
}
// Set any compression (100 = max quality)
if err := mw.SetCompressionQuality(50); err != nil {
return err
}
for i := int64(0); i < pages; i++ {
// Select only first page of pdf
mw.SetIteratorIndex(int(i))
// Convert into JPG
if err := mw.SetFormat("jpg"); err != nil {
return err
}
// Save File
err := mw.WriteImage(fmt.Sprintf("%03d.jpg", i + 1))
if err != nil {
log.Fatal(fmt.Sprintf("error at page: %d", i+1))
fmt.Printf("error: %v", err)
os.Exit(1)
}
}
return nil
}