Created
September 21, 2009 06:24
-
-
Save benschwarz/190100 to your computer and use it in GitHub Desktop.
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 few things left to do: | |
* Ability to add default options for each PDF, rather than having to pass it to each generate_* method call | |
* Doesn't check if the pdf already exists. By default, which should it do? | |
- Overwrite the existing file. Let your users decide what they'd like to do here. | |
* Consider the inclusion of css, or even generated css templates using pre existing libraries (lesscss or yerk – sass.) | |
Anything else? |
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
class Notifier < ActionMailer::Base | |
def terms_of_service | |
PdfGenerator.generate_terms_of_service('tos.pdf', :page_layout => :portrait) # or landscape | |
# attach pdf to email | |
end | |
end | |
Notifier.deliver_terms_of_service |
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
class PdfGeneration | |
def self.method_missing(method_symbol, *parameters, &block) | |
if method_symbol.to_s =~ /^generate_(\w+)$/ | |
filename, options = *parameters | |
raise "ERROR: PdfGeneration requires a filename string be set as the first parameter" unless filename && filename.is_a?(String) | |
new_pdf = new((options || {})) | |
new_pdf.send($1) | |
new_pdf.save(filename) | |
else | |
super | |
end | |
end | |
def initialize(options = {}) | |
@options = options | |
@pdf = Prawn::Document.new(@options) | |
end | |
def method_missing(method_symbol, *parameters, &block) | |
if @pdf.respond_to?(method_symbol) | |
@pdf.send(method_symbol, *parameters, &block) | |
else | |
super | |
end | |
end | |
def save(filename) | |
@pdf.render_file(filename) | |
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 PDF generation class that acts like action mailer in that you can call | |
# generate_* but drop generate_ on the method definition | |
class PdfGenerator < PdfGeneration | |
def terms_of_service | |
text "Terms Of Service" | |
# any other prawn method can be called here and be called on the pdf object behind the scenes | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment