Skip to content

Commit

Permalink
v1.0.0 (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger authored Jan 21, 2017
1 parent da56c51 commit 80275f3
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 206 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Changelog

* `v1.0.0` - January 21, 2017
- Support `@filename`
- Set PDF Title metadata based on `@filename` if not specified
- Fix for Ruby 1.9.3+
- Cleanup

* `v0.1.1` - January 12, 2015
* `v0.1.0` - October 7, 2014
* `v0.0.2` - February 22, 2012
* `v0.0.1` - February 21, 2012
16 changes: 0 additions & 16 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
source "http://rubygems.org"

# Declare your gem's dependencies in prawn-rails.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# jquery-rails is used by the dummy application
gem "jquery-rails"
gem "prawn"


# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use debugger
# gem 'ruby-debug'
2 changes: 1 addition & 1 deletion MIT-LICENSE → LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2012 YOURNAME
Copyright 2012 Carlos Ortiz

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
97 changes: 0 additions & 97 deletions README.markdown

This file was deleted.

92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Prawn-Rails [![Gem Version](https://badge.fury.io/rb/prawn-rails.svg)](http://badge.fury.io/rb/prawn-rails)
<a href='http://ko-fi.com/A552JBK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>

## Install
```ruby
gem 'prawn-rails'
```

Note: `prawn` and `prawn-table` are dependencies of `prawn-rails` so there is no need to mention it in the projects Gemfile unless you want to use a specific version of either of those libraries.

## Usage
Create a view with `pdf` as format and `prawn` as handler so filename should look like `example.pdf.prawn`

It provides a helper called `prawn_document` which builds a PrawnRails::Document with default options. You can override any options as you please. Example:

```ruby
prawn_document(page_layout: :landscape) do |pdf|
pdf.text "Hello World"
end
```

No need to call `pdf.render`, it is called by `prawn_document`

If you want to customize the name of the file should a user try to save it, you can specify the filename in your action:

```ruby
def show
@filename = 'my_report.pdf'
end
```

## Built-in Helpers
* *html_strip(html)*:
Removes the html tags from a string

## Default configuration

Add a `prawn-rails.rb` config to your Rails app under `config/initializers` like this

```ruby
PrawnRails.config do |config|
config.page_layout = :portrait
config.page_size = "A4"
config.skip_page_creation = false
end
```

Please note that these are the defaults.

For a list of all available options: [http://www.rubydoc.info/gems/prawn/Prawn%2FDocument:initialize](http://www.rubydoc.info/gems/prawn/Prawn%2FDocument:initialize).

For a list of all metadata the the `:info` option supports, please see [https://github.com/prawnpdf/prawn/blob/master/manual/document_and_page_options/metadata.rb](https://github.com/prawnpdf/prawn/blob/master/manual/document_and_page_options/metadata.rb)

If `skip_page_creation` is set to true then have to create the first page yourself for eg.

```ruby
pdf.start_new_page size: "A4", page_layout: :landscape
```

## Examples

1. **Hello World**

```ruby
# hello.pdf.prawn

prawn_document(page_layout: :landscape) do
pdf.text 'Hello World!'
end
```

2. ** Using Active Record **

```ruby
# myproducts.pdf.prawn

prawn_document do
pdf.text 'Current Products are:'
pdf.move_down 20
pdf.table @products.collect{|p| [p.name,p.price]}
end
```

## Credits

Created by Carlos Ortiz - @cortiz

<a href='http://ko-fi.com/A552JBK' target='_blank'><img height='32' style='border:0px;height:32px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=a' border='0' alt='Buy Me a Coffee' /></a>

Maintained by Weston Ganger - @westonganger

Special thanks to @rwilliams, @sigmike, @smber1, @iffyuva
29 changes: 2 additions & 27 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,6 @@
#!/usr/bin/env rake
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end
begin
require 'rdoc/task'
rescue LoadError
require 'rdoc/rdoc'
require 'rake/rdoctask'
RDoc::Task = Rake::RDocTask
end

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'PrawnRails'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'


Bundler::GemHelper.install_tasks
require 'bundler/setup'

require 'rake/testtask'

Expand All @@ -35,5 +11,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end


task :default => :test
task default: :test
1 change: 1 addition & 0 deletions lib/prawn-rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "prawn-rails/config"
require "prawn-rails/engine"
require "prawn-rails/version"

module PrawnRails
end
9 changes: 2 additions & 7 deletions lib/prawn-rails/config.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'ostruct'

module PrawnRails
extend self
extend self

@config = OpenStruct.new(:page_layout => :portrait,
:page_size => "A4",
:skip_page_creation => false)
@config = OpenStruct.new(page_layout: :portrait, page_size: "A4", skip_page_creation: false)

def config
begin
Expand All @@ -14,9 +12,6 @@ def config
puts e
puts e.backtrace
end

end

end


12 changes: 5 additions & 7 deletions lib/prawn-rails/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require "prawn-rails/extension"

module PrawnRails
# This derives from Prawn::Document in order to override defaults. Note
# that the Prawn::Document behaviour itself shouldn't be changed.

# This derives from Prawn::Document in order to override defaults. Note that the Prawn::Document behaviour itself shouldn't be changed.
class Document < Prawn::Document
def initialize(opts = {})
if PrawnRails.config.respond_to?(:to_h)
Expand All @@ -15,15 +15,13 @@ def initialize(opts = {})
super(default)
end

# Typicall text expects a string. But rails views have this interesting
# concept that they implicitly call `to_s` on all the variables before
# rendering. So, passing an integer to text fails:
# Typically text expects a string. But rails views have this interesting concept that they implicitly call
# `to_s` on all the variables before rendering. So, passing an integer to text fails:
#
# pdf.text 10 #=> fails because 10 is not a string
# pdf.text 10.to_s #=> works
#
# To circumvent this situation, we call to_s on value, and delegate
# action to actual Prawn::Document.
# To circumvent this situation, we call to_s on value, and delegate action to actual Prawn::Document.
def text(value, options = {})
super(value.to_s, options)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn-rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Engine < Rails::Engine
ActionView::Base.send(:include, PrawnRails::RailsHelper)
ActionView::Template.register_template_handler(:prawn, PrawnRails::Renderer)

if !Mime::Type.lookup_by_extension(:pdf)
unless Mime::Type.lookup_by_extension(:pdf)
Mime::Type.register_alias("application/pdf", :pdf)
end
end
Expand Down
Loading

0 comments on commit 80275f3

Please sign in to comment.