blank pdf on trying "hello prawn!"

404 views
Skip to first unread message

sc-kitten

unread,
Apr 26, 2011, 7:45:48 PM4/26/11
to Prawn
Hi
I just installed prawn, and somehow even the simpliest "Hello Prawn!"
statement does not get printed. Why am I getting an empty PDF?

Here is what I did:

1. Installed the gem
> sugo gem install prawn

2. Downloaded prawnto into my apps:
> script/plugin install git://github.com/thorny-sun/prawnto.git

3. Added a config gem line to environment.rb
> config.gem "prawn"

4. Added a route to the routes.rb file:

map.connect 'exams/:id/users/:user_id/certificate',
:conditions => {:method => :get},
:controller => "exams",
:action => "certificate"

5. Created "certificate.pdf.prawn" with this content:

require 'prawn'
Prawn::Document.generate('certificate.pdf', options={
:page_size => 'A4',
:page_layout => :landscape
}) do |pdf|

pdf.move_down 100
pdf.text "Hello Prawn!"
end

6. Added action within the controller:

class ExamsController < ApplicationController

def certificate
respond_to do |format|
format.pdf { render :layout => false }
# or for download
# format.pdf { prawnto :inline => true }
end
end

I don't see why PDF would be generated as blank page. I am able to
save this blank certificate.pdf, but why doesn't it put the text on
the screen???
I would appreciate any input on this.

Thanks so much
Elle




pascal

unread,
Apr 27, 2011, 5:11:02 AM4/27/11
to Prawn
Correct me if i'm wrong but i think prawnto is not the recommended way
to go.
It always scares me away from a plugin (actually plugins scare me
away) when last commit is from more than 2 years (https://github.com/
thorny-sun/prawnto/commits/master) and the prawnto homepage still
talks about rails 2.2 (looking at your code it looks like your still
using something older than rails 3?)

Have a look at: https://github.com/sandal/prawn/wiki/Using-Prawn-in-Rails.

Pascal

sc-kitten

unread,
Apr 27, 2011, 1:43:26 PM4/27/11
to Prawn
Yes, I am using Rails 2.3.8. This is the production server and if we
upgrade, a bunch of stuff stops working... We have to keep that
version of rails. I will try what u suggest in a bit...

Brad Ediger

unread,
Apr 27, 2011, 1:49:49 PM4/27/11
to prawn...@googlegroups.com
On Wed, Apr 27, 2011 at 12:43 PM, sc-kitten <kotyo...@gmail.com> wrote:
> Yes, I am using Rails 2.3.8. This is the production server and if we
> upgrade, a bunch of stuff stops working... We have to keep that
> version of rails. I will try what u suggest in a bit...

I'm pretty sure that your problem comes from trying to create a
document from within a Prawnto view. My understanding is that Prawnto
creates a document and pushes it into your view, so you only have to
write your actual document code. But I would definitely recommend
using the documentation that Pascal pointed out; it's the recommended
way to use Prawn from Rails.

Brad

sc-kitten

unread,
Apr 27, 2011, 2:09:53 PM4/27/11
to Prawn
Hi guys,
I am trying to follow that page and this command is not even
installing:

$ git submodule add git://github.com/sandal/prawn.git vendor/prawn
fatal: Not a git repository (or any of the parent directories): .git

Not sure if that document is up to date. Also, I noticed that the URL
that I was creating with routes, was ending up blank, but, I found
within the main folder generated certificate.pdf that renders what I
asked it to do... Very weird... How come the url I am loading is
blank, but something is saved separately at the root?

Brad Ediger

unread,
Apr 27, 2011, 2:12:47 PM4/27/11
to prawn...@googlegroups.com
On Wed, Apr 27, 2011 at 1:09 PM, sc-kitten <kotyo...@gmail.com> wrote:
> Hi guys,
> I am trying to follow that page and this command is not even
> installing:
>
> $ git submodule add git://github.com/sandal/prawn.git vendor/prawn
> fatal: Not a git repository (or any of the parent directories): .git

The instructions were written as if you're using git to manage your
project. If you're not, you'll just need to "git clone" Prawn into a
directory under your project, or use a gem version of Prawn.

> Not sure if that document is up to date. Also, I noticed that the URL
> that I was creating with routes, was ending up blank, but, I found
> within the main folder generated certificate.pdf that renders what I
> asked it to do... Very weird... How come the url I am loading is
> blank, but something is saved separately at the root?

As I mentioned earlier, Prawnto generates a Prawn::Document for you
and expects you to use it to render your data. But you are throwing it
away and creating a new Prawn::Document in your code, and saving it
off to the filesystem. So that document looks correct but the document
Prawnto gave you didn't have anything rendered to it.

-be

sc-kitten

unread,
Apr 27, 2011, 7:17:14 PM4/27/11
to Prawn
Okay, I followed exactly that doc, based on what I have, and managed
to get things rolling. Just in case if anyone needs a recap (newbies
like me), I will post the list of instructions here:

1. Load into vendor directory Prawn library
> git clone git://github.come/sandal/prawn.git vendor/prawn

2. Checkout stable release & load dependent submodules
> cd vendor/prawn
> git checkout -b stable
> git submodule init
> git submodule update

3. Install dependencies
> gem install pdf-reader

4. Add to config/environment.rb:
> config.load_paths << "#{RAILS_ROOT}/vendor/prawn/lib"

5. Create initializer file:
> touch config/initializers/prawn.rb
> chmod 644 config/initializers/prawn.rb
> vim config/initializers/prawn.rb
> require "prawn"

6. Test installation:
> script/console
> Prawn::BASEDIR
> "/Users/acuadmin/acutrainme/trunk/vendor/prawn"

7. Additional configuration, create pdf directory
> touch app/pdfs

8. Add pdfs directory to configuration:
> config.load_paths << "#{RAILS_ROOT}/app/pdfs"

9. Add PDF as mime extension in /config/inializers/mime_types.rb
> Mime::Type.register_alias "application/pdf", :pdf

10. Rendering PDFs. Create your pdf in pdfs directory:
> touch pdfs/exam_certificate.rb
> Add to_pdf method that renders it
> Create a route and controller to display / download a PDF

This is what I have in my pdfs directory:
pdfs/exam_certificate.rb

# test version
class ExamCertificate < Prawn::Document
def to_pdf(_params)
@session_token = _params[:session_token]
if (@session_token)
text "Session Token #{@session_token}"
else
text "No parameters were defined to render your PDF certificate."
end
render
end
end

And I added to the existing exam_sessions_controller.rb:

class ExamSessionsController < ApplicationController
def certificate
output = ExamCertificate.new.to_pdf(params)
respond_to do |format|
format.pdf do
send_data output, :filename => "certificate.pdf", :type =>
"application/pdf", :disposition => 'inline'
end
end
end
end

And lastly, my routes looks like this:

map.connect 'exam_sessions/:session_token/certificate',
:conditions => {:method => :get},
:controller => "exam_sessions",
:action => "certificate"

Okay, well, now will continue with the complexities... how to render
the content...

Thanks everybody!


Reply all
Reply to author
Forward
0 new messages