Undefined method `pdf` with prawn-rails - ruby-on-rails

Trying to set up a basic PDF template with prawn-rails, and something's not quite right.
Gemfile:
gem 'prawn-rails'
which does grab the latest version (1.0.0).
Controller:
def show
respond_to do |format|
format.pdf { render pdf: 'show.pdf.prawn' }
# ...
end
end
HAML view:
= link_to object_url(object, format: :pdf) do
%button.btn.btn.default Print
show.pdf.prawn:
prawn_document do
pdf.text "Some text"
end
config/initializers/prawn-rails.rb:
require 'prawn-rails'
PrawnRails.config do |config|
config.page_layout = :portrait
config.page_size = 'LETTER'
config.skip_page_creation = true
end
This is all according to the README of the prawn-rails repository.
But I get ActionView::Template::Error (undefined local variable or method 'pdf' for #<#<Class:0x005618ff5021e8>:0x005618ff97b968>).
Any thoughts? Should I just report this as an issue on the repo?

Should be:
prawn_document do |pdf|
pdf.text "Some text"
end
I'd send them a note and tell them to update their README.

Related

How can I show a new file/webpage using ActiveJob in Rails 5?

I used to generate PDFs for my users by using the wicked_pdf gem and writing
something like the example code below:
class ThingsController < ApplicationController
def show
respond_to do |format|
format.html
format.pdf do
render pdf: "file_name" # Excluding ".pdf" extension.
end
end
end
end
I would then have a link on a page like <%= link_to report_pdf_path(#report), "Download PDF", target: "_blank" %> that would cause the PDF to show up in the user's browser as a new tab. This is the preferred behavior I would like, but it halts all requests until the PDF is completed for the user and some of these take quite some time to generate.
So I've since offloaded the generating of the PDF to ActiveJob which works nice, but I can't figure out how to have ActiveJob open the file in a new window yet. Currently, I have it writing to the server and then updating a partial that shows the files the user has requested. Below is an example of it.
class GeneratePdfJob < ApplicationJob
queue_as :default
def perform(*args)
params = args.first
generate_pdf_document(params)
end
def generate_pdf_document(params)
html = ApplicationController.new.render_to_string(
template: 'players/board_labels.pdf.erb',
locals: { player_ids: params[:player_ids] }
)
save_to_pdf(html, params[:pdf_title], params[:user_code])
end
def save_to_pdf(html, pdf_title, user_code)
pdf = WickedPdf.new.pdf_from_string(
html,
pdf: "#{pdf_title}",
layout: 'print',
encoding: 'utf-8'
)
pdf_name = "#{pdf_title}.pdf"
pdf_dir = Rails.root.join('public','uploads','reports',"#{user_code}")
pdf_path = Rails.root.join(pdf_dir,pdf_name)
# create the folder if it doesn't exist
FileUtils.mkdir_p(pdf_dir) unless File.directory?(pdf_dir)
# create a new file
File.open(pdf_path,'wb') do |file|
file.binmode
file << pdf.force_encoding("UTF-8")
end
end
end
So how can I make a method that would replace the save_to_pdf method and instead open the file in a new tab for the user? If this isn't possible could I open a tab when I initiate the ActiveJob that is a placeholder tab and then incorporate ActionCable to display the file some how after it's been generated for the user?

RoR HTML template to .docx

I need to create a .docx file from a HTML template, so I used htmltoword gem.
Usage:
I added the gem (Gemfile):
gem 'htmltoword', '~> 0.5.1' #last version of the gem
I put a route (route.rb):
get 'preview' => 'foo#preview'
And in my bar.html.erb I have a link which target's that url:
<%= link_to '.docx', preview_path %>
Template (preview.docx.erb):
<h1>foobar</h1>
And in the controller (foos_controller.rb):
class FoosController < ApplicationController
respond_to :docx
#other code
def preview
respond_to do |format|
format.docx do
render docx: 'foobar', filename: 'preview.docx'
end
end
end
end
However, I'm getting an error:
ActionController::UnknownFormat
How to fix this error?
My config:
RoR v4.2.4
Ruby v2.2.3p173
Also, there is an open github issue for this/similar topic.
Update: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem, however, I get the same error.
Since respond_to has been removed from rails 4.2 to a individual gem i will recommend you to use formatter gem..
For further details you can look to the link given below.
Why is respond_with being removed from rails 4.2 into it's own gem?
Have you tried caracal-rails? You can find it here
I had to build this same functionality in an app earlier this year and also used the htmltoword gem.
# At the top of the controller:
respond_to :html, :js, :docx
def download
format.docx {
filename: "#{dynamically_generated_filename}",
word_template: 'name_of_my_word_template.docx')
}
end
I then have two "view" files that come into play. The first, is my method view file download.docx.haml. This file contains the following code:
%html
%head
%title Title
%body
%h1 A Cool Heading
%h2 A Cooler Heading
= render partial: 'name_of_my_word_template', locals: { local_var: #local_var }
From there, I have another file name_of_my_word_template.docx.haml that contains the meat of my Word file.
%h4 Header
%h5 Subheader
%div= local_var.method
%div Some other content
%div More content
%div Some footer content
When someone hits my_app.com/controller_name/download.docx, a Word file is generated and downloaded for them.
In order to ensure this happens, I have a route for the download method in my routes.rb file:
resources :model_name do
member do
get :download
end
end
Apologies for the long reply ... this has worked well for me and I hope helps you through this issue!
So, I figured it out. I added format: 'docx' to the route and it works now.
Note: as #kajalojha mentioned, respond_with / Class-Level respond_to has been removed to an individual gem, so I installed the responders gem.
Let's create a download logic.
Gemfile
gem 'responders'
gem 'htmltoword', '~> 0.5.1'
routes.rb
get 'download' => 'foos#download', format: 'docx' #added format
foos_controller.rb
class FoosController < ApplicationController
respond_to :docx
def download
#bar = "Lorem Ipsum"
respond_to do |format|
format.docx do
# docx - the docx template that you'll use
# filename - the name of the created docx file
render docx: 'download', filename: 'bar.docx'
end
end
end
end
download.docx.erb
<p><%= #bar %></p>
And I've added some link to trigger the download logic:
<%= link_to 'Download bar.docx', foo_download_path %>
Which will download the bar.docx file with "Lorem Ipsum" in it.

"Error: PDF could not be generated!" with WickedPDF

Error:
RuntimeError in BillsController#printing
Failed to execute:
"/usr/local/bin/wkhtmltopdf" -q "file:////var/folders/j5/2wx0qdjj7kl7vbvq3m2z4rj00000gn/T/wicked_pdf20130213-41259-x9dcb5.html" "/var/folders/j5/2wx0qdjj7kl7vbvq3m2z4rj00000gn/T/wicked_pdf_generated_file20130213-41259-mg4iqp.pdf"
Error: PDF could not be generated!
BillsController:
# GET /bills
# GET /bills.json
def print
respond_to do |format|
format.html
format.pdf do
render :pdf => "rechnung_id",
:layout => "printing.pdf",
:show_as_html => params[:debug]
end
end
end
as views I created a printing.html.erb and a printing.pdf.erb - and tried both.
I've installed wkhtmltopdf as binary and as gem. When I try to use the gem (commenting out the line:
WickedPdf.config = { :exe_path => '/usr/local/bin/wkhtmltopdf'}
something seems to crash and nothing happens..
When I use the binary, I get the error displayed on top.
The versions of my gems are:
wicked_pdf (0.9.4)
and wkhtmltopdf-binary (0.9.9.1).
I was searching for help - that's what I already tried:
"bundle update" and "bundle install"
installed wkhtmltopdf in version 9.9
added "Mime::Type.register "application/pdf", :pdf"
EDIT:
If I use the terminal and enter "wkhtmltopdf www.myHomepage.info myhomepage.pdf" it works fine. "which wkhtmltopdf" gives me the path "/usr/bin/wkhtmltopdf", but if I want to use with one, it's opening "wkhtmltopdf_darwin_386" and the website freezes..
now i solved the problem.
I changed my controller method to:
def printing
#bills = Bill.find(params[:id])
respond_to do |format|
format.html
format.pdf do
render :pdf => "bill_#{#bills.id}",
:wkhtmltopdf => '/usr/bin/wkhtmltopdf',
:template => '/bills/printing.pdf.erb',
:disposition => "inline"
#:save_to_file => Rails.root.join('pdf', "rechnung_#{#bills.id}.pdf")
end
end
end
and i had to remove WickedPDF as middleware in the application.rb:
require 'wicked_pdf'
config.middleware.use WickedPdf::Middleware, {}
now it's working as expected.

Rails 3 Render Prawn pdf in ActionMailer

How to render prawn pdf as attachment in ActionMailer? I use delayed_job and don't understand, how could I render pdf-file in action mailer (not in controller). What format should I use?
You just need to tell Prawn to render the PDF to a string, and then add that as an attachment to the email. See the ActionMailer docs for details on attachments.
Here's an example:
class ReportPdf
def initialize(report)
#report = report
end
def render
doc = Prawn::Document.new
# Draw some stuff...
doc.draw_text #report.title, :at => [100, 100], :size => 32
# Return the PDF, rendered to a string
doc.render
end
end
class MyPdfMailer < ActionMailer::Base
def report(report_id, recipient_email)
report = Report.find(report_id)
report_pdf_view = ReportPdf.new(report)
report_pdf_content = report_pdf_view.render()
attachments['report.pdf'] = {
mime_type: 'application/pdf',
content: report_pdf_content
}
mail(:to => recipient_email, :subject => "Your report is attached")
end
end
I followed the RailsCasts for PRAWN. Taken what has already been said and what I was trying to similarly accomplish, I set the attachment name and then created the PDF.
InvoiceMailer:
def invoice_email(invoice)
#invoice = invoice
#user = #invoice.user
attachments["#{#invoice.id}.pdf"] = InvoicePdf.new(#invoice, view_context).render
mail(:to => #invoice.user.email,
:subject => "Invoice # #{#invoice.id}")
end
My solution:
render_to_string('invoices/show.pdf', :type => :prawn)
PDF was corrupted because I didn't write block for mail function and multi-part email was incorrect.

How do I generate pdf file using Prawn in Rails?

I have used the following syntax to generate a pdf file:
Prawn::Document.generate("#{RAILS_ROOT}/public/pdf/generate.pdf") do
pdf.text "hello"
end
But when I look for the file within the /public/pdf/ folder, I dont find any file.
Well my controller code for this is
def generate
respond_to do |format|
format.pdf{ render :file => "#{RAILS_ROOT}/public/pdf/generate.pdf"}
end
end
It could be something else, but the code supplied doesn't seem to work:
Prawn::Document.generate("x.pdf") do
pdf.text "Hello"
end
gives me
NameError: undefined local variable or method `pdf' for #<Prawn::Document:0x352b6e4>
from c:/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.8.4/lib/prawn/graphics/color.rb:72:in `method_missing'
from (irb):3
You should be able to see if you're getting something similar in your log file.
I think the pdf. is not needed:
Prawn::Document.generate("x.pdf") do
text "Hello"
end
ruby - 1.9.3
rails - 3.2
way 1
create new project in rails
rails new prawn_sample
you can find gem file inside project folder, add prawn gem.
gem 'prawn'
then bundle install
now, install prawnto plugin
rails plugin install git#github.com:forrest/prawnto.git
For sample data to display, let’s create a Book model with title,author and description.
rails generate scaffold book title:string author:string description:text
then
rake db:migrate
now start the server and enter sample data's to test
so,
rails s
localhost:3000/books
here enter the required amount of data's
next
Let’s get started with something simple and add a pdf version of the show action. Open up the books controller and add format.pdf
def show
#book = Book.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => #book }
format.pdf { render :layout => false }
end
end
create the show.pdf.prawn file inside app/views/books. For now, lets have hello world
pdf.text "Hello World!"
visit
http://localhost:3000/books/1
http://localhost:3000/books/1.pdf
you successfully generated PDF.
Let’s make the view specific to the books.
in show.pdf.prawn
pdf.font "Helvetica"
pdf.text "Book: #{#book.title}", :size => 16, :style => :bold, :spacing => 4
pdf.text "Author: #{#book.author}", :spacing => 16
pdf.text #book.description
now you see some text with format specified above . Browse more for format you required.
way 2
Gemfile
gem 'prawn'
/config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
AuditsController
def show
#audit = Audit.find(params[:id])
respond_to do |format|
format.html
format.pdf do
pdf = Prawn::Document.new
pdf.text "This is an audit."
send_data pdf.render, type: "application/pdf", disposition: "inline"
end
end
end
Not sure what version of Prawn you're using, but this worked for me:
pdf = Prawn::Document.new(background: img, :page_size => "LETTER", :page_layout => :landscape)
pdf.render_file File.join(Rails.root, "app/pdfs", "x.pdf")
I think you're missing the pdf.render_file line that actually creates the file.

Resources