<?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>manu &#8211; redpanthers.co</title>
	<atom:link href="/author/manu/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Sun, 17 Mar 2013 13:06:41 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>PDF generation in rails with wicked_pdf</title>
		<link>/pdf-generation-in-rails-with-wicked_pdf-2/</link>
				<pubDate>Sun, 17 Mar 2013 13:06:41 +0000</pubDate>
		<dc:creator><![CDATA[manu]]></dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=1341</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Recently my friend was working on a project that had a requirement to export the html page to PDF. He had some issues setting this up. So I thought I would write this article, so that it will be helpful for anyone who faces the same issue. It’s easy getting your pdf in rails as long as you follow the right steps <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> (But unfortunately my good friend doesnt always do that for some reason of his own - No offense intended <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /> )
If you are using Rails, then you have this wonderful <a href="http://rubygems.org/" rel="nofollow noreferrer">rubygems</a> where you can find a number of gems for pdf generation. The suggested ones are prawn and wicked_pdf.


<h2><a id="which-one-to-chose-" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#which-one-to-chose-"></a>Which one to chose ?</h2>


For me wicked_pdf is very simple to understand and it gets your job done very quickly if you follow all the right steps, while Prawn restricts you to the old table, grids layout. But ultimately its you who will have to decide which to use depending upon your specific requirements.
I will be using wicked_pdf and ubuntu.


<h2><a id="wicked_pdf" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#wicked_pdf"></a>Wicked_pdf</h2>


Wicked_pdf uses a shell utility <a href="http://code.google.com/p/wkhtmltopdf/" rel="nofollow noreferrer">Wkhtmltopdf</a> to serve a PDF file to a user from a HTML page. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff


<h2><a id="now-what-is-wkhtmltopdf-" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#now-what-is-wkhtmltopdf-"></a>Now what is wkhtmltopdf ?</h2>


<a href="http://code.google.com/p/wkhtmltopdf" rel="nofollow noreferrer">Wkhtmltopdf</a> is an c++ executable that <a href="https://github.com/mileszs/wicked_pdf" rel="nofollow noreferrer">wicked_pdf</a> gem essentialy wraps. It works great for pdf generation of tables reports etc. The WK in wkhtmltopdf stands for webkit which is the browser engine that is used to render HTML and java-script Chrome uses this same engine.


<h2><a id="installation" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#installation"></a>Installation</h2>


Since we use Ubuntu for development I will be covering steps for installations for ubuntu machines. It wont be hard for you to change the commands a little bit according to your OS.
Installing it simply via
emphasis sudo apt-get install wkhtmltopdf on Ubuntu will install a reduced functionality version which is probably not what you want. According to the manual the reduced functionality version does not include the following features.
Printing more then one HTML document into a PDF file. Running without an X11 server. Adding a document outline to the PDF file. Adding headers and footers to the PDF file. Generating a table of contents. Adding links in the generated PDF file. Printing using the screen media-type. Disabling the smart shrink feature of webkit.
If you need any of these, then you need to setup the static version of wkhtmltopdf.
First check OS is 32 bit or 64 bit by using following command


<pre class="code highlight js-syntax-highlight plaintext white"><code>Try uname -m    </code></pre>


Run following command for installing dependencies.


<pre class="code highlight js-syntax-highlight plaintext white"><code>sudo apt-get install openssl build-essential xorg libssl-dev libxrender-dev  </code></pre>


Based on OS download wkhtmltopdf package from following site <a href="http://code.google.com/p/wkhtmltopdf/downloads/list" rel="nofollow noreferrer">http://code.google.com/p/wkhtmltopdf/downloads/list</a>
Then extract using command


<pre class="code highlight js-syntax-highlight plaintext white"><code>tar xvjf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2    </code></pre>


Then move extracted DIR to usr/local/bin folder


<pre class="code highlight js-syntax-highlight plaintext white"><code>sudo mv wkhtmltopdf-0.11.0_rc1-static-amd64 /usr/local/bin/wkhtmltopdf     </code></pre>


Check wkhtmltopdf is installed or not using following command
which wkhtmltopdf
Using wicked_pdf in your Rails app Add this to your Gemfile:


<pre class="code highlight js-syntax-highlight plaintext white"><code>gem 'wicked_pdf'   </code></pre>


You may also need to add the following to your config/initializers/mime_types.rb
Mime::Type.register &#8220;application/pdf&#8221;, :pdf
Basic Usage:


<pre class="code highlight js-syntax-highlight plaintext white"><code>class Invoice &lt; ApplicationController
  def show
   respond_to do |format|
         format.html
         format.pd[id]f do
          render :pdf =&gt; "invoice"
       end
    end
   end
end</code></pre>


If you need to include stylesheets and javascript inside the generated document, the wicked_pdf gem has helpers for that
&lt;%= wicked_pdf_stylesheet_link_tag &#8220;pdf_stylesheet&#8221; -%&gt;
&lt;%= wicked_pdf_javascript_include_tag &#8220;pdf_javascript&#8221; %&gt;
Some tweaks and tips
If you need to hide some links in your generated PDF document then you can use a custom stylesheet which defines the properties for print media and can include the same using the above helper method.
Eg: You have a button on your html page and you dont want that to be displayed when you generate the PDF.
If you have the following link with css selector &#8220;print_friendly&#8221;
&lt;%= link_to &#8220;Print&#8221; , &#8220;<a href="http://example.com/download" rel="nofollow noreferrer">http://example.com/download</a>&#8220;, :id=&gt;&#8221;print_friendly&#8221; %&gt;
and have a stylesheet with the css properties for print media as


<pre class="code highlight js-syntax-highlight plaintext white"><code>@media print {
  #print_friendly{
     display:hidden;
  }
}</code></pre>


then the link wont be visible inside the generated PDF document.
If your page has a lot of javascript and needs longer time for rendering then you can add the following the wicked_pdf
wkhtmltopdf.exe &#8211;javascript-delay 15000 <a href="http://example.com/downloads" rel="nofollow noreferrer">http://example.com/downloads</a>


<h2><a id="some-common-issues" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#some-common-issues"></a>Some common Issues</h2>




<h2><a id="1-runtime-error" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#1-runtime-error"></a>1. Runtime Error</h2>


RuntimeError in XxxxController#show
Failed to execute: /usr/bin/wkhtmltopdf &#8211;print-media-type -q &#8211; &#8211; Error: PDF could not be generated!
Rails.root: /apps/rails/show
Check the location of your wkhtmltopdf using which wkhtmltopdf and also run a bundle update command as sometimes your gem in gemfile would be configured with the wrong path.


<h2><a id="2-is-wkhtmltopdf-binary-necessary-to-generated-pdf-files-" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#2-is-wkhtmltopdf-binary-necessary-to-generated-pdf-files-"></a>2. Is wkhtmltopdf-binary necessary to generated PDF files ?</h2>


No
If you have installed the system libraries and have configured the path correctly in your app correctly, you dont need to install he wkhtmltopdf-binary gem seperately.


<h2><a id="3-wicked_pdf-not-rendering-graphs-with-highcharts" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#3-wicked_pdf-not-rendering-graphs-with-highcharts"></a>3. Wicked_pdf not rendering graphs with HighCharts</h2>


Wkhtmltopdf doesn&#8217;t handle transparency/shadows properly and can result in unpredictable results. So the workaround is to add the following options to yo2ur highcharts
<code>f.options[:plotOptions][:line] = { :animation =&gt; false, :shadow =&gt; false, :enableMouseTracking =&gt; false }</code>


<h2><a id="as-i-said-earlier-its-easy-getting-your-pdf-in-rails-as-long-as-you-follow-the-right-steps-" class="anchor" href="http://code.redpanthers.co/manusajith/rubykitkchen/blob/master/_posts/2013-03-17-pdf-generation-with-rails.markdown#as-i-said-earlier-its-easy-getting-your-pdf-in-rails-as-long-as-you-follow-the-right-steps-"></a>As I said earlier it’s easy getting your pdf in rails as long as you follow the right steps ;)##</h2>


&#8211;]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>JTK How to kill a rails server, who is working as a deamon?</title>
		<link>/jtk-how-to-kill-a-rails-server-who-is-working-as-a-deamon/</link>
				<comments>/jtk-how-to-kill-a-rails-server-who-is-working-as-a-deamon/#comments</comments>
				<pubDate>Mon, 25 Feb 2013 13:05:04 +0000</pubDate>
		<dc:creator><![CDATA[manu]]></dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=1339</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[$rails s , command has a -d (detach) option to let run your application run as a daemon and to stop this process you need to kill the process. Given below is a small shell command that will find the process id of our rails application and kill it for us.


<pre class="code highlight js-syntax-highlight plaintext white"><code>kill $(lsof -i :3000 -t)</code></pre>


<code>The meaning of the above command is to find the process id of the process which is running in port</code>number 3000 and send to it the KILL signal.
A handy little command, which has not technical importance, but nice to know.
—
Issued in the interest of new and novice programmers.. <img src="https://s.w.org/images/core/emoji/12.0.0-1/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" />
-]]&gt;		</p>
]]></content:encoded>
							<wfw:commentRss>/jtk-how-to-kill-a-rails-server-who-is-working-as-a-deamon/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
							</item>
	</channel>
</rss>
