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

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

报告相同问题?

悬赏问题

  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 51单片机显示器问题