<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Chelsea.dev</title>
	<atom:link href="http://chelsearobb.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chelsearobb.wordpress.com</link>
	<description>Musings of a ruby on Rails dev</description>
	<lastBuildDate>Wed, 09 Sep 2009 14:51:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='chelsearobb.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Chelsea.dev</title>
		<link>http://chelsearobb.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chelsearobb.wordpress.com/osd.xml" title="Chelsea.dev" />
	<atom:link rel='hub' href='http://chelsearobb.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Saving a Prawn PDF to File</title>
		<link>http://chelsearobb.wordpress.com/2009/09/09/saving-a-prawn-pdf-to-file/</link>
		<comments>http://chelsearobb.wordpress.com/2009/09/09/saving-a-prawn-pdf-to-file/#comments</comments>
		<pubDate>Wed, 09 Sep 2009 14:33:59 +0000</pubDate>
		<dc:creator>chelsearobb</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[prawn]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chelsearobb.wordpress.com/?p=3</guid>
		<description><![CDATA[On a recent project I was generating a PDF using Prawn and feeding it out to the user via send_data.  Then requirements changed (of course), and we needed to show past invoices.  To improve response times I was asked to save the generated PDFs as they wouldn&#8217;t change and the storage of them is not [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chelsearobb.wordpress.com&amp;blog=9401002&amp;post=3&amp;subd=chelsearobb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>On a recent project I was generating a PDF using Prawn and feeding it out to the user via send_data.  Then requirements changed (of course), and we needed to show past invoices.  To improve response times I was asked to save the generated PDFs as they wouldn&#8217;t change and the storage of them is not an issue.</p>
<p>Unfortunately Prawn doesn&#8217;t seem to support this functionality on it&#8217;s own, and there weren&#8217;t too many guides for how to do this out there.</p>
<p>Fortunately, it turned out to be pretty easy to implement.  I set up a static method in my Invoices class that takes in teh desired file name, and the list of invoices:</p>
<p><pre class="brush: ruby;">

def self.create_pdf(invoices, filename)
   pdf = Prawn::Document.new
   eval(Invoice.get_pdf_markup(invoices))
   pdf.render_file(filename)

 end</pre></p>
<p>And another static method to get the PDF markup.  Mine&#8217;s pretty long and complicated since it&#8217;s a formatted invoice.  Just put what would normally be in your prawn.pdf file here.</p>
<p><pre class="brush: ruby;">

 def self.get_pdf_markup(invoices)
 return &lt;&lt;-EOS
 if invoices.blank?
 pdf.text &quot;There has been no account activity this month&quot;
 else
 point = [pdf.bounds.right-50, pdf.bounds.bottom + 648]
 page_counter = pdf.lazy_bounding_box(point, :width =&gt; 10) do
 pdf.text pdf.page_count, :size =&gt; 9, :align =&gt; :right
 end

 pdf.header [pdf.margin_box.top_left] do
 pdf.fill_color &quot;299f6f&quot;
 pdf.text &quot;STATEMENT&quot;, :align =&gt; :center, :size =&gt; 9

 pdf.bounding_box [300,670], :width=&gt;100 do
 pdf.text &quot;Customer No:&quot;, :size =&gt; 9
 pdf.text &quot;Date: &quot;, :size =&gt; 9
 pdf.text &quot;Page: &quot;, :size =&gt; 9
 end

 pdf.bounding_box [400, 670], :width=&gt; 100 do
 pdf.fill_color &quot;000000&quot;
 pdf.text invoices.first.invoice.accpac_customer.IDCUST.strip, :align =&gt; :right, :size =&gt; 9
 pdf.text Date.today.strftime(&quot;%m/%d/%Y&quot;), :align =&gt; :right, :size =&gt; 9
 end

 logo_header = &quot;#{RAILS_ROOT}/public/images/logo.png&quot;
 pdf.image logo_header, :width =&gt; 225, :at =&gt; [5, 680]

 pdf.bounding_box [50,600], :width =&gt; 200 do
 pdf.fill_color &quot;299f6f&quot;
 pdf.text &quot;Sold To:&quot;, :size =&gt; 9
 pdf.bounding_box [10, 0], :width =&gt; 190 do
 pdf.fill_color &quot;000000&quot;
 pdf.text invoices.first.invoice.accpac_customer.display_address.split(&quot;
&quot;)* &quot;\n&quot;, :size =&gt; 9
 pdf.text invoices.first.invoice.accpac_customer.TEXTPHON1, :size =&gt; 9
 end
 end

 pdf.bounding_box [300,600], :width =&gt; 200 do
 pdf.fill_color &quot;299f6f&quot;
 pdf.text &quot;Remit To Address:&quot;, :size =&gt; 9

 pdf.bounding_box [10, 0], :width =&gt; 190 do
 pdf.fill_color &quot;000000&quot;
 pdf.text REMIT_TO_ADDRESS * &quot;\n&quot;, :size =&gt; 9
 end
 end
 page_counter.draw
 end

 data = invoices.collect{|arobl| [arobl.IDINVC, Date.strptime(arobl.DATEINVC.to_s, '%Y%m%d').strftime('%Y/%m/%d'),
 arobl.TRXTYPETXT, &quot;&quot;, Date.strptime(arobl.DATEDUE.to_s, '%Y%m%d').strftime('%Y/%m/%d'), sprintf(&quot;%0.2f&quot;, arobl.AMTDUETC) ]}
 pdf.fill_color &quot;000000&quot;
 pdf.bounding_box [0,500], :width =&gt; 500 do
 pdf.table data,
 :position =&gt; :center,
 :header_text_color =&gt; &quot;299f6f&quot;,
 :border_color =&gt; &quot;299f6f&quot;,
 :headers =&gt; [&quot;Document No.&quot;, &quot;Doc. Date&quot;, &quot;Ty.&quot;, &quot;Reference/Applied No.&quot;, &quot;Due Date&quot;, &quot;Amount&quot; ],
 :font_size =&gt; 9,
 :width =&gt; 500,
 :vertical_padding =&gt; 1
 end

 end
 EOS
 end

</pre></p>
<p>And to return that from your controller all you have to do is a send_data (or send_file, I opted for send_data) call with the file:</p>
<p><pre class="brush: ruby;">

 send_data(File.read(filename), :type =&gt; &quot;application/pdf&quot;,
 :filename =&gt; &quot;#{Date::MONTHNAMES[params[:month]]}#{params[:year]}.pdf&quot;)

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chelsearobb.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chelsearobb.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chelsearobb.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chelsearobb.wordpress.com&amp;blog=9401002&amp;post=3&amp;subd=chelsearobb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chelsearobb.wordpress.com/2009/09/09/saving-a-prawn-pdf-to-file/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/b3638e85319f6fbf08001c3beaaa319b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">chelsearobb</media:title>
		</media:content>
	</item>
	</channel>
</rss>
