#78
Nov 05
Generating PDF Documents
In this episode I will show you how to create PDF documents using the excellent PDF::Writer gem.
Resources
# environment.rb require 'pdf/writer' Mime::Type.register 'application/pdf', :pdf # products_controller.rb def index @products = Product.find(:all) respond_to do |format| format.html format.pdf do send_data ProductDrawer.draw(@products), :filename => 'products.pdf', :type => 'application/pdf', :disposition => 'inline' end end end # product_drawer.rb def self.draw(products) pdf = PDF::Writer.new products.each do |product| pdf.text product.name end pdf.render end
<p><%= link_to 'PDF Format', formatted_products_path(:pdf) %></p>



I just had to say "W00000000T!"
Hi Ryan!
To generate PDF reports there's also *RGhost*. It's a gem (also available as a rails plugin) that wraps PostScript with pure Ruby syntax. The gem is very stable and tested with reports over 1k pages in various OS's. The downside is that you need ghostscript libs installed.
More info: "RGhost[Rubyforge]":http://rubyforge.org/projects/rghost/
Hello Ryan,
I must agree with others, your screencasts are really top notch. Keep up the good work!
Regarding this one, I have one question, but it is not about writing pdf documents - I want to read and parse pdf. Is there some useful gem/plugin available for this kind of stuff? I've looked around but without any success. I would really appreciate any tip or recommendation on how to get into pdf parsing with ruby/rails.
Thanks and best regards!
Hello Marcello,
i tried to work with PDF::Writer in the last weeks, and i have to say that RGhost looks really good, but do you know about some translation, because http://rghost.rubyforge.org/doc/ looks english but is in fact Portuguese...
Or does anybody has some short sample apps to build on???
Thanks
René
Hi Ryan,
1st,
thx for this one, just great as always.
2nd,
maybe you want to share your experience using other pdf-writing-tools(gems) in some words, which you mentioned to be not as easy as this one, but better.
(Additional Links in the resources of this episode would be enough)
3rd,
with the new rails (upcoming versions) you can put your mimetypes in a separate file in the folder 'configuration/initializers' which is called 'mime_types' and for example put the 'require'-stuff in a seperate file 'requirements.rb' to keep your envirements.rb file clean (no changes)
4th,
keep going ^^
There is nothing excellent about the PDF::Writer stuff... right now.
The Ruports[1] guys have just taken this over, and hopefully they make it the awesome package is could be.
1: http://www.rubyreports.org
Thanks for the (as always) well-done screencast!
I've got a Rails app that I'll be adding PDF generation to soon. I've collected some links to various blog entries and tutorials. Maybe others will find them useful.
http://del.icio.us/tjstankus/pdf
Any thoughts on the rails PDF plugin?
http://railspdfplugin.rubyforge.org
Seems a lot cleaner to me to use an .rpdf view than putting a "Drawer" class in lib.
A cool extension to this would be to also generate a flash paper doc. I'm not sure of the exact implementation, but I know a lot of the social document sharing services are doing this. That way the doc can be viewed in the browser rather than needing a separate app.
Very cool extension.
Thanks!
Hi Brian,
Flash paper is mostly generated via printing method not authored per se.
we have build a rails app urwords.com which uses it that way.
you can contact us in case you need any help
Anybody got an idea how to generate a google map in PDF?
@Marcello, I haven't tried RGhost yet. I'll have to look into it. Thanks for the suggestion!
@QuBit, a while ago I tried most of the options mentioned on this wiki page:
http://wiki.rubyonrails.org/rails/pages/HowtoGeneratePDFs
Out of those PDF::Writer was by far my favorite so I've stuck with it and haven't had any complaints. I should look into some more newer options though (RGhost).
@Paul, for simple PDFs I think a template view would work really well, but most of the PDFs I've done get fairly complex. In those cases it helps having an actual object with methods and variables.
cool~thx.
and can anybody share somethings about “how to genarate reports chart in rails".
I tried out the PDF:Writer a while ago. One thing that bothered me is that it does not support Unicode. There is a hack to make it work with some European languages (documented in the rails wiki). However, Asian languages, Arabic, and so on are not supported. And since I read that Austin Ziegler isn't working on the PDF:Writer project any more there doesn't seem to be too much hope that this will change soon. Or does any one have new info on that?
@jurglic,
FYI i just came across this... http://software.pmade.com/pdfreader
Looks like it was started in October but couldnt really find anything else on the net for reading PDFs.
你的英语讲的很好,内容也不错,我们很喜欢!
但是,有点小遗憾,就是有些东西讲的太少了.
我看过layout那部分,partial那部分没有涉及到!
Hi iceskysl, for charts I think you could use this:
http://elctech.com/2007/9/7/swfchart-generator
I didn't try it but like the results. It uses http://www.maani.us/xml_charts/index.php?menu=Gallery
which can be controlled also via PHP.
Nik
@rene and Ryan:
The translation is underway. At least the pdf manual shall be ready by the ende of the week. The code soon follows. =)
@Marcello:
this would be really great. Thanks a lot for your efforts!!!
@Nik:
your are so kind~
that's cool~
thx~
Hi Ryan,
Thanks, your screencasts, just great as always!
I wonder why you use the ProductDrawer class in lib. Why don't you use a method in the Product Model?
Thank you so much for your great screencasts! Very inspiring and informative and quite entertaining as well:-)
I ran into problems with the encoding of German Umlaute characters, such as "äöüÄÖUß" with PDF::Writer.
I have my source code encoded in UTF-8 and PDF::Writer cannot yet display UTF-8 characters.
A great solution is to overwrite PDF::Writer's text method and have it convert the text from UTF-8 to ISO-8859-15, like so:
CONVERTER = Iconv.new( 'ISO-8859-15//IGNORE//TRANSLIT', 'utf-8')
module PDF
class Writer
alias_method :old_text, :text
def text(textto, options = {})
old_text(CONVERTER.iconv(textto), options)
end
end
end
All credits go to Peter Krantz and Anibal. See http://www.peterkrantz.com/2007/utf8-in-pdf-writer/
Hi Ryan,
very nice railscast! This Railcast was very interesting.
A time extended episode about the formating seaction would be great to learn more about layout development (is this so far supported in the writer?).
In general an extend version of PDF:Writer with UTF-8 or international support planned yet?
Could this also be done easily in Ruby (asked from a beginner point of view)?
Excellent...EXCELLENT....*rasps fingers*
Hi,
I would like to implement a PDF function into my application.
I have already installed the pdf-writer-1.1.6.
Firstly, there are several folders in this zip folder e.g. pdf, writer etc.
Where exactly should I paste this folders to?
Secondly, I added the following codes to the environment.rb:
require 'pdf/writer'
Mime:Type.register 'application/pdf',:pdf
Where should I place these codes and the should the word "application" be replaced with my project's name?
I am sure that there is something wrong with the codes in here as when i restarted WEBRICK in my command prompt, some of the errors that occurred are:
- already initialized constant OPTIONS
-no such file to load -- transaction/simple<MissingSourceFile>
Can someone please give me a solution? Because there is no way I can implement the codes if I cannot start my WEBrick successfully.
This is quite urgent as the deadline is drawing nearer. Please help!
Thank you very much!!!
@Valerie:
1. Install by gems (not from source) - it should be a command line tools but in netbeans/eclipse plugins you have GUI
2. 'application/pdf' is mime type
Thanks! Error occurred...
undefined method `formatted_programmes_path'
Seriously, I don't see where i should define this.
hi, i am using pdf:writer with attachment_fu and when i am trying to load an image into the pdf like so.
avatar = @villa.avatar.public_filename()
pdf = PDF::Writer.new(:paper => "A4")
pdf.select_font "Helvetica"
pdf.image avatar, :resize => 0.75
but all i get is an error.
"No such file or directory - /avatars/0000/0007/1.jpg"
so whats wrong here?
thx for the great screencast :)
Thanks for the screencast.
Is passing instance variables as arguments the only way to get instance variables into in the drawer method? I have a controller that collects/calculates a lot of information destined for output as rhtml or pdf.
Any tips or URLs I could follow would be appreciated.
Thanks again.
Great screencast.
But I get this error:
Routing Error
no route found to match "/reporting/index.pdf" with {:method=>:get}
Can you help me?
regards
svend
@svend:
I am working on a rails 1.2.6 app that does not use RESTful routing. To get this to work for me, I added the following to routes.rb:
map.connect ':controller/:action.:format'
Check out the above link for Extremely Simple PDF Generation in Rails using HTMLDOC
Hi,
I wonder how i get an error " undefined method `pdf'". I think I follow all you stpes.
thanks
wing
By virtue of pure reason, it must not be supposed that, irrespective of all empirical conditions, the Ideal of human reason would thereby be made to contradict our sense perceptions, but the Transcendental Deduction can never furnish a true and demonstrated science, because, like the manifold, it would thereby be made to contradict disjunctive principles.
I used rfpdf since pdf-writer did not go together with activescaffold (+activescaffoldlocalize +calendar_date_select). Be sure to pick the right plugin (http://code.google.com/p/rfpdf/source/checkout), since there are two.
Now RGhost 0.8 with English Documentation. For PDF, PS, TIF, etc
The Ghost's speed!!
see http://rghost.rubyforge.org