<?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>Andreas Happe &#187; ruby on rails</title>
	<atom:link href="http://snikt.net/index.php/tag/rails/feed" rel="self" type="application/rss+xml" />
	<link>http://snikt.net</link>
	<description>Vi veri ueniversum vivus vici</description>
	<lastBuildDate>Wed, 10 Mar 2010 14:22:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Generating PDFs from Ruby on Rails</title>
		<link>http://snikt.net/index.php/2010/03/03/generating-pdfs-from-ruby-on-rails</link>
		<comments>http://snikt.net/index.php/2010/03/03/generating-pdfs-from-ruby-on-rails#comments</comments>
		<pubDate>Wed, 03 Mar 2010 12:25:45 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[adobe pdf]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[prawn]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[wicked_pdf]]></category>
		<category><![CDATA[wkthmltopdf]]></category>

		<guid isPermaLink="false">http://snikt.net/?p=491</guid>
		<description><![CDATA[The problem
Ruby on Rails provides various helpers for generating dynamic web content but sometimes you need documents that users can easily store and share between them. The ubiquitous file format for this is Adobe PDF nowadays.
The common solution fro this problem is Prawn [github, introduction]. Alas it requires a custom DSL for document description, no existing Rails [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<h2>The problem</h2>
<p><a href="http://rubyonrails.org/">Ruby on Rails</a> provides various helpers for generating dynamic web content but sometimes you need documents that users can easily store and share between them. The ubiquitous file format for this is <a href="http://en.wikipedia.org/wiki/Adobe_PDF">Adobe PDF</a> nowadays.</p>
<p>The common solution fro this problem is Prawn [<a href="http://wiki.github.com/sandal/prawn/">github</a>, <a href="http://railscasts.com/episodes/153-pdfs-with-prawn">introduction</a>]. Alas it requires a custom DSL for document description, no existing Rails views or partials can be reused. An alternative is <a href="http://www.princexml.com/">princeXML</a> which transforms HTML/CSS into pdf through an external binary. This would allow reuse of existing templates and knowledge (think CSS designers) but has the downside of its price tag of $3800.</p>
<h2>The solution</h2>
<p>Enters <a href="http://github.com/mileszs/wicked_pdf">wicked_pdf</a>: it utilizes <a href="http://code.google.com/p/wkhtmltopdf/">wkhtmltopdf</a> to create a PDF document from a Rails HTML template. HTML rendering is done through the well-known webkit-engine. This allows developers to do PDFs in the right way™: define the document&#8217;s structure through a simple HTML document and theme them through CSS. You&#8217;ll get the automatic benefit of themability: exchange the CSS and you have another format. There are also lots of CSS artists out there that can supply you with different designs.</p>
<p><span id="more-491"></span></p>
<p>I&#8217;ve used this solution with a test Ruby on Rails 3.0-beta application, the steps involved were:</p>
<h3>Install <em>wkhtmltopdf:</em></h3>
<p><strong><span style="font-weight: normal; font-size: 13px;">This program will convert the HTML into the PDF format. To make it work you&#8217;ll need at least version 0.9 which isn&#8217;t installed in Ubuntu 9.10 by default. Just download the <a href="http://code.google.com/p/wkhtmltopdf/downloads/list">static compiled version</a> from the <a href="http://code.google.com/p/wkhtmltopdf/downloads/list">wkhtmltopdf website</a> and place it under <em>/usr/local/bin</em>. </span></strong></p>
<h3>Install the wicked_pdf plugin:</h3>
<p>Just install it in the usual Rails way:</p>
<pre>git submodule add ﻿git://github.com/mileszs/wicked_pdf.git vendor/plugins/wicked_pdf</pre>
<p>Additionally a sample configuration file is needed. You could use the plugin&#8217;s generator script for it but alas this didn&#8217;t work for me with Rails 3.0. Let&#8217;s just copy it form the plugin&#8217;s directory into config/initializers.</p>
<pre>$ cp vendor/plugins/wicked_pdf/generators/wicked_pdf/templates/wicked_pdf.rb config/initializers</pre>
<p>and alter the wkhtmltopdf path within it:</p>
<pre>  WICKED_PDF = {
       :exe_path =&gt; '/usr/local/bin/wkhtmltopdf-amd64'
  }</pre>
<h3>﻿﻿Add pdf instructions to your controller..</h3>
<p>For example I&#8217;m using the <em>invoice#index</em> action to render a simple PDF document.</p>
<pre>  format.pdf do
          @example_text = "some text"
          render :pdf =&gt; "file_name",
                 :template =&gt; 'offers/show.pdf.erb',
                 :layout =&gt; 'pdf',
                 :footer =&gt; {
                    :center =&gt; "Center",
                    :left =&gt; "Left",
                    :right =&gt; "Right"
                 }
  end</pre>
<p>We are rendering the view with a predefined footer containing some sample &#8220;center&#8221;, &#8220;left&#8221; and &#8220;right&#8221; strings. The <em>render :pdf</em> call has various options which can be seen on the wicked_pdf homepage.</p>
<h3>.. and create PDF templates</h3>
<p>The other half of the PDF templates is the view code consisting of a special layout and template for pdf generation.</p>
<p>The layout (<em>app/views/layouts/pdf.html.erb</em>) resembles a normal Ruby on Rails layout file:</p>
<pre>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
  &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
    &lt;%= wicked_pdf_stylesheet_link_tag "pdf" %&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="content"&gt;
      &lt;%= yield %&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>The view code should not be too surprising, just place it under <em>app/views/offers/index.pdf.erb</em> :</p>
<pre>&lt;div id="someid"&gt;&lt;%= @example_text %&gt;&lt;/div&gt;</pre>
<p>Lets also create a simple CSS file (<span style="font-style: normal;"><em>public/stylesheets/pdf.css</em></span><span style="font-style: normal;">):</span></p>
<pre>#someid {
  margin-left: 430px;
  display: float;
  height: 150px;
  background-color: green;
  width: 250px;
}</pre>
<p><span style="font-style: normal;"> </span></p>
<p>This is the whole rendering code.</p>
<h3>Reference the PDF view and download it</h3>
<p><span style="font-style: normal;">We still need a reference to the newly created rendernig link, we can easily create this through:</span></p>
<pre><span style="font-style: normal;">link_to 'Create PDF document', invoice_path(@invoice, :format =&gt; :pdf)</span></pre>
<p><span style="font-style: normal;">When the link is clicked a pdf document will be generated and downloaded.</span></p>
<h2>Conclusion</h2>
<p>wicked_pdf allows easy pdf generation in a very Ruby on Rails&#8217; way. It&#8217;s free, it works and is easy to employ..</p>
<p><span style="font-style: normal;">..but not everything is perfect within the wicked_pdf world, especially error handlnig is lacking sometimes. When you&#8217;ve debugging initial problems you can expect the only error feedback to be a HTTP return code of 406. Just start with limited controller (rendering) options and double check that you&#8217;ve referenced the right view and layout paths and you should be fine.</span></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://snikt.net/index.php/2010/03/03/generating-pdfs-from-ruby-on-rails/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails vs. Java, part 1</title>
		<link>http://snikt.net/index.php/2008/02/17/ruby-on-rails-vs-java-part-1</link>
		<comments>http://snikt.net/index.php/2008/02/17/ruby-on-rails-vs-java-part-1#comments</comments>
		<pubDate>Sun, 17 Feb 2008 17:05:38 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://happiness-is-slavery.net/index.php/2008/02/17/ruby-on-rails-vs-java-part-1</guid>
		<description><![CDATA[One of the components of the system developed here at Blackwhale is an fairly advanced web mail/campaigning and analytics system. The first iteration of that component was fully implemented in Ruby on Rails. Writing the front end was fairly easy and fast, a perfect opportunity for Rails to display its strengths. On the back end [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>One of the components of the system developed here at <a href="http://www.blackwhale.net">Blackwhale</a> is an fairly advanced web mail/campaigning and analytics system. The first iteration of that component was fully implemented in <a href="http://www.rubyonrails.org">Ruby on Rails</a>. Writing the front end was fairly easy and fast, a perfect opportunity for Rails to display its strengths. On the back end and communication part on the other hand I stumbled into various problems:</p>
<ul>
<li>Compared to PHP, Python or Java there are just too few communication libraries. And even the libraries that exist are lacking fundamental features and almost all of them come without useable documentation. That there is no encryption (SSL or TLS) support in the whole Ruby 1.8 mail libraries is an outright shame. The IMAP library is so clumsy that a web company sells their own (still not perfect library) through their online store and they seem to make a good buck with it.
<p>On the Java side is just a completly different picture: Lots of libraries, even documented ones are available. After having to touch the <a href="http://tmail.rubyforge.org/">TMail</a> (rail&#8217;s MIME mail handler library) API using javax.mail is a heavenly gift. And it seems that the TMail generated MIME messages were invalid in a couple of cases. Not the best feature of a support library. Also the chance of finding unsolved known bugs and errors seems to be lot smaller in Java.</li>
<li>This brings me to another point: I might change my opinion on Java&#8217;s constraints on its users. Java tries hard to prevent errors (i.e. the forced exception catching). I always thought that that took too many stylish possibilities away from the user, but by now I must confess that I think that this is exactly what I want from something that I&#8217;m using on the network side. I&#8217;m a lazy programmer, I want to be reminded and forced to write secure and stable code. This is the quite different to Ruby and Rails &#8216;make it easy for the programmer&#8217; attitude.</li>
<li>Background processing is hard. As Ruby on Rails is not multi-thread safe you can&#8217;t just spawn a thread if you need to perform some longer running task. Another disadvantage of using a single-process model is that the long running request will occupy one rails worker (i.e. rails cluster process) until it has finished &#8211; in our case that costs us around 60MB of memory per long running request, even if it is just waiting for some simple SMTP feedback. If you can find a situation where you can delay the execution of a network related task (and if you don&#8217;t you&#8217;re not thinking) for two seconds, 6 requests per seconds will DoS a standard rails cluster.
<p>The only solution that&#8217;s actually usable is <a href="http://backgroundrb.rubyforge.org/">BackgroundRb</a>. But projects that just change their background communication system just don&#8217;t sound to production-grade ready for me. Also the admin start/stop scripts for their background server didn&#8217;t work too well for me.</li>
<li>For a language that interferes it&#8217;s object&#8217;s attribute types directly from the database the ActiveRecord layer is weak. Don&#8217;t get me wrong, I understand that the Simplicity is needed to make it easily usable but I ran into various situations where I&#8217;d love to have a full blown ORM behind me. One feature that is needed quite often by our application is inheritance. ActiveRecord only offers single table inheritance, and even there you have to make sure that each row is valid (ActiveRecord should have all needed information to do that by itself BTW) or you will run into problems later on. One problem is, that it tries to abstract too much functionality away from the database while not provided as advanced interfaces by itself. Data integrity handling? Abstracted away by rails, so all databases can be used the same. The drawback is, that the data constraints and relations are fully handled by rails and not passed on to the database. Any process that might produce invalid data (e.g. a faulty rails component) might corrupt the data. Rails is able to handle that cases by itself (due to ducktyping and very few default checks), but access that data with any other framework and it blows up directly into your face.</li>
<li>Transaction handling. Just try it. Then cry. Also I&#8217;m not sure if Transaction handling is even done on database level or in Rails (as done with constraints). If the later is true, it&#8217;s acutally not worth anything as soon as more processes try to access the database.</li>
</ul>
<p>The library and documentation problems where the main reason for me to reimplement the mailing and campaigning backend in Java. The front end is still a Rails application &#8212; which is exactly what Rails is for. As I&#8217;m no friend of blown-up EJB based solutions I&#8217;ve choosen a simple Spring and JPA based solution for that problem.</p>
<p>I&#8217;m currently testing the last features and replacing the Ruby code part by part. As soon as I&#8217;ve done that another blog post will examine the two implementations, how much time was spent on coding them and how they perform when compared to each other.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://snikt.net/index.php/2008/02/17/ruby-on-rails-vs-java-part-1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
