Created
April 24, 2014 16:00
-
-
Save illbzo1/11259869 to your computer and use it in GitHub Desktop.
A step by step Ruby on Rails implementation for DocRaptor
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
<style type="text/css"> | |
/* setup the page */ | |
@page { | |
size: US-Letter; | |
margin: 0 0 35mm 0; | |
background: #ffffff; | |
} | |
/* setup the footer */ | |
@page { | |
@bottom { | |
content: flow(footer); | |
} | |
} | |
footer { | |
flow: static(footer); | |
} | |
body { | |
border-top: 10px solid #3877B1; | |
font-family: "myriad-pro-1", "myriad-pro-2", sans-serif; | |
} | |
#container { | |
margin: 0 auto; | |
} | |
header, #main { | |
margin: 15mm; | |
} | |
table { | |
width: 100%; | |
} | |
th { | |
font-size: 8pt; | |
color: #919191; | |
line-height: 14pt; | |
text-align: left; | |
padding-left: 10pt; | |
border-bottom: 1px solid #D6D6D6; | |
} | |
th:last-child, td:last-child { | |
width: 20%; | |
} | |
th:last-child { | |
padding-left: 10mm; | |
} | |
td { | |
padding: 10pt; | |
border-bottom: 1px solid #D6D6D6; | |
} | |
td h4 { | |
font-weight: bold; | |
font-size: 12pt; | |
} | |
tr:nth-child(odd) td{ | |
background: #F1F1F1; | |
} | |
</style> | |
<body> | |
<div id="container"> | |
<header> | |
<h1> Random Title </h1> | |
</header> | |
</div> | |
<table> | |
<thead> | |
<tr> | |
<th>End of year</th> | |
<th>Age</th> | |
<th>Balance</th> | |
<th>Withdrawal</th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @withdrawal_bad_timing.each_with_index do | record, values | %> | |
<tr> | |
<% if record[:current_balance] > 0 %> | |
<td><%= record[:year] %> </td> | |
<% else %> | |
<td> <%= record[:year] %> </td> | |
<% end %> | |
<% if record[:current_balance] > 0 %> | |
<td><%= record[:age] %> </td> | |
<% else %> | |
<td><%= record[:age] %> </td> | |
<% end %> | |
<% if record[:current_balance] > 0 %> | |
<td><%= number_to_currency(record[:current_balance]) %> </td> | |
<% else %> | |
<td><%= number_to_currency(record[:current_balance]) %> </td> | |
<% end %> | |
<% if record[:current_balance] > 0 %> | |
<td><%= number_to_currency(record[:withdrawal]) %> </td> | |
<% else %> | |
<td><%= number_to_currency(record[:withdrawal]) %> </td> | |
<% end %> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
</body> |
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
require 'rubygems' | |
require 'doc_raptor' | |
DocRaptor.api_key "YOUR_KEY_HERE" |
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
def show | |
@random = Random.find(params[:id]) | |
respond_to do |format| | |
format.html | |
format.pdf { doc_raptor_send } | |
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
<%= show_for @random do |s| %> | |
<%= s.attribute :title %> | |
<% end %> | |
<%= link_to 'Edit', edit_random_path(@random) %> | | |
<%= link_to 'Back', randoms_path %> | |
<%= link_to("Download Random PDF", random_path(@random, format: :pdf)) %> |
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
def show | |
@random = Random.find(params[:id]) | |
respond_to do |format| | |
format.html | |
format.pdf { doc_raptor_send } | |
end | |
end | |
def doc_raptor_send(options = { }) | |
default_options = { | |
:name => controller_name, | |
:document_type => request.format, | |
:test => ! Rails.env.production? | |
} | |
options = default_options.merge(options) | |
options[:document_content] ||= render_to_string | |
ext = options[:document_type] | |
response = DocRaptor.create(options) | |
if response.code == 200 | |
send_data response, :filename => "#{options[:name]}.#{ext}", :type => ext | |
else | |
render :inline => response.body, :status => response.code | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment