0

I am succesfully able to convert PDF/DOC/DOCX to PNG using Rmagick gem in Ruby on Rails in my local system.

The algorithm I am following:

1. Fetch the PDF/DOC/DOCX from a given url.

2. Download that PDF/DOC/DOCX file in local project folder temporarily.

      download_path_of_document = "#{Rails.root}/path/to/my/local/download_folder"

       open("https://my-url.com") do |doc|
          File.open(File.join(download_path_of_document, ".#{extension according to file doc/docx/pdf}"), "wb") do |file|
            file.write(doc.read)
          end
       end

3. Pass that file through the RMagick convertor code by mentioning the path of that locally downloaded file.

4. Get it converted to PNG successfully.

my_converted_pngs = Magick::ImageList.new(File.join(download_path_of_document, ".#{extension according to file doc/docx/pdf}"))

5. Delete the downloaded file that was downloaded in step 2.

This method is working absolutely fine in my local system (environment).

But something weird is happening in my staging server. I am getting an error like : unable to open image `/tmp/magick-14238gTk5-CaYAECT': No such file or directory @ error/blob.c/OpenBlob/2712. The weird thing is that I am getting this error only when I pass DOC and DOCX file in step 3; in case of PDF it is working fine.

Kindy please help. (If any doubt regarding questions please ask multiple times in comments)

ruby - 2.2.2 rails - 4.2.2 gem - rmagick

  • Do you have Ghostscript installed with ImageMagick? Does your ImageMagick policy.xml file restrict the use of Ghostscript. You may need to edit it. See https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413 – fmw42 Oct 09 '20 at 06:17

1 Answers1

0

Hey I don't know if this will help, but I had this similar problem which would return the same error. For my use case I was requesting a page which was doing 2 simultaneous process. First process was fetching an image, second process was generating the image that was to be fetched.

I discovered that because rails Puma server is single threaded, my computer would not be able to execute both of these processes at the same time.

What I did is I added another concurrency to Puma.

#puma.rb
#uncomment this line and add a concurrency of 2    
workers ENV.fetch("WEB_CONCURRENCY") { 2 }

Now kill rails server.

Next time you run rails server you should be able to see something different. Process workers shouldve been increased to 2.

If you don't see this change try entering bundle exec puma -C config/puma.rb

Hope this was of help.

tofus
  • 31
  • 3