Skip to content

Commit

Permalink
start converting stuffs!
Browse files Browse the repository at this point in the history
  • Loading branch information
vtno committed May 18, 2022
0 parents commit 1bd5303
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2023.*/
*.pdf
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.17
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# PDFTOJPG

No frils CLI command to convert pdf to jpg image(s).

## Prerequisite
- GO 1.17
- imagemagick

## Build
```shell
# install imagemagick
brew install ghostscript imagemagick
# allow CFLAG to compile main.go
export CGO_CFLAGS_ALLOW='-Xpreprocessor'

go build # will output `pdftojpg` binary
```

## Usage

```shell
# pass in pdf source, jpg output, number of page
./pdftojpg 2023.3.10.pdf 2023.3.10 440
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/vtno/pdftojpg

go 1.17

require gopkg.in/gographics/imagick.v3 v3.4.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gopkg.in/gographics/imagick.v3 v3.4.0 h1:kSnbsXOWofo81VJEn/Hw8w3qqoOrfTyWwjAQzSdtPlg=
gopkg.in/gographics/imagick.v3 v3.4.0/go.mod h1:+Q9nyA2xRZXrDyTtJ/eko+8V/5E7bWYs08ndkZp8UmA=
94 changes: 94 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
Binary file added pdftojpg
Binary file not shown.

0 comments on commit 1bd5303

Please sign in to comment.