How to convert selected pdf page with gm

两盒软妹~` 提交于 2019-12-08 04:07:32

问题


I am converting different images and pdf files with "gm" module for nodejs. Image types go successfully but when I want to convert PDF to image have problems. I need to covert only one selected page from pdf file to jpg/png. If I pass whole pdf file to "gm" it saves to image only first page, but I cannot find the way to save another page.

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

Thank you.


回答1:


for only first pdf page use:

gm(file, 'pdf.pdf[0]').toBuffer(...)

for only second pdf page use:

gm(file, 'pdf.pdf[1]').toBuffer(...)




回答2:


gm(file).selectFrame(0).toBuffer() // To get first page

gm(file).selectFrame(1).toBuffer() // To get second page




回答3:


There is spindrift for manipulating pdf (includes image conversion).

You can define your pdf using (You don't have you use all of the commands):

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 

Later on convert to image:

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

If you have to use gm, you can do what @Ben Fortune suggested in his comment and split the pdf first.



来源:https://stackoverflow.com/questions/28453823/how-to-convert-selected-pdf-page-with-gm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!