Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating PDF from HTML in rails

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 491
    Comment on it

    Creating PDF from HTML in rails

    Hi friends,
    In rails if you want to generate a pdf, there are multiple gems available that will make your task easy. Normally the approach for creating a pdf will be first create a html template and then convert that template into pdf. Here I am going to tell you how we can use "wicked_pdf" gem.

    1) First you need to add the gem to your Gemfile:

    1.     gem 'wicked_pdf'

    2) Now run bundler in root directory:

    1.     > bundle

    3) Now create initializers by generators for customizing the configurable options available in wicked_pdf

    1.     > rails generate wicked_pdf
    2.     #=>
    3.     create config/initializers/wicked_pdf.rb

    4) If you are using an older version of rails, you may also need to configure the mime type inside config/initializers/mime_types.rb

    1.     Mime::Type.register "application/pdf", :pdf

    5) Now one thing to be noted wicked_pdf works over wkhtmltopdf, which needs to be installed on your machine, For installing wkhtmltopdf you can through the following link:
    Install pdfkit and wkhtmltopdf library on Ubuntu 12.04/ruby
    Alternatively you can install all the required components by adding a gem 'wkhtmltopdf-binary' in your Gemfile and running the bundle

    6) After that you need to update the path of the wkhtmltopdf inside the config/initializers/wicked_pdf.rb, if it is not the one where wkhtmltopdf if installed:

    1.     WickedPdf.config = {
    2.      exe_path: '/usr/local/bin/wkhtmltopdf'
    3.     }

    Note: For information regarding wkhtmltopdf you can see http://wkhtmltopdf.org/

    7) The output of a file in pdf can be achieved as simple as rendering html from a view like this:

    1.     class WelcomeController < ApplicationController
    2.      def index
    3.      respond_to do |format|
    4.      format.html
    5.      format.pdf do
    6.      render pdf: "index"
    7.      end
    8.      end
    9.      end
    10.     end

    If a index.pdf.erb is present there, it will get rendered as pdf

    8) Thus we can see that we can create html file with extension .pdf.erb and it will be rendered as a pdf. But as wkhtmltopdf is installed outside the rails application it will not get the layouts present in the rails application. for that we will use the wicked_pdf helpers like this:

    1.     <!doctype html>
    2.     <html>
    3.      <head>
    4.      <meta charset='utf-8' />
    5.      <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
    6.      <%= wicked_pdf_javascript_include_tag "number_pages" %>
    7.      </head>
    8.      <body onload='number_pages'>
    9.      <div id="header">
    10.      <%= wicked_pdf_image_tag 'image path' %>
    11.      </div>
    12.      <div id="content">
    13.      <%= yield %>
    14.      </div>
    15.      </body>
    16.     </html>


    9) At last there are so many customizable options that can be used during the rendering of the pdf, some of them are like:

    1. respond_to do |format|
    2. format.html
    3. format.pdf do
    4. render pdf: 'file_name',
    5. disposition: 'attachment', # default 'inline'
    6. template:'template_path',
    7. ..............
    8. header: {
    9. html:
    10. {
    11.      template: 'some_header', # use :template OR :url
    12. layout: 'pdf_layout_name',
    13. .....
    14. }
    15. },
    16. footer: {
    17.      html:
    18. {
    19.      template: 'some_footer',# use :template OR :url
    20. layout: 'pdf_layout_name',
    21. .....
    22. }
    23. },
    24. toc: {
    25. font_name: "NAME",
    26. depth: LEVEL,
    27. ......
    28. }
    29. end
    30. end

    There are so many other customizable options that you can get at https://github.com/mileszs/wicked_pdf

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: