I started using HTMLDOC at first, which is fine if you've already had the HTML version. Really, I like it. It is pretty simple to set up and running. You do not need to care about too much about the layout and format of the file, because it is relatively easy to do that in a html.erb file. But there is a catch on production servers, if you have, like me, several different types of environment, Red Hat, Ubuntu, OS X, you really have to be care of the PATH to the binary htmldoc command. Otherwise the Phusion Passenger will throw out very strange error message.
After I came back from Ruby Conf 2008(if you were there, you problaly knew Gregory Brown, he held a hack session with Prawn.), so I decide to give Prawn a try.
Here are the steps( of cause within Merb):
1. Add mime type in your config file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Merb::BootLoader.after_app_loads do | |
Merb.add_mime_type(:pdf, :to_pdf, %w[application/pdf], "Content-Encoding" => "gzip") | |
end |
2. In the view file, the form for the PDF request:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for(@price_letter, :action => resource(:ordering, @price_letter, :priceletter, :format => "pdf"), :class => "form") do %> | |
# fields for .... | |
<% end =%> |
Please notice the :format => 'pdf'
3. In controller, provides the pdf format:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def priceletter | |
provides :pdf | |
... | |
end |
From here, you have two ways to send the PDF file:
In the controller/action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pdf = Prawn::Document.new( | |
:page_size => "A4", | |
:left_margin => 2.cm, | |
:right_margin => 2.cm, | |
:top_margin => 2.cm, | |
:bottom_margin => 1.5.cm | |
) | |
# logo | |
logo = image_dir + 'logo.jpg' | |
pdf.image logo, :position => :center | |
... | |
send_data pdf.render | |
Or, in a pdf.erb file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# price_letter.pdf.erb | |
<%= | |
require "prawn/measurement_extensions" | |
require "prawn/table" | |
pdf = Prawn::Document.new( | |
:page_size => "A4", | |
:left_margin => 2.cm, | |
:right_margin => 2.cm, | |
:top_margin => 2.cm, | |
:bottom_margin => 1.5.cm | |
) | |
# logo | |
logo = image_dir + 'logo.jpg' | |
pdf.image logo, :position => :center | |
pdf.font "Times-Roman" | |
... | |
pdf.render | |
%> |
Now, you should be able to get a PDF file.
UPDATE(Feb-23-2009):
Someone asked what if you using 'GET' instead of post?
Well, it is pretty simple, you can just add the format like:
resource(:ordering, @price_letter, :print, :format => :pdf)
I am hoping this is helpful.
3 comments:
Thanks for this post, feel free to link it on the Prawn mailing list.
But... have you tried Prawnto?
http://www.cracklabs.com/prawnto
-greg
Oh hmm... I'm sorry it must have been late (or early) when I read this. Prawnto isn't going to work with Merb, but it'd be cool if it did!
-greg
Thanks for your comments, Greg. I will look at Prawnto to see if I can make it working with Merb.
Post a Comment