如何使用 gm 转换选定的 pdf 页面

Ser*_*hei 5 pdf imagemagick image-processing node.js graphicsmagick

我正在使用 Nodejs 的“gm”模块转换不同的图像和 pdf 文件。图像类型成功,但当我想将 PDF 转换为图像时遇到问题。我只需要将选定的一页从 pdf 文件转换为 jpg/png。如果我将整个 pdf 文件传递​​给“gm”,它只会将第一页保存到图像,但我找不到保存另一页的方法。

gm(file).toBuffer(format.toUpperCase(), 
       function (err, buffer) {
    // so in buffer now we have converted image
 }
Run Code Online (Sandbox Code Playgroud)

谢谢。

小智 5

你可以gm.selectFrame这样使用

gm(file).selectFrame(0).toBuffer() // To get first page
gm(file).selectFrame(1).toBuffer() // To get second page
Run Code Online (Sandbox Code Playgroud)


Or *_*Guz -2

有用于操作 pdf 的spindrift(包括图像转换)。

您可以使用以下命令定义 pdf(您不必使用所有命令):

var pdf = spindrift('in.pdf')
   .pages(7, 24)
   .page(1)
   .even()
   .odd()
   .rotate(90)
   .compress()
   .uncompress()
   .crop(100, 100, 300, 200) // left, bottom, right, top 
Run Code Online (Sandbox Code Playgroud)

稍后转换为图像:

// Use the 'index' property of an image element to extract an image: 
pdf.extractImageStream(0)
Run Code Online (Sandbox Code Playgroud)

如果你必须使用 gm,你可以按照 @Ben Fortune 在他的评论中建议的方法,先分割 pdf。