Created
April 15, 2015 18:01
-
-
Save alexdunae/63b3f7de5cd13c264c92 to your computer and use it in GitHub Desktop.
prawn-for-mike
This file contains hidden or 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
# in your Gemfile | |
gem 'prawn' | |
gem 'prawn-table' | |
# then run bundle install |
This file contains hidden or 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
# in app/pdfs/order_pdf.rb | |
require 'prawn/measurement_extensions' | |
class OrderPDF < Prawn::Document | |
attr_reader :order | |
def initialize(order) | |
@order = order | |
super( | |
skip_page_creation: true, | |
page_size: [8.5.in, 11.in], | |
layout: :portrait, | |
top_margin: 0.5.in, | |
bottom_margin: 0.5.in, | |
left_margin: 0.5.in, | |
right_margin: 0.5.in, | |
) | |
end | |
def generate(render_to = nil) | |
start_new_page | |
# prawn code goes here | |
text "Order \##{order.id}", style: :bold, size: 12 | |
move_down 2 | |
text 'Some other text', style: :bold, size: 12 | |
end | |
end |
This file contains hidden or 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
# in your controller | |
class OrdersController < ApplicationController | |
# ... other action methods | |
def show | |
@order = Order.find(params[:id]) | |
respond_to do |format| | |
format.html | |
format.pdf do | |
pdf = OrderPDF.new(@order) | |
send_data pdf.generate, filename: 'order.pdf', type: 'application/pdf', disposition: 'attachment' | |
end | |
end | |
end | |
# ... other action methods | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment