Skip to content

Instantly share code, notes, and snippets.

@gavingmiller
Forked from wrburgess/gist:3778949
Created February 14, 2013 19:38
Show Gist options
  • Select an option

  • Save gavingmiller/4955659 to your computer and use it in GitHub Desktop.

Select an option

Save gavingmiller/4955659 to your computer and use it in GitHub Desktop.

Revisions

  1. Randy Burgess created this gist Sep 24, 2012.
    68 changes: 68 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    ##Refs

    * [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf)
    * [wicked_pdf](https://github.com/mileszs/wicked_pdf)
    * [Blog: Generate PDFs in Rails 3.2](http://snikt.net/rails/2012/04/26/wicked-pdf/)

    ## Install wicked_pdf

    Add to Gemfile: ```gem 'wicked_pdf'```

    In terminal: ```bundle```

    ## For local, install wkhtmltopdf

    In terminal: ```git checkout 6e2d550 /usr/local/Library/Formula/wkhtmltopdf.rb```

    In terminal: ```brew install wkhtmltopdf```

    *Make sure you installed wkhtmltopdf 0.9.9*

    ## For Heroku, download wkhtmltopdf

    Go to [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf)

    Download the [wkhtmltopdf-0.9.9 Linux Static Binary (amd64)](http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-amd64.tar.bz2&can=2&q=) binary

    Copy the binary to ``bin/wkhtmltopdf-amd64```

    ## Install QT

    In terminal: ```brew install qt```

    ## Set up the Config

    Create a file: ```config/wicked_pdf.rb```

    ```ruby
    if Rails.env.staging? || Rails.env.production?
    exe_path = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
    else
    exe_path = Rails.root.join('bin', 'wkhtmltopdf').to_s
    # exe_path = '/usr/local/bin/wkhtmltopdf'
    end
    ```

    ## Render PDF in Controller

    ```ruby
    def show
    @model = Model params[:id]

    respond_to do |format|
    format.html
    format.csv do
    model_csv = ModelCSV.new(@lesson)
    send_data(model_csv.to_csv, filename: model_csv.generate_csv_filename)
    end
    format.pdf do
    render pdf: "#{@model.uuid}",
    template: 'admin/modelss/show.pdf.haml',
    disposition: 'attachment',
    :toc => { :depth => 2, :header_text => 'TEXT', disable_links: false }
    end
    end
    end
    ```

    ## Create a PDF View