Topic: Generating PDF

Hello,
Which is a good tutorial to generate a PDF with Rails 3.0.9?
I found a lot of tutorials but they are all out of date or incomplete.

I would like to have another view to display for the "show" action, somenthing like:

format.pdf { :layout=>'pdf_layout' }

Thanks to all

Re: Generating PDF

If you want to construct pdf's from scratch,  you can try

http://www.cracklabs.com/prawnto

Even though the website says only tested on 2.2, it apparently works on 3

If you want to build pdf's in some other tool, and do things like form fills,  assemble pdf's from component pdf's, etc. within Rails,  you can try pdftk:

http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/

I use pdftk to do form fills on pdf's I create with Illustrator / Acrobat Pro

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

prawnto use a prawn, a very powerfull tool, maybe it is quite hard...
What about a "wkhtmltopdf" based solution? Is it reccomandable?

However I'll spend some time to study prawnto.

For the second point, how can I set up a link_to that make the "show" action respond with the format.pdf  instead of the frmat.html?

Thank you.

Re: Generating PDF

So long as your route has the optional (:.format) tacked on to the end

Then if you request the action like:

www.yourapp.com/foo/bar.pdf

You'll call the bar action in foo_controller and the

def bar
  respond_to do |format|
    format.pdf {:render #whatever}
  end
end

will work

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

I tryed to use prawnto with rails 3.0.9.

I'm using this videocast as tutorial: http://railscasts.com/episodes/153-pdfs-with-prawn
I installed prawn:

man-10-21-2-67:gesba user$ bundle list
Gems included by the bundle:
[...]
  * prawn (0.12.0)
[...]

and I installed the prawnto plugin:

man-10-21-2-67:gesba user$ rails plugin install --force git://github.com/prior/prawnto.git 
Initialized empty Git repository in /Users/user/Sites/gesba/vendor/plugins/prawnto/.git/
remote: Counting objects: 29, done.
remote: Compressing objects: 100% (25/25), done.
remote: Total 29 (delta 3), reused 19 (delta 1)
Unpacking objects: 100% (29/29), done.
From git://github.com/prior/prawnto
 * branch            HEAD       -> FETCH_HEAD

I created the show.pdf.prawn file with

pdf.text "Hello World"

But I get a "file not found" error when I try to point my browser to http://localhost:3000/pay_checks/1.pdf

any suggestion?

Re: Generating PDF

In the video tutorial, the format.pdf in the controller is missing

Re: Generating PDF

BradHodges wrote:

So long as your route has the optional (:.format) tacked on to the end
Then if you request the action like:
www.yourapp.com/foo/bar.pdf
will work

How can I set up a link_to to respond with the format.pdf

Re: Generating PDF

link_to 'Get PDF' , ???_path(:format=>'pdf')

just pass :format=>'pdf' to whatever url_helper you are using

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

I'm using this code:

<%= link_to 'Generate PDF', pay_check(:format => 'pdf'), :class => 'btn grey small' %>

And I also added this route:

get '/pay_checks/:id(.:format)' => 'pay_checks#show'

But I get a NoMethodError error:
NoMethodError in Pay_checks#index
[...]
undefined method `pay_check' for #<#<Class:0x10f96fd10>:0x10f96dc68>

I would like to get this link: http://localhost:3000/pay_checks/6.pdf

Re: Generating PDF

<%= link_to 'Generate PDF', pay_check_path(:id=>1, :format => 'pdf'), :class => 'btn grey small' %>

That should get you the show action in your pay_check_controller, passing id = 1.

run

rake routes

and post what you get

You should notice that the route you added is redundant

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

This is the result of the rake routes:

DEPRECATION WARNING: Rake tasks in /Users/user/Sites/RoR/gesba/vendor/plugins/prawnto/tasks/prawnto_tasks.rake are deprecated. Use lib/tasks instead. (called from /Users/user/Sites/RoR/gesba/Rakefile:9)
          affords GET    /affords(.:format)                      {:controller=>"affords", :action=>"index"}
                  POST   /affords(.:format)                      {:controller=>"affords", :action=>"create"}
       new_afford GET    /affords/new(.:format)                  {:controller=>"affords", :action=>"new"}
      edit_afford GET    /affords/:id/edit(.:format)             {:controller=>"affords", :action=>"edit"}
           afford GET    /affords/:id(.:format)                  {:controller=>"affords", :action=>"show"}
                  PUT    /affords/:id(.:format)                  {:controller=>"affords", :action=>"update"}
                  DELETE /affords/:id(.:format)                  {:controller=>"affords", :action=>"destroy"}
       pay_checks GET    /pay_checks(.:format)                   {:controller=>"pay_checks", :action=>"index"}
                  POST   /pay_checks(.:format)                   {:controller=>"pay_checks", :action=>"create"}
    new_pay_check GET    /pay_checks/new(.:format)               {:controller=>"pay_checks", :action=>"new"}
   edit_pay_check GET    /pay_checks/:id/edit(.:format)          {:controller=>"pay_checks", :action=>"edit"}
        pay_check GET    /pay_checks/:id(.:format)               {:controller=>"pay_checks", :action=>"show"}
                  PUT    /pay_checks/:id(.:format)               {:controller=>"pay_checks", :action=>"update"}
                  DELETE /pay_checks/:id(.:format)               {:controller=>"pay_checks", :action=>"destroy"}
                  GET    /pay_checks/:id(.:format)               {:controller=>"pay_checks", :action=>"show"}
   work_addresses GET    /work_addresses(.:format)               {:controller=>"work_addresses", :action=>"index"}
                  POST   /work_addresses(.:format)               {:controller=>"work_addresses", :action=>"create"}
 new_work_address GET    /work_addresses/new(.:format)           {:controller=>"work_addresses", :action=>"new"}
edit_work_address GET    /work_addresses/:id/edit(.:format)      {:controller=>"work_addresses", :action=>"edit"}
     work_address GET    /work_addresses/:id(.:format)           {:controller=>"work_addresses", :action=>"show"}
                  PUT    /work_addresses/:id(.:format)           {:controller=>"work_addresses", :action=>"update"}
                  DELETE /work_addresses/:id(.:format)           {:controller=>"work_addresses", :action=>"destroy"}
        contracts GET    /contracts(.:format)                    {:controller=>"contracts", :action=>"index"}
                  POST   /contracts(.:format)                    {:controller=>"contracts", :action=>"create"}
     new_contract GET    /contracts/new(.:format)                {:controller=>"contracts", :action=>"new"}
    edit_contract GET    /contracts/:id/edit(.:format)           {:controller=>"contracts", :action=>"edit"}
         contract GET    /contracts/:id(.:format)                {:controller=>"contracts", :action=>"show"}
                  PUT    /contracts/:id(.:format)                {:controller=>"contracts", :action=>"update"}
                  DELETE /contracts/:id(.:format)                {:controller=>"contracts", :action=>"destroy"}
        referents GET    /referents(.:format)                    {:controller=>"referents", :action=>"index"}
                  POST   /referents(.:format)                    {:controller=>"referents", :action=>"create"}
     new_referent GET    /referents/new(.:format)                {:controller=>"referents", :action=>"new"}
    edit_referent GET    /referents/:id/edit(.:format)           {:controller=>"referents", :action=>"edit"}
         referent GET    /referents/:id(.:format)                {:controller=>"referents", :action=>"show"}
                  PUT    /referents/:id(.:format)                {:controller=>"referents", :action=>"update"}
                  DELETE /referents/:id(.:format)                {:controller=>"referents", :action=>"destroy"}
        customers GET    /customers(.:format)                    {:controller=>"customers", :action=>"index"}
                  POST   /customers(.:format)                    {:controller=>"customers", :action=>"create"}
     new_customer GET    /customers/new(.:format)                {:controller=>"customers", :action=>"new"}
    edit_customer GET    /customers/:id/edit(.:format)           {:controller=>"customers", :action=>"edit"}
         customer GET    /customers/:id(.:format)                {:controller=>"customers", :action=>"show"}
                  PUT    /customers/:id(.:format)                {:controller=>"customers", :action=>"update"}
                  DELETE /customers/:id(.:format)                {:controller=>"customers", :action=>"destroy"}
          workers GET    /workers(.:format)                      {:controller=>"workers", :action=>"index"}
                  POST   /workers(.:format)                      {:controller=>"workers", :action=>"create"}
       new_worker GET    /workers/new(.:format)                  {:controller=>"workers", :action=>"new"}
      edit_worker GET    /workers/:id/edit(.:format)             {:controller=>"workers", :action=>"edit"}
           worker GET    /workers/:id(.:format)                  {:controller=>"workers", :action=>"show"}
                  PUT    /workers/:id(.:format)                  {:controller=>"workers", :action=>"update"}
                  DELETE /workers/:id(.:format)                  {:controller=>"workers", :action=>"destroy"}
            users GET    /users(.:format)                        {:controller=>"users", :action=>"index"}
                  POST   /users(.:format)                        {:controller=>"users", :action=>"create"}
         new_user GET    /users/new(.:format)                    {:controller=>"users", :action=>"new"}
        edit_user GET    /users/:id/edit(.:format)               {:controller=>"users", :action=>"edit"}
             user GET    /users/:id(.:format)                    {:controller=>"users", :action=>"show"}
                  PUT    /users/:id(.:format)                    {:controller=>"users", :action=>"update"}
                  DELETE /users/:id(.:format)                    {:controller=>"users", :action=>"destroy"}
            start GET    /start(.:format)                        {:controller=>"start", :action=>"index"}
                  POST   /start(.:format)                        {:controller=>"start", :action=>"index"}
            login GET    /login(.:format)                        {:controller=>"sessions", :action=>"new"}
                  POST   /login(.:format)                        {:controller=>"sessions", :action=>"create"}
           logout DELETE /logout(.:format)                       {:controller=>"sessions", :action=>"destroy"}
                         /logout(.:format)                       {:controller=>"sessions", :action=>"destroy"}
  contracts_close GET    /contracts/close/:contract_id(.:format) {:controller=>"contracts", :action=>"close"}
                  POST   /contracts/close/:contract_id(.:format) {:controller=>"contracts", :action=>"close_submitted"}
            start        /(.:format)                             {:controller=>"contracts", :action=>"index"}

Now I'm testing my app on the production hosting.
I'm using a "test" account and there is Rails 2.3.2 installed (when I'll purchase the hosting I'll be able to update it)
With Rails 2.3.2 I get a "Missing template" error when I generate the pdf:

Missing template pay_checks/show with {:locale=>[:en, :en], :formats=>[:pdf], :handlers=>[:builder, :erb, :rjs, :rhtml, :rxml]} in view paths "/accounts/user/www/test/app/views"

Since I'm using prawnto I'm using the show.pdf.prawn template which don't appera in the handlers list.
This problem will be fixed using Rails 3.x?

Thanks for your help

Last edited by nimbuz77 (2011-11-08 10:39:50)

Re: Generating PDF

Is the prawn gem on the production environment?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

Yes, also the same version (0.12.0)

user@remotehost:~/www/test$ bundle187 list prawn
/accounts/user/www/test/vendor/gems/ruby/1.8/gems/prawn-0.12.0

Re: Generating PDF

Check this:
https://github.com/sandal/prawn/wiki/Us … th-Bundler

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

I'm not using git to get prawn.
I'm using the bundle install to get all the gems I need, then I do the "bundle pack" then I move the app to the hosting ant there I do a bundle install again.

Last edited by nimbuz77 (2011-11-08 13:19:26)

Re: Generating PDF

Worth a try.

Here is what I see as the problem,  in the error you posted there is this:

:handlers=>[:builder, :erb, :rjs, :rhtml, :rxml]

There should be a 'pdf' in there,  which is (I THINK) normally done by the prawnto plugin

So I think prawn is probably installed OK,  but the prawnto plugin isn't installed correctly for 2.X.

You may find an answer here:

https://github.com/sandal/prawn/wiki/Us … n-in-Rails

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Generating PDF

I agree with you, the problem is that prawnto didn't add the :prawn to the handler list.

I didn't find any answer to my problem in your link, the problem is not prawn but prawnto.

First of all the link in the homepage of the prawnto project on github (https://github.com/thorny-sun/prawnto) isn't active I used this one: https://github.com/prior/prawnto and in the wiki I found this guide to install it with rails 2.2 (https://github.com/prior/prawnto/wiki/Use).

I added the line

config.gem “prawn”

to the enviroment.rb but I get an error when I restart the webserver:

config/environment.rb:7: undefined local variable or method `config' for main:Object

At the moment... I hope the problem will be solved with rails update to the 3.0.x ...

Re: Generating PDF

Joe got a job, on the day switch, at the Application Muffin Research Cooking area, arrogantly warming up the clean and sterile textile nose of a completely energized frosting anointment equipment.