Use XServer in Heroku - ruby-on-rails

I'm using wicked_pdf gem to generate pdf files in my Rails application. This gem uses wkhtmltopdf to convert a HTML file to PDF.
The HTML files I'm converting to PDF contains JavaScript used to generate charts. In order to wkhtmltopdf properly render those charts I need to set the use_xserver option to true.
Everything works fine locally. However, when I try it on Heroku, I get the following error message:
Error: PDF could not be generated!
Command Error: wkhtmltopdf-amd64: cannot connect to X server
Does Heroku runs a X server? Is it possible to do so?

You probably shouldn't run an X-server in the dyno just to generate pdfs. It looks like wkhtmltopdf can be made to run headless, check out these questions:
wkhtmltopdf: cannot connect to X server
wkhtmltopdf extremely slow on Debian without X-server

Related

While converting PDF/DOC/DOX to PNG via Rmagick error : No such file or directory # error/blob.c/OpenBlob/2712 - Ruby on Rails

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
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.

How to run rails server - execute wkhtmltopdf - stop rails server

I've been long time investigating how to generate complex .pdf files (many images and 30 pages aprox). from a html web, of my rails application.
At the end I realized than the best option is wkhtmltopdf from command line.
using pdfKit gem don't work in production, but it works in development
using wkhtmltopdf from command line, works in development but neither in production.
I have read a lot of issues with wkhtmltopdf, so I abandon use wkhtmltopdf getting info from production web server. --> Instead I have created a new environment "genera_pdf" to run in production database, with development configuration (assets, cache..etc)
And now I need to run some proces than execute this:
1) RAILS_ENV=genera_pdf rails s
2) wkhtmltopdf localhost:3000 result.pdf
3) stops (CTRL+C) rails s
If this is achievable, Whats the best way to do this in Linux?
I'm absolutly missed...with rake tasks? or rails runner? or Cron-task?
Thanks a lot

Rails Rspec/Capybara feature spec asking me to install ImageMagick?

I am writing some integration specs that test forms for uploading user content to a site. I get the following error though I have ImageMagick installed and it is working perfectly in the development environment.
[paperclip] An error was received while processing: #<Paperclip::Errors::CommandNotFoundError: Could not run the `identify` command. Please install ImageMagick.>
I know I could stub out ImageMagick as described here however I would like to get it working without stubbing out the service. How do I make sure Rspec can access ImageMagick?
I needed to add the Paperclip command path to the environments/test.rb file.
To do this I typed which convert in the terminal to get the ImageMagick path which was /usr/local/bin/convert. I used this info to insert the following line in the test environment file:
Paperclip.options[:command_path] = "/usr/local/bin/"
Done

MiniMagick error. undefined method 'resize'

This error only happens when executed by the Ruby on Rails server.
I've tried bypassing Carrierwave's resize option by implementing my own but it has the same result.
when I run a script from command line (in which I require the 'mini_magick' gem). It all works flawlessly.
the MiniMagick gem seems to be installed correctly and to be honest I'm kinda out of ideas to tackle this problem.
some versions (not sure if usefull or not):
ruby - 1.9.3p545
rails - 4.1.1
mini_magick - 4.0.1
ImageMagick - 6.9.0-0 Q16
Any help is appreciated.
-edit, I'm using windows 8.1
example code:
require 'mini_magick'
filename = 'test.jpg'
image = MiniMagick::Image.open(filename)
image.resize "1000x1000"
image.format "png"
image.write "test.png"
The code above works flawlessly when executed from cmd.
thumb = MiniMagick::Image.open(i.image.path)
thumb.resize "200*200"
code snippet above throws error in RoR when placed in a controller (Create)
alternatively, uncommenting the resize option directly from carrierwave throws the exact same error.
# version :thumb do
# process :resize_to_fill => [200, 200]
# end
Also a little note:
I've disabled minimagicks validations to figure out what causes the error it threw at that time:
Failed to manipulate with MiniMagick, maybe it is not an image? original error: executable not found: "identify"
I hope this clears a few things up.
-edit
I've decided to leave windows as a dev platform and switch to linux as a dev platform. fixed all of my issues (In hindsight my error was probably due to some lacking dependencies on the imagemagick side).
I experienced a similar problem recently with the following environment:
Windows 8.1
Ruby 2.1.5p273
Ruby Dev Kit 2.1.5
Rails 4.2.0.beta4
carrierwave 0.10.0
mini_magick 4.0.1
ImageMagick6.9.0-Q16 installed at C:\ImageMagick-6.9.0-Q16
In the rails console within RubyMine, I would get:
executable not found: "identify"
after keying in just two things:
filename = 'test.jpg'
image = MiniMagick::Image.open(filename)
One of the steps was to add C:\ImageMagick-6.9.0-Q16 to my Windows PATH environment variable. At first, that didn't help. However, after closing and re-opening RubyMine, the problem went away -- I assume because the change to the PATH env var (done with an independent Windows command prompt) didn't affect the RubyMine IDE environment.
executable not found: "identify"
This is an error message from MiniMagick (Source code). I guess, as the error message says, your server cannot find executable "identify".
Make sure that your ENV['PATH'] has the path to where the "identify" exists.
I'm running into the same problem.
it works flawlessly on my localhost, but on the server (windows 2012), it only works as a rake task. when run inside the application, then it fails saying
NoMethodError (undefined method `resize' for #<MiniMagick::Image:0x28c3260>):
Unfortunately, the path variable is set...
I got it to work by doing the minimagick-stuff inside the model instead of the controller.... nasty workaround.

Errno::ENOENT when uploading an image using Carrierwave and MiniMagick

My rails app has an image upload which uses the latest versions of Carrierwave and MiniMagick. It works great on the server, but in my local host which is WEBrick and Windows 7, i get this error when i try and upload an image:
Errno::ENOENT in PostsController#create
No such file or directory - identify -ping C:/Users/Ryan/AppData/Local/Temp/mini_magick20121024-6068-g2bed8.jpg
I go to the path and the image actually exists, i'm not sure what prevents Carrierwave or MiniMagick from reading it.
Any one has an idea?
Do you have ImageMagick installed? Can you run the identify command manually on the console?
The reason I ask is it appears your error message is what occurs when it can't find the identify commend, which is part of ImageMagick.

Resources