Git Product home page Git Product logo

railspdf's Introduction

railspdf

Adds pdf-writer support to Rails 2.2


Usage
-----

Before going further, do not forget to add the following line in one of our initializers file:

	ActionView::Template.register_template_handler 'rpdf', RailsPDF::PDFRender

To begin rendering PDFs, simply create a view with a .rpdf extension and paste in the code:

  pdf.select_font "Times-Roman"
  pdf.text "Hello, World", :font_size => 72, :justification => :center

If you want the text to be dynamic, simply replace "Hello World" with an instance variable. It works like a charm, although I had to rearrange the code a bit to make it work. (See the sample controller code at the bottom.)

I've not yet tested any of this; I was just too excited once it started working. Note: to get plugins to work properly, you MUST restart the server after installing it.

  **Important** If you are using a layout, you must disable it for the view!!! 

The default filename for the pdf is "Default.pdf" I'll probably change that later to reflect the view name, but for now it works pretty good. To override it, set an instance variable in your controller named "@rails_pdf_name" The rendered pdf will take this filename.

Please let me know if you have any questions.


Sample Controller
-----------------

  class PagesController < ApplicationController
    def getpdf
    	@rails_pdf_name = "Hello.pdf"
    	@content = "This is dynamic content!!!"
    end
  end

Another way
-----------

	class OrdersController < ApplicationController
		def show
			@order = Order.find(params[:id])
    
	    respond_to do |format|
	      format.html
	      format.pdf do 
	        @rails_pdf_name = "order_#{@order.token}.pdf"
	        render :layout => false
	      end
	    end
		end
	end

Note: you have to register the mime-type for pdf files. The line below has to be added to your config/initializer/mime_types.rb file:

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

To render the link in your views to generate the pdf, just do this:
	
	<%= link_to 'pdf', formatted_order_path(order, :pdf) %>

Sample View
-----------

  pdf.select_font "Times-Roman"
  pdf.text @content, :font_size => 72, :justification => :center


Misc
----

Original RubyForge project (outdated):
  http://rubyforge.org/projects/railspdfplugin/

GitHub fork:
  http://github.com/pelargir/railspdf/

Clone URL:
  git://github.com/pelargir/railspdf.git


Credit
------

  * Created by Tom Willett
  * Forked and updated by Matthew Bass <[email protected]>

railspdf's People

Contributors

did avatar

Stargazers

Angus H. avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.