5

The example code below convert pdf to jpeg by using bimg.

func main() {

    buffer, err := bimg.Read("test.pdf")
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }

    newImage, err := bimg.NewImage(buffer).Convert(bimg.JPEG)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
    }

    if bimg.NewImage(newImage).Type() == "jpeg" {
        fmt.Fprintln(os.Stderr, "The image was converted into jpeg")
    }

    bimg.Write("test.jpg", newImage)

}

But it only convert 1st page of test.pdf.

Is there any way that convert to image that contain more than one page.

khue bui
  • 1,366
  • 3
  • 22
  • 30

1 Answers1

14

bimg uses libvips, and can potentially load PDFs. Unfortunately, the default for libvips loading PDFs is to load one page only. Unless you want to modify bimg (vendor, contribute, hack the source, etc.) you're out of luck.

Not an answer to the question (not using bimg), but you can use imagemagick instead,

import "gopkg.in/gographics/imagick.v3/imagick"
func main() {
    imagick.Initialize()
    defer imagick.Terminate()
    mw := imagick.NewMagickWand()
    defer mw.Destroy()
    mw.ReadImage("test.pdf")
    mw.SetIteratorIndex(0)        // This being the page offset
    mw.SetImageFormat("jpg")
    mw.WriteImage("test.jpg")
}
Patrick Tou
  • 164
  • 1
  • 3
  • 1
    Thank Patrick, I have run example above with some test files and imagick.v2. Somehow, some large files ( > 15 pages ) was fail, but with two, three pages pdf file is ok. I dont know why??, but anyway thank you, it's enough for me. – khue bui Nov 28 '17 at 04:42