shairontoledo / rghost

RGhost is a document creation and conversion API. It uses the Ghostscript framework for the format conversion, utilizes EPS templates and is optimized to work with larger documents. Support(PDF,PS,GIF,TIF,PNG,JPG,etc)
http://rghost.rubyforge.org
MIT License
187 stars 46 forks source link

Converting PDF to Images #68

Closed aalvrz closed 6 years ago

aalvrz commented 6 years ago

I am following this Wiki page to convert a PDF file to multiple images. However, I am seeing a few strange things on the way that rghost works.

For example, according to the Wiki, the following command will return an array of File objects:

pages = RGhost::Convert.new('/tmp/test.pdf').to :jpeg, :multipage => true, :range => 5..6

Yet I get an array of strings instead while the converted files get generated in my /tmp/ directory.

I am trying to find a way to specify the name of each file converted that gets created and where to put it in my file system, yet I can't find a way to do this in the Wiki. Can you help me?

shairontoledo commented 6 years ago

what does come from puts pages.inspect?

aalvrz commented 6 years ago

@shairontoledo

This:

["/tmp/17879240-349300-1506537028_0002.jpeg", "/tmp/17879240-349300-1506537028_0001.jpeg"]

You can see that it is an array of strings:

> pages.first.class
=> String
shairontoledo commented 6 years ago

Try to rename the file like:

range = 5..6

pages = RGhost::Convert.new('/tmp/test.pdf').to :jpeg, multipage: true, range: range
dest_dir = "/your/dir/here/"
files = range.to_a.zip(pages).map do |page_number, filename| 
  dest_file = "#{dest_dir}/page_#{page_number}.jpeg" 
  FileUtils.mv(filename, dest_file)
  File.open(dest_file)
end
aalvrz commented 6 years ago

@shairontoledo Seems like a very cumbersome way of achieving this.