问题描述
下面的示例代码通过使用 bimg 将pdf转换为jpeg.
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)
}
但是它只能转换test.pdf
的第一页.
有什么方法可以转换成包含一页以上的图像.
bimg使用libvips,并且可以可能会加载PDF .不幸的是,用于加载PDF的libvips的默认设置是仅一页.除非您要修改bimg(供应商,贡献,破解源代码,等等.)
不是问题的答案(不使用bimg),但是您可以改用imagemagick
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")
}
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")
}
这篇关于Golang通过Bimg将pdf转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!