Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 19433c756610b59ff41ff9b5959899902a9a3c8b @igor-alexandrov committed Mar 9, 2012
@@ -0,0 +1,3 @@
+coverage
+*.swp
+pkg
11 Gemfile
@@ -0,0 +1,11 @@
+group :development do
+ gem "bundler", "~> 1.0.0"
+ gem "jeweler", "~> 1.6.4"
+end
+
+group :test do
+ gem 'rails'
+ gem "sqlite3", '~> 1.3.4'
+ gem 'shoulda-context'
+ gem "mocha"
+end
@@ -0,0 +1,99 @@
+GEM
+ specs:
+ actionmailer (3.2.2)
+ actionpack (= 3.2.2)
+ mail (~> 2.4.0)
+ actionpack (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.1)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.1)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.2)
+ activemodel (3.2.2)
+ activesupport (= 3.2.2)
+ builder (~> 3.0.0)
+ activerecord (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ arel (~> 3.0.2)
+ tzinfo (~> 0.3.29)
+ activeresource (3.2.2)
+ activemodel (= 3.2.2)
+ activesupport (= 3.2.2)
+ activesupport (3.2.2)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ arel (3.0.2)
+ builder (3.0.0)
+ erubis (2.7.0)
+ git (1.2.5)
+ hike (1.2.1)
+ i18n (0.6.0)
+ jeweler (1.6.4)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ journey (1.0.3)
+ json (1.6.5)
+ mail (2.4.3)
+ i18n (>= 0.4.0)
+ mime-types (~> 1.16)
+ treetop (~> 1.4.8)
+ metaclass (0.0.1)
+ mime-types (1.17.2)
+ mocha (0.10.4)
+ metaclass (~> 0.0.1)
+ multi_json (1.1.0)
+ polyglot (0.3.3)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-ssl (1.3.2)
+ rack
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rails (3.2.2)
+ actionmailer (= 3.2.2)
+ actionpack (= 3.2.2)
+ activerecord (= 3.2.2)
+ activeresource (= 3.2.2)
+ activesupport (= 3.2.2)
+ bundler (~> 1.0)
+ railties (= 3.2.2)
+ railties (3.2.2)
+ actionpack (= 3.2.2)
+ activesupport (= 3.2.2)
+ rack-ssl (~> 1.3.2)
+ rake (>= 0.8.7)
+ rdoc (~> 3.4)
+ thor (~> 0.14.6)
+ rake (0.9.2.2)
+ rdoc (3.12)
+ json (~> 1.4)
+ shoulda-context (1.0.0)
+ sprockets (2.1.2)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ sqlite3 (1.3.5)
+ thor (0.14.6)
+ tilt (1.3.3)
+ treetop (1.4.10)
+ polyglot
+ polyglot (>= 0.3.1)
+ tzinfo (0.3.32)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.0.0)
+ jeweler (~> 1.6.4)
+ mocha
+ rails
+ shoulda-context
+ sqlite3 (~> 1.3.4)
@@ -0,0 +1,20 @@
+Copyright (c) 2008 Miles Z. Sterret
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
213 README.md
@@ -0,0 +1,213 @@
+# PDF
+
+## A PDF generation plugin for Ruby on Rails
+
+PDF uses the shell utility [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. 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 PDF take care of the hard stuff.
+
+**PDF** is inspired by [Wicked PDF](https://github.com/mileszs/wicked_pdf) and [PDFKit](https://github.com/jdpace/PDFKit). PDF is optimized to use with Rails 3.1 (3.2), Ruby 1.9.2 and wkhtmltopdf 0.10.0 (and above).
+
+### Installation
+
+First, be sure to install [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/).
+Note that versions before 0.9.0 [have problems](http://code.google.com/p/wkhtmltopdf/issues/detail?id=82&q=vodnik) on some machines with reading/writing to streams.
+This plugin relies on streams to communicate with wkhtmltopdf.
+
+More information about [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) could be found [here](http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html).
+
+Add this to your Gemfile:
+
+ gem 'pdf'
+
+### Basic Usage
+
+ class ThingsController < ApplicationController
+ def show
+ respond_to do |format|
+ format.html
+ format.pdf do
+ render :pdf => "file_name"
+ end
+ end
+ end
+ end
+
+### Advanced Usage with all available options
+
+ class ThingsController < ApplicationController
+ def show
+ respond_to do |format|
+ format.html
+ format.pdf do
+ render :pdf => 'file_name',
+ :template => 'things/show.pdf.erb',
+ :layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
+ :show_as_html => params[:debug].present?, # allow debuging based on url param
+ :orientation => 'Landscape', # default Portrait
+ :page_size => 'A4, Letter, ...', # default A4
+ :save_to_file => Rails.root.join('pdfs', "#{filename}.pdf"),
+ :save_only => false, # depends on :save_to_file being set first
+ :proxy => 'TEXT',
+ :basic_auth => false # when true username & password are automatically sent from session
+ :username => 'TEXT',
+ :password => 'TEXT',
+ :cover => 'URL',
+ :dpi => 'dpi',
+ :encoding => 'TEXT',
+ :user_style_sheet => 'URL',
+ :cookie => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
+ :post => ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
+ :redirect_delay => NUMBER,
+ :zoom => FLOAT,
+ :page_offset => NUMBER,
+ :book => true,
+ :default_header => true,
+ :disable_javascript => false,
+ :greyscale => true,
+ :lowquality => true,
+ :enable_plugins => true,
+ :disable_internal_links => true,
+ :disable_external_links => true,
+ :print_media_type => true,
+ :disable_smart_shrinking => true,
+ :use_xserver => true,
+ :no_background => true,
+ :margin => {:top => SIZE, # default 10 (mm)
+ :bottom => SIZE,
+ :left => SIZE,
+ :right => SIZE},
+ :header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
+ :url => 'www.example.com',
+ :locals => { :foo => @bar }},
+ :center => 'TEXT',
+ :font_name => 'NAME',
+ :font_size => SIZE,
+ :left => 'TEXT',
+ :right => 'TEXT',
+ :spacing => REAL,
+ :line => true},
+ :footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
+ :url => 'www.example.com',
+ :locals => { :foo => @bar }},
+ :center => 'TEXT',
+ :font_name => 'NAME',
+ :font_size => SIZE,
+ :left => 'TEXT',
+ :right => 'TEXT',
+ :spacing => REAL,
+ :line => true},
+ :outline => {:outline => true,
+ :outline_depth => LEVEL}
+ end
+ end
+ end
+ end
+
+By default, it will render without a layout (:layout => false) and the template for the current controller and action.
+
+### Super Advanced Usage ###
+
+If you need to just create a pdf and not display it:
+
+ # create a pdf from a string
+ pdf = Pdf::Writer.new.to_pdf('<h1>Hello There!</h1>')
+
+ # or from your controller, using views & templates and all wicked_pdf options as normal
+ pdf = render_to_string :pdf => "some_file_name"
+
+ # then save to a file
+ save_path = Rails.root.join('pdfs','filename.pdf')
+ File.open(save_path, 'wb') do |file|
+ file << pdf
+ end
+
+If you need to display utf encoded characters, add this to your pdf views or layouts:
+
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
+
+### Styles
+
+You must define absolute paths to CSS files, images, and javascripts; the best option is to use the *wicked_pdf_stylesheet_link_tag*, *wicked_pdf_image_tag*, and *wicked_pdf_javascript_include_tag* helpers.
+
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
+ <%= wicked_pdf_javascript_include_tag "number_pages" %>
+ </head>
+ <body onload='number_pages'>
+ <div id="header">
+ <%= wicked_pdf_image_tag 'mysite.jpg' %>
+ </div>
+ <div id="content">
+ <%= yield %>
+ </div>
+ </body>
+ </html>
+
+### Page Numbering
+
+A bit of javascript can help you number your pages. Create a template or header/footer file with this:
+
+ <html>
+ <head>
+ <script>
+ function number_pages() {
+ var vars={};
+ var x=document.location.search.substring(1).split('&');
+ for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
+ var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
+ for(var i in x) {
+ var y = document.getElementsByClassName(x[i]);
+ for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
+ }
+ }
+ </script>
+ </head>
+ <body onload="number_pages()">
+ Page <span class="page"></span> of <span class="topage"></span>
+ </body>
+ </html>
+
+Anything with a class listed in "var x" above will be auto-filled at render time.
+
+If you do not have explicit page breaks (and therefore do not have any "page" class), you can also use wkhtmltopdf's built in page number generation by setting one of the headers to "[page]":
+
+ render :pdf => 'filename', :header => { :right => '[page] of [topage]' }
+
+### Configuration
+
+You can put your default configuration, applied to all pdf's at "configure_pdf.rb" initializer.
+
+ Pdf::Configuration.configuration do |config|
+ config.wkhtmltopdf = '/path/to/wkhtmltopdf'
+ config.options = {
+ :layout => "layout.html",
+ :use_xserver => true,
+ :footer => {
+ :right => "#{Date.today.year}",
+ :font_size => 8,
+ :spacing => 8
+ },
+ :margin => {
+ :bottom => 15
+ }
+ }
+
+ end
+
+
+### Debugging
+
+Now you can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.
+
+First of all you must configure the render parameter `:show_as_html => params[:debug]` and then just use it like normally but adding `debug=1` as a param:
+
+ http://localhost:3001/CONTROLLER/X.pdf?debug=1
+
+However, the wicked_pdf_* helpers will use file:// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:
+
+ <%= params[:debug].present? ? image_tag('foo') : pdf_image_tag('foo') %>
@@ -0,0 +1,43 @@
+# encoding: utf-8
+
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ gem.name = "pdf"
+ gem.homepage = "http://github.com/igor-alexandrov/pdf"
+ gem.license = "MIT"
+ gem.summary = "PDF generator for Ruby on Rails"
+ gem.description = %Q{pdf uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, and let pdf take care of the hard stuff.}
+ gem.email = "igor.alexandrov@gmail.com"
+ gem.authors = [ "Igor Alexandrov" ]
+end
+Jeweler::RubygemsDotOrgTasks.new
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+end
+
+task :default => :test
+
+require 'rdoc/task'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "yapdf #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+0.7.5
Oops, something went wrong.

0 comments on commit 19433c7

Please sign in to comment.