<?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/"
	>

<channel>
	<title>Blog &#8211; Branden Silva</title>
	<atom:link href="http://brandensilva.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://brandensilva.com</link>
	<description>Web Designer &#38; Web Developer from Salt Lake City, Utah.</description>
	<lastBuildDate>Sun, 07 Feb 2016 02:17:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
	<item>
		<title>Create a PDF Invoice Using Prawn in Rails</title>
		<link>http://brandensilva.com/create-a-pdf-invoice-using-prawn-in-rails/</link>
		<pubDate>Wed, 05 Mar 2014 20:44:17 +0000</pubDate>
		<dc:creator><![CDATA[bsilva]]></dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://localhost/composer/bedrock/?p=309</guid>
		<description><![CDATA[<p>After coming to the realization that I was going to need to create PDFs on the fly for the rails application I’m working on for a client, I started my hunt for something that could get the job done quick&#8230; <a href="http://brandensilva.com/create-a-pdf-invoice-using-prawn-in-rails/">Continued</a></p>
<p>The post <a rel="nofollow" href="http://brandensilva.com/create-a-pdf-invoice-using-prawn-in-rails/">Create a PDF Invoice Using Prawn in Rails</a> appeared first on <a rel="nofollow" href="http://brandensilva.com">Branden Silva</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>After coming to the realization that I was going to need to create PDFs on the fly for the rails application I’m working on for a client, I started my hunt for something that could get the job done quick with minimal effort. The Prawn gem was highly recommended by other rails enthusiasts.</p>
<div class="alert alert-warning">This tutorial is old and may not work with newer versions of Rails 3+ and Prawn without some modifications. I&#8217;ve only tested this in a Rails 2.3.x environment, but I do plan to do an updated article in the future when I have time.</div>
<p>Perhaps the greatest gripe I had about Prawn on first encounter was the lack of being able to use CSS and HTML for easy styling, which is something I’m much more comfortable with from experience. But after realizing that there weren’t many candidates that allowed this type of styling format in PDFs, I decided to give Prawn a go anyhow.</p>
<p>I also found prawnto googling, which is a plugin used with Prawn to provide easy access to the prawn library and allows for a simple respond_to block slapped into your controller to provide you with the .pdf extension. This seemed perfect for what I needed so I figured I’d share my knowledge with others.</p>
<h2>Lets Get Started</h2>
<pre class="crayon-plain-tag"># Create a rails project
rails customer

# Change directories to our project
cd customer</pre>
<p>Let’s do some scaffolding to save some time. In this case we are going to create invoices for our customers.</p>
<pre class="crayon-plain-tag"># Scaffold an invoice for our customer
script/generate scaffold invoice customer_name:string item_name:string item_price:string item_qty:integer

# Migrate your database
rake db:migrate</pre>
<p>Launch your server like so:</p>
<pre class="crayon-plain-tag">script/server</pre>
<p>Time to create a new customer invoice that we can work with. Go to your http://localhost:3000/invoices page and click on new invoice and add some data; here is what I used:</p>
<p>Name: Branden Silva<br />
Item Name: Awesome Serif Fonts<br />
Item Price: 24.99<br />
Item Quantity: 2
</p>
<p>Once done, http://localhost:3000/invoice/1 should now be your first customer invoice. Awesome, now lets get prawn and prawnto installed.</p>
<h2>Getting Prawn and Prawnto Up and Running</h2>
<p>I’m using rails 2.3.5 for reference so your results may vary depending on what version you are running. First you’ll want to download the prawn gem and prawnto plugin into your project like so:</p>
<pre class="crayon-plain-tag"># Install Prawn Gem
sudo gem install prawn

# Install Prawnto Plugin
script/plugin install git://github.com/thorny-sun/prawnto.git</pre>
<p>
Note: If you end up getting a “uninitialized constant Mime::PDF” error further down the tutorial when launching the pdf then prawnto may have not installed correctly. I recommend you go download prawnto at the git repository here and create a folder prawnto under your vendor/plugins directory in your rails application and stash the files in there.
</p>
<p>Then your going to want to add prawn to your environment.rb file to avoid any dependency issues in the future:</p>
<pre class="crayon-plain-tag"># Add this line to your config/environment.rb file.
config.gem "prawn"</pre>
<p>
Then you are going to have to set up prawnto in your invoice controller. Here I’m going to fetch a customer invoice and display a pdf in my show action when a user adds a .pdf extension to the url. The part you’ll want to pay attention to and add is the format.pdf line.
</p>
<pre class="crayon-plain-tag"># invoices/show
def show
	@invoice = Invoice.find(params[:id])
	respond_to do |format|
		format.html # show.html.erb
		format.xml  { render :xml => @invoice }
		format.pdf { render :layout => false } # Add this line
	end
end</pre>
<p>Now your going to need to create a file to put your prawn code into. In this case I just create a blank show.pdf.prawn file under the http://localhost:3000/views/invoices folder.</p>
<h2>Now Its Time to Start Dishing Some Prawn</h2>
<p>Lets start writing some prawn code. You can access your PDF at any time by simply adding a .pdf extension to your individual customer invoices. So http://localhost:3000/invoices/1 is how you show your customer invoice information via the browser and http://localhost:3000/invoices/1.pdf would be how you access your customer invoice via PDF format. Of course it’s not going to do anything until we write some code so open up your show.pdf.prawn file.</p>
<p>I usually start off by adding an image to my pdfs to give them a nice spiffy look. Here is the awesome logo I’m going to use:</p>
<p>Now save your logo (or mine) to your public/images directory in your rails app. This way you can reference it in your show.pdf.prawn file. Here is what the code looks like in our show.pdf.prawn file to add this image:</p>
<pre class="crayon-plain-tag"># Assign the path to your file name first to a local variable.
logopath = "#{RAILS_ROOT}/public/images/awesomecompany.jpg"

# Displays the image in your PDF. Dimensions are optional.
pdf.image logopath, :width => 197, :height => 91</pre>
<p>We are simply creating a local variable called logopath and then displaying it with the pdf.image method off our pdf object. I’ve also set the width and height of the image here.</p>
<p>How about we add some text now to our PDF. First lets shift the page down 70 points, add some styling, and then we can access our lovely instance variables that come with our rails app to pull in dynamic data.</p>
<pre class="crayon-plain-tag">pdf.move_down 70

# Add the font style and size
pdf.font "Helvetica"
pdf.font_size 18
pdf.text_box "Invoice # #{ @invoice.id }", :align => :right

pdf.font_size 22
pdf.text "Thank you for your order, #{ @invoice.customer_name }.", :align => :center
pdf.move_down 20

pdf.font_size 14
pdf.text "Below you can find your order details. We hope you shop with Awesome Company again in the future. Now unleash those fonts like hell have no fury!", :align=> :center</pre>
<p>
Thats a big chuck of code so let me break it down for you. pdf.move_down shifts your cursor focus down the PDF document. This doesn’t apply to our pdf.text_box which is entirely different creature that doesn’t follow the normal pdf page flow (you can think of text_boxes like absolute divs in html). pdf.font allows you to use supported prawn fonts. I’m sure it can be extended but I haven’t researched it in depth because Helvetica fits my needs for this example and client project. pdf.font_size adjusts the size of your text.
</p>
<p>
So what would we you do if you needed a table? Simple enough, try this:
</p>
<pre class="crayon-plain-tag">invoiceinfo = [["Item Name", "#{ @invoice.item_name }"], ["Item Price", "#{ @invoice.item_price }"],["Item Quantity", "#{ @invoice.item_qty }"]]

pdf.move_down 30

pdf.table invoiceinfo,
	:border_style => :grid,
	:font_size => 11,
	:position => :center,
	:column_widths => { 0 => 150, 1 => 250},
	:align => { 0 => :right, 1 => :left, 2 => :right, 3 => :left},
	:row_colors => ["d2e3ed", "FFFFFF"]</pre>
<p>
Here we are creating a multi-dimensional array and assigning it values from our rails application. In this case our local variable invoiceinfo is storing our table information. You don’t have to put the information in it’s own variable but I find its easier to keep the data separate from the formatting of the table. #{@invoice.item_name} would pull our item name from our rails application and display it in our pdf, yeah, it’s that easy. pdf.table calls invoiceinfo and then assigns a few attributes to that table. Pretty awesome that it’s that clean and simple to style everything up with a few key value pairs. I’ve even added some row colors to make it easier to scan for the user.
</p>
<p>
However, a PDF wouldn’t be complete without allowing page numbers. Fairly straightforward to do in prawn as well, here is the code:
</p>
<pre class="crayon-plain-tag">pdf.font_size 14

pdf.bounding_box([pdf.bounds.right - 50,pdf.bounds.bottom], :width => 60, :height => 20) do
	count = pdf.page_count
	pdf.text "Page #{count}"
end</pre>
<p>
Here we shrink the pdf.font_size down so the page number won’t be enormous. We start to get into the pdf.bounding_box. You can basically define boxes inside your pdf. In this case we are telling the bounding_box to find the right most edge (bounds) of the box you are in, which happens to be the pdf document, then we subtract 50 points. We do the same thing with the bottom minus the shift of 50 points. We then slap a width and height to our bounding box and perform a block of code in the bounding_box. the local variable count is assigned and then displayed with pdf.text.
</p>
<p>Heres my final example for <a href="http://69.28.95.103/app/uploads/1.pdf">Download</a>.</p>
<p>
Well there you have it; a simple invoice with very little effort. With some additional research you can add more data and style it further to fit your needs. Before you go though lets add a nice link to our PDF on our main show page so the customer can find it without having to type it in the URL manually. Head over to your views folder and open your http://localhost:3000/invoices/show.html.erb file. Then you’ll want to add this code somewhere in your file:
</p>
<pre class="crayon-plain-tag"><%= link_to "PDF Invoice", invoice_path(@invoice, :format => 'pdf') %></pre>
<p>
We’re basically just calling the invoices path and throwing it the invoice with an extension of pdf attached to the end of it. This should show up as a link now so when your customer clicks on it they will be redirected to the PDF version of your data in your rails application.
</p>
<h2>Conclusion</h2>
<p>Prawn is definitely a simple and easy way to get PDFs up and going with very minimal effort. The only downsides I’ve noticed from my perspective is it’s not completely possible to style ever nook and cranny because your at the whims of Prawns formatting options; but I’m guessing it won’t make much of a difference because there aren’t to many alternatives as nice as Prawn or that have this simple functionality. Documentation was available, but examples were limited but nothing a Google search can’t net you. Hopefully this helps out some of those in pursuit of a quick PDF solution.</p>
<p>Final Note: Prawnto’s website mentions it only been updated to rails 2.2 but I had no problems getting it to work on 2.3.5. It may however be broken in rails 3 so just be aware of that when upgrading.</p>
<h2>Other Resources</h2>
<ul>
<li><a href="https://github.com/prawnpdf/prawn" title="Prawn at Github.com" target="">Prawn Github</a></li>
<li><a href="https://github.com/prior/prawnto" title="Prawnto at Github.com" target="">Prawnto Site</a></li>
<li><a href="http://railscasts.com/episodes/153-pdfs-with-prawn" title="PDFs with Prawn at Railscasts.com" target="">Ryan Bates Railscasts #153</a></li>
</ul>
<p>The post <a rel="nofollow" href="http://brandensilva.com/create-a-pdf-invoice-using-prawn-in-rails/">Create a PDF Invoice Using Prawn in Rails</a> appeared first on <a rel="nofollow" href="http://brandensilva.com">Branden Silva</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Crossover: A Dribbble WordPress Widget</title>
		<link>http://brandensilva.com/crossover-dribbble-wordpress-widget/</link>
		<pubDate>Wed, 18 Dec 2013 19:46:56 +0000</pubDate>
		<dc:creator><![CDATA[bsilva]]></dc:creator>
				<category><![CDATA[Free]]></category>

		<guid isPermaLink="false">http://localhost/sandbox/?p=1</guid>
		<description><![CDATA[<p>I created Crossover to be a simple WordPress Dribbble widget that allows you to display your shots. Minimal and lightweight, Crossover weighs in at only 7kb and keeps features to a minimum to avoid bloat and reduce concerns of breakage.&#8230; <a href="http://brandensilva.com/crossover-dribbble-wordpress-widget/">Continued</a></p>
<p>The post <a rel="nofollow" href="http://brandensilva.com/crossover-dribbble-wordpress-widget/">Crossover: A Dribbble WordPress Widget</a> appeared first on <a rel="nofollow" href="http://brandensilva.com">Branden Silva</a>.</p>
]]></description>
				<content:encoded><![CDATA[<p>I created Crossover to be a simple WordPress Dribbble widget that allows you to display your shots.</p>
<p>Minimal and lightweight, Crossover weighs in at only 7kb and keeps features to a minimum to avoid bloat and reduce concerns of breakage.</p>
<p>Big thanks goes to <strong>Tyle Gaw</strong> for making his <a style="color: #ea4c89;" href="http://jribbble.com/">Jribbble</a> plugin which sits at the heart of Crossover.</p>
<h3>Features</h3>
<ul>
<li>Add a players name and pull down their dribbble shot(s).</li>
<li>Pick between 3 pre-defined image sizes.</li>
<li>Set the number of shots you want to display.</li>
</ul>
<h3>Requirements</h3>
<ul>
<li>WordPress 3+</li>
<li>jQuery 1.3 or higher (comes included with WordPress)</li>
</ul>
<h3>Installation &amp; Usage</h3>
<p>First things first, download <a style="color: #ea4c89;" href="https://github.com/brandensilva/Crossover/archive/0.9.zip">Crossover</a> and save it on your computer.</p>
<p>Go to Plugins page within WordPress and click <strong>Add New</strong>. You&#8217;ll want to click <strong>Upload</strong> and chose the file you saved and click <strong>Install Now.</strong> After that is finished, click <strong>Activate</strong> to enable it. </p>
<p>Then go to Widgets within WordPress and drag Crossover to the sidebar you want it to appear on. Set your Dribbble player name, image size, and shot count and click <strong>Save.</strong> You are good to go!</p>
<p>One more thing before you go. Do you need CSS styling? Just target the <strong>.shot</strong> class.</p>
<p><a class="btn-dribbble" href="https://github.com/brandensilva/Crossover/archive/0.9.zip">Download</a></p>
<p>The post <a rel="nofollow" href="http://brandensilva.com/crossover-dribbble-wordpress-widget/">Crossover: A Dribbble WordPress Widget</a> appeared first on <a rel="nofollow" href="http://brandensilva.com">Branden Silva</a>.</p>
]]></content:encoded>
			</item>
	</channel>
</rss>
