Created
April 22, 2012 06:52
-
-
Save ktkaushik/2462133 to your computer and use it in GitHub Desktop.
Using Prawn in your Rails App to download and attach the pdf in your email
This file contains 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
# This file exists in your initializers folder | |
# Insert this line | |
Mime::Type.register "application/pdf", :pdf |
This file contains 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
# Create a folder named in the app/pdfs and create this file. | |
class PdfGenerator < Prawn::Document | |
def initialize | |
# Default state of the pdf would state to this code | |
super | |
# Uncomment the below line if you wish for some custom rearrangements in the default state. like for ex | |
# super(top_margin: 100) | |
end | |
# Now start defining methods and perform operations for your PDF. | |
def header | |
move_down 30 # This will move down the start of pdf by 30 pixels. | |
text "My Header" , :style => :bold , :size => 14 # obviously, the size and style can be customized. | |
text "#{Date.today}" , :style => :bold , :size => 5 # You can use Interpolation and rails as is, obviously. | |
end | |
def design_a_table( table_header , table_data ) | |
@header = table_header | |
@data = table_data | |
# I will try and show as many operations for a table as i can. | |
# 1st method to draw a table. | |
table [ [ @header ] , [ @data ] ] | |
# another method is : | |
# create a array object which has to be two dimensional no matter what. | |
data_to_draw_a_table = [ [ @header ] , [ @data ] ] | |
table( data_to_draw_a_table ) | |
# Some beautiful customization you can do before you call table to draw a table is | |
data.each do |data| | |
table(data) do | |
column(0).background_color = "b7b5b0" | |
column(0).width = 120 | |
column(1).width = 180 | |
end | |
end | |
end | |
# I used this method to draw a table as well. Here you can see how you can set width of a particular column. Dont worry the Height would be set automatically. | |
# In order to DRY my code , i have a defined a method for width_of_table_columns. | |
def tasks_table( tasks ) | |
move_down 30 | |
@tasks = tasks | |
table [["#" , "Title" , "Rate" , "Hours" , "Cost" ]] , :column_widths => width_of_table_columns , :row_colors => ["b7b5b0"] | |
@tasks.each_with_index do | task , index | | |
table [[index + 1 , task.title , task.price_per_hour , task.hours_taken_to_complete_task , task.cost_by_hour_basis]] , :column_widths => width_of_table_columns | |
end | |
def width_of_table_columns | |
width = { 0 => 35 , 1 => 300 , 2=> 40 , 3 => 39 , 4 => 60 } | |
end | |
end |
This file contains 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
# This file is a library and thus it would be in lib/ folder of your rails app | |
module PdfGenerator | |
extends ActiveSupport::Concern | |
module ClassMethods | |
end | |
module InstanceMethods | |
def pdf_generator( download = nil ) | |
pdf = InvoicePdf.new | |
pdf.header | |
pdf.design_a_table( "header" , "corresponding_data") | |
pdf.tasks_table( "tasks" ) | |
# To download this pdf, write this down | |
# Basically send_data would download the file in obviously a pdf format. | |
if download | |
send_data pdf.render , :filename => file_name_here , :type => "application/pdf" | |
else | |
pdf.render , :filename => file_name_here , :type => "application/pdf" | |
end | |
end | |
end | |
end |
This file contains 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
require 'pdf_generator' | |
class TasksController < ApplicationController | |
include PdfGenerator | |
def some_action | |
pdf_generator( download = true ) | |
end | |
end |
This file contains 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
# A sample code of how to send the generated pdf as an attachment to your email. | |
require "pdf_generator" | |
class InvoiceMailer < ActionMailer::Base | |
include PdfGenerator | |
def your_mailer_method | |
# mail.attachments api is the new way to do it since Rails 3.1, i guess. | |
mail.attachments["invoice.pdf"] = {:mime_type => "application/pdf" , :content => pdf_generator } | |
# mailing configurations | |
mail( | |
:to => "to_email@abc.com", | |
:from => "from_email@abc.com", | |
:subject => "the pdf has been attached with this email" | |
) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for taking the time man.