2015-02-11 34 views
1

我使用“gm”模块为nodejs转换不同的图像和pdf文件。图像类型成功,但是当我想将PDF转换为图像时出现问题。我需要将pdf文件中只有一个选定的页面转换为jpg/png。如果我将整个pdf文件传递给“gm”,它只会将图像保存到第一页,但我无法找到保存其他页面的方式。如何将所选的pdf页面转换为gm

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

谢谢。

+0

您需要拆分PDF页面并将每个页面传递给GM​​。 – 2015-02-11 12:04:32

+0

@BenFortune你能为此推荐一些工作模块吗? spindrift产生错误 – Serghei 2015-02-11 13:17:17

+0

你有没有得到解决方案 – 2016-05-02 09:55:14

回答

-1

spindrift用于处理pdf(包括图像转换)。

可以使用定义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 

后来转换为图像:

// Use the 'index' property of an image element to extract an image: 
pdf.extractImageStream(0) 

如果你必须使用通用,你可以做一些@Ben Fortune在他的评论中提出的建议,并首先将pdf分开。

+0

我试过这个库,但是如何从缓冲区创建这个“spindrift”? – Serghei 2015-02-11 12:53:30

+0

在使用toBuffer之前,您是否尝试过使用带有spindrift的“文件”变量? – 2015-02-11 12:58:17

+0

嗯,我只是想无缓冲(刚开业,并试图保存新文件),并得到了错误 '重生:PDFTK /var/www//temp/tt.pdf输出-' 'events.js:72' '扔呃; //未处理的'错误'事件' '^' 'Error:spawn ENOENT' 'at errnoException(child_process.js:1011:11)' 'at Process.ChildProcess._handle.onexit(child_process.js:802 :34)' – Serghei 2015-02-11 13:11:56

相关问题