How can I inject a dynamically generated image (barcode, as it happens) into a PDF document (I'm in rails if that matters)? - ruby-on-rails

I'm aware of pdf-stamper, but I'm trying to avoid switching everything to jruby right now.
I just need to "stamp" an image that I generate within the rails app (a PDF417 barcode) into a form field in the PDF document (there's an FDF; it's a document template kinda thing).
I'm filling out the text-based fields by just shelling out to pdftk, so if there's a way to do it using pdftk, I'd be fine with that, but I've looked high and low for one without any luck.

How about using a barcode font? some alternatives too. I haven't used that one but there may be others available too

I know I'm late to the party, but the PDF417 Rubygem should do what you need. https://rubygems.org/gems/pdf417 will generate it and if you have chunky_png installed you can easily write out PNGs to a file.

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.

Creating PDF from photos on a Facebook Rails app

I'm creating a Facebook app using Rails and hosted on Heroku and I'm having a bit of trouble finding the ideal way to solve a problem. I want the user to be able to move their photos around on the screen, position them, and then download that as either a PDF or a PNG file to be emailed or printed. I have the app getting the user's facebook photos and they can drag them on to a HTML5 Canvas element to position them. Taking this canvas, however, and converting it into something printable is where I'm hitting a dead end.
Basically I have a few ideas I have tried:
Convert the Canvas to a PNG using toDataURL() - Would work perfectly but since the photos are external, the canvas is "dirty" and will throw up a security issue. I've thought about trying to copy the pixels of each image to a pixel array but I've heard this may not work either due to security issues. Since the app is dealing with people's facebook images I really don't want to store them on the app's server.
Use PDFKit/wkhtmltopdf to create a PDF using Rails - I've tried this, but since the app is a Sinatra app (I think), it's confusing me a lot. It's throwing errors with the to_pdf call saying 'Command Failed'. I've tried adding a config.middleware.use line but I'm not 100% sure where to put it and everywhere seems to be failing saying "config" is an undefined variable. Also installing wkhtmltopdf seems to fail on Heroku once I test it outside localhost.
Use Prawn to create a PDF using Rails - I've tried prawn but it seems to have a similar problem to PDFKit and I get confused about what goes where on a Sinatra app. I'm sure I read as well that people were having problems with it too.
Have I missed any obvious solutions to this, or is there something I'm not thinking of? All I want to do is create some kind of easily printable file with positioning intact that can be easily downloaded and printed by the user but I've come across so many problems that I don't know where to go next and I'm going round in circles.
If anyone had any advice for me about how I could get around this problem I'd really appreciate it.
if prawn is giving you grief just use one of the jquery plugins to print your div content. You could even configure a pdf printer and print the document instead of hard copy if you so wish/need images in pdf format.
I use http://archive.plugins.jquery.com/project/jqPrint plugin in one of my projects and it works like a charm.
It sounds like many of your issues relate to the necessary PDF binaries not being accessible on Heroku. In following with the twelve factor approach to dependency isolation Heroku purposely provides a very bare system.
If you need to run a custom binary on Heroku I'd suggest looking at a tool called Vulcan which can compile a binary that's compatible with the Heroku runtime.

Is Prince the best way to create PDFs in Ruby on Rails?

After several Google searches, it appears that the way to create PDFs in Rails from HTML and CSS (versus a new markup language) is to use Prince.
With licensing at $3800 for my non-big-commercial app, I'm wondering if this is, in fact, consensus or people have an alternative they can share the whats and hows.
You may check out prawn too. Tutorial can be found on railscasts.com.
This may fit the bill: http://code.google.com/p/wkhtmltopdf/
We tried tow solutions:
using latex generate pdf, there is ruby gem code rtex
using java library iText, use it you may need rjb which allow you using java lib directly in ruby code, just like jruby, but you don't need build all you application on jruby.
I create tons of different PDF files on the fly from various data sources using Rails, including finest layout. I create need to create them for presenting products to customers.
After having tried all the tools mentioned above, Prince is the best tool for this task.
Prince's rendering quality & CSS support (better than some browsers) is its main selling point. If you're only generating documents with simple layouts, stick with Prawn.

WYSIWYG image uploads in Rails App

Can anyone recommend a way of creating a view where users can upload images to my app through a WYSIWYG editor?
I've tried solving this using CK Editor and Paperclip but am having lots of trouble... Maybe I'm going about this the wrong way.
If someone's done this before I'd really like to know how! I don't have a editor or file storage mechanism preference so fire away...
This is all dependent on the WYSIWYG's file upload API. From there, just build an ImagesController to handle requests from that API, use whatever system (Paperclip is good) to handle those files internally, and you should be good to go. You won't find a plug-and-play solution; you'll have to hand-roll it.
Turns out that, with more targeted Google searching, you can find a preexisting solution. Here's one for TinyMCE and Rails. You may, however, end up finding that it doesn't meet your needs, in which case I would not be surprised to find that creating your own solution would be simpler than you expect :)
You could try Bootsy. It's a WYSIWYG editor with image upload capability. Includes a (rather simple) image manager as well.
https://github.com/volmer/bootsy
There is an other solution for rails out there:
https://github.com/spohlenz/tinymce-rails
You can load it as gem and configure it via a yml file. And it comes with an extra language gem.

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