Ruby gem to resize PDFs? - ruby-on-rails

I have a Shipcloud.io implementation in my Rails app. A successful API call generates a shipping label in PDF format. I then need to convert the given PDF to another size (DIN A5 format, specifically). How can this be done in Rails? I checked out the wicked_pdf gem but it seems that only generates html to pdf.

Use imagemagick,
then use ruby system call method.
something like,
system("convert original.pdf -resize "{width}x{height}" resized.pdf")

The rmagick gem is one way to go. It uses the imagemagick binary in the background and therefore you have to install it before you can use the ruby gem. I use it everyday for work.
There are also other variants as well. For example the mini_magick gem boosts better memory management and also supports ImageMagick or GraphicsMagick.
https://rmagick.github.io/
https://github.com/minimagick/minimagick

what's the size of the shipping label you're currently getting? For a few carriers you can change the size from within your shipcloud backoffice. The shipping labels are all in standard sizes so be aware that when resizing them you could end up with a label that won't be accepted by the carrier.

Related

Ruby Gem to generate custom Text over Image Banner

I have a slider which shows some custom made images on the main site.
At the moment every time I want to change something I need to deploy a new image with the changed text.
I was wondering if there is a gem which can be settled up to put some custom text over a default image if several conditions are met.
To do any kind of programatic image manipulation I would look at imagemagick. Think of photoshop in an open source binary. As imagemagick has been written in C you will not be able to call it directly. Instead use rmagick. Rmagick provides a nice ruby interface around imagemagick's c based API. There might be an updated way to do this as I have not worked with images in ruby for years however it's the best place to start. GL.

Which is fast? Using minimagick gem in ruby or using direct system call convert (ImageMagick) in linux

I am using a freescale board with low processing capacity. I need to resize images of various sizes into 800x480 pixels. I had used imagemagick to resize it through direct system call. System is taking a lot of time to convert large images. I thought of using minimagick since ruby is also installed in my system for other purposes. I need some advice on the performance of using minimagick( I know Rmagic is slow).
Summary:
1) bash$ convert image1.jpg -resize 800x480 image2.jpg
2) ruby miniconvert.rb
where miniconvert.rb is
require 'mini_magick'
image = MiniMagick::Image.open("image1.jpg")
image.resize "800X480"
image.write("image2.jpg")
Which will run faster on a single core low cpu machine with minimal linux installed?
Also, Is minimagick directly using system call (ie, calling convert of imagemagick)?
It doensn't matter much, because MiniMagick uses the command line tool of ImageMagick as well, according to its Readme (it uses mogrify instead of convert).
So… the gem is probably more convenient, the command line probably just a bit faster (no overhead).

How to take screenshot of a website with Rails 3.1? -- without using a service

Almost every answer I've found references using some existing service. Is there a way to do this using Rails 3.1 programmatically? This was dead easy to do with PHP (there are prebuilt libraries in PHP that do this).
What I'm looking to do, given a URL, is:
Take a screenshot of the website
Crop it (only take the top left most 100x100 pixels
PS. Here is my environment: Rails 3.1, Ruby 1.9.2
Note: The solution would probably need to follow any redirections on the URL as well.
Updates:
I've seen https://github.com/topfunky/osxscreenshot The problem is that it requires an older version of Ruby (1.8.x) and will only work on my dev Mac machine.
I've seen the vulnerability with Wordpress (they have a service that doesn't do any rate limiting which someone could potentially abuse). I would not want to abuse their resources for my benefit... http://s.wordpress.com/mshots/v1/http%3A%2F%2Fstackoverflow.com%2F?w=500
The cropping will be easy with http://rmagick.rubyforge.org/ or https://github.com/thoughtbot/paperclip
There is a Rails gem for this task.
gem install selenium-webdriver
Simple use case:
require 'selenium-webdriver'
width = 1024
height = 728
driver = Selenium::WebDriver.for :firefox
driver.navigate.to 'http://domain.com'
driver.execute_script %Q{
window.resizeTo(#{width}, #{height});
}
driver.save_screenshot('/tmp/screenshot.png')
driver.quit
This might help: https://github.com/csquared/IMGKit
You could use wkhtmltoimage to load up the webpage and save it as an image, then imagemagick, (or one of the ruby wrappers for it) to crop it.
wkhtmltoimage www.google.com output.jpg
convert -crop 100x100+0+100 output.jpg cropped.jpg
There isn't a prebuilt wkhtmltoimage binary for OSX though, so perhaps you may want to use wkhtmltopdf instead and then imagemagick to convert to an image.
wkthmltopdf www.google.com output.pdf
convert -crop 100x100+0+100 output.pdf cropped.jpg
A simple, but Mac only, solution seems to be http://www.paulhammond.org/webkit2png/
Just chmod -x that script and use as python webkit2png http://www.google.com/ and it creates 3 files:
Full screenshot
Thumbnail of the top most portion of site
Thumbnail of the full screenshot

Can wicked_pdf or wkhtmltopdf generate a protected pdf?

So, I have wicked_pdf up and running, now I am wondering if I can password protect the pdf being generated so it is not easily editable. Is this possible? Perhaps a better question is can wkhtmltopdf generate a protected pdf?
If not, is there a great rails pdf gem or similar that can take an existing pdf and protect it?
Thanks!
Let me check the usage for wkhtmltopdf (v0.10.0 rc2).
Nope, at least not according to it's usage dump. And wicked_pdf just wraps wkhtmltopdf, so you're out of luck.
There are any number of OSS PDF projects floating around out there that could encrypt your PDF output in a second pass.

Ruby/Rails image processing libraries

Good friends of stackoverflow, I am here today for guidance regarding image processing/manipulation using Ruby in a Rails environment. I'm creating on-the-fly dynamic banner ads that will feature mostly (if not completely) text. It's fairly simple with just a line or two, but I'd like the option to adjust font, text color, text size, etc.
What libraries do you recommend for this sort of task?
I've read up in rMagick a little bit and I see a lot of complaints about memory issues and lack of text rendering features. I'm not seeing many alternative active projects.
Thanks!
Edit: I got a chance to mess around with RMagick and while it's library is full featured, it seriously lacks in the text department. One feature I'm unable to use is non-breaking spaces. I'm printing a phone number in my text and it really doesn't make sense to have the area code on a different line than the rest of the number.
I'm choosing RMagick as the best solution for now, because it's full-featured and actively developed, but it is by no means a good solution.
I wrote something like the following:
require 'rubygems'
require 'RMagick'
include Magick
image = Image.new(50, 50) {
self.background_color = "white"
}
text = Draw.new
text.annotate(image, 0,0,0,40, 'Named Colors') {
self.fill = 'black'
self.pointsize = 32
}
image.write("image.png")
Which should be easy enough to follow. Also have a look at the documentation. Whilst it's not quite laid out to perfection, it's pretty much all there.
Recently I have been experimenting with creating charts/graphs from data sets using Ruby. When I was unable to find any libraries or gems that really did what I wanted, I started tinkering around with SVG graphics and I found that they are actually fairly simple to create. The SVG format is simply plain-text XML. I built an SVG image in Inkscape, saved it to a plain SVG file, and my Ruby script uses that file as a template (I dynamically update a few line elements and several text labels and leave the structure of the file intact). SVG gives you all sorts of font options (like CSS or HTML).
These two tutorials give you a quick look at SVG and how you can build an image fairly quickly with any app or language that can write to a text file. Ruby can use plain old puts to build a SVG file, or you can let the 'builder' gem do the formatting for you.
You can farm out your image processing needs to the command-line version of ImageMagick instead of using the the rMagick Ruby bindings.
Are you limited to MRI Ruby? If there's any way you can get access to a jRuby instance (maybe on an EC2 stack for instance) you could make use of the wonderful Processing library. A project I worked on before did something similar with Processing via jRuby, it's really quite a potent combination.
You can use mini-magick to work with ImageMagick in Ruby.
The ImageScience library is made for people who hate rMagick's poor memory usage, etc. I use it as a backend processor for the attachment_fu plugin, which makes it easy to make an image model in Rails.
It may be worth having a look at the image_processing gem:
https://github.com/janko/image_processing
It makes use of either ImageMagick or VIPS (the latter being even more efficient).

Resources