donglong9745 2017-11-26 03:47
浏览 1024
已采纳

Golang通过Bimg将pdf转换为图像

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.

  • 写回答

1条回答 默认 最新

  • dqfxao2898 2017-11-27 22:03
    关注

    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")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?