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.
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")
}