Skip to content

Commit

Permalink
Remove automated installer. Add link to wiki page.
Browse files Browse the repository at this point in the history
  Too many issues cropping up around that feature.
  • Loading branch information
jdpace committed Dec 27, 2010
1 parent 1dc58e6 commit add90fd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 110 deletions.
32 changes: 19 additions & 13 deletions README.md
Expand Up @@ -9,32 +9,38 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
gem install pdfkit

### wkhtmltopdf
* **Automatic**: `sudo pdfkit --install-wkhtmltopdf`
install latest version into /usr/local/bin
(overwrite defaults with e.g. ARCHITECTURE=amd64 TO=/home/foo/bin)
* By hand: http://code.google.com/p/wkhtmltopdf/downloads/list

1. Install by hand (recomended):

https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF

2. Try using the wkhtmltopdf-binary gem (mac + linux i386)

gem install wkhtmltopdf-binary

*Note:* The automated installer has been removed.

## Usage

# PDFKit.new takes the HTML and any options for wkhtmltopdf
# run `wkhtmltopdf --extended-help` for a full list of options
kit = PDFKit.new(html, :page_size => 'Letter')
kit.stylesheets << '/path/to/css/file'

# Git an inline PDF
pdf = kit.to_pdf

# Save the PDF to a file
file = kit.to_file('/path/to/save/pdf')

# PDFKit.new can optionally accept a URL or a File.
# Stylesheets can not be added when source is provided as a URL of File.
kit = PDFKit.new('http://google.com')
kit = PDFKit.new(File.new('/path/to/html'))

# Add any kind of option through meta tags
PDFKit.new('<html><head><meta name="pdfkit-page_size" content="Letter")

## Configuration

If you're on Windows or you installed wkhtmltopdf by hand to a location other than /usr/local/bin you will need to tell PDFKit where the binary is. You can configure PDFKit like so:
Expand All @@ -55,17 +61,17 @@ PDFKit comes with a middleware that allows users to get a PDF view of any page o
### Middleware Setup

**Non-Rails Rack apps**

# in config.ru
require 'pdfkit'
use PDFKit::Middleware

**Rails apps**

# in application.rb(Rails3) or environment.rb(Rails2)
require 'pdfkit'
config.middleware.use PDFKit::Middleware

**With PDFKit options**

# options will be passed to PDFKit.new
Expand All @@ -75,7 +81,7 @@ PDFKit comes with a middleware that allows users to get a PDF view of any page o
- add amd64 support in --install-wkhtmltopdf

## Note on Patches/Pull Requests

* Fork the project.
* Setup your development environment with: gem install bundler; bundle install
* Make your feature addition or bug fix.
Expand Down
72 changes: 0 additions & 72 deletions bin/pdfkit

This file was deleted.

42 changes: 21 additions & 21 deletions lib/pdfkit/pdfkit.rb
Expand Up @@ -3,43 +3,43 @@ class PDFKit
class NoExecutableError < StandardError
def initialize
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
msg << ">> Install wkhtmltopdf by hand or try running `pdfkit --install-wkhtmltopdf`"
msg << ">> Please install wkhtmltopdf - https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF"
super(msg)
end
end

class ImproperSourceError < StandardError
def initialize(msg)
super("Improper Source: #{msg}")
end
end

attr_accessor :source, :stylesheets
attr_reader :options

def initialize(url_file_or_html, options = {})
@source = Source.new(url_file_or_html)

@stylesheets = []

@options = PDFKit.configuration.default_options.merge(options)
@options.merge! find_options_in_meta(url_file_or_html) unless source.url?
@options = normalize_options(@options)

raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf)
end

def command
args = [executable]
args += @options.to_a.flatten.compact
args << '--quiet'

if @source.html?
args << '-' # Get HTML from stdin
else
args << @source.to_s
end

args << '-' # Read PDF from stdout
args
end
Expand All @@ -53,10 +53,10 @@ def executable
default.split('/').last
end
end

def to_pdf
append_stylesheets

pdf = Kernel.open('|-', "w+")
exec(*command) if pdf.nil?
pdf.puts(@source.to_s) if @source.html?
Expand All @@ -67,11 +67,11 @@ def to_pdf
raise "command failed: #{command.join(' ')}" if result.to_s.strip.empty?
return result
end

def to_file(path)
File.open(path,'w') {|file| file << self.to_pdf}
end

protected

def find_options_in_meta(body)
Expand All @@ -92,14 +92,14 @@ def pdfkit_meta_tags(body)
rescue # rexml random crash on invalid xml
[]
end

def style_tag_for(stylesheet)
"<style>#{File.read(stylesheet)}</style>"
end

def append_stylesheets
raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?

stylesheets.each do |stylesheet|
if @source.to_s.match(/<\/head>/)
@source.to_s.gsub!(/(<\/head>)/, style_tag_for(stylesheet)+'\1')
Expand All @@ -108,7 +108,7 @@ def append_stylesheets
end
end
end

def normalize_options(options)
normalized_options = {}

Expand All @@ -119,11 +119,11 @@ def normalize_options(options)
end
normalized_options
end

def normalize_arg(arg)
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
end

def normalize_value(value)
case value
when TrueClass
Expand All @@ -132,5 +132,5 @@ def normalize_value(value)
value.to_s
end
end
end

end
15 changes: 11 additions & 4 deletions pdfkit.gemspec
Expand Up @@ -46,10 +46,17 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/jdpace/PDFKit}
s.post_install_message = %q{******************************************************************
Now install wkhtmltopdf binaries:
Global: sudo `which pdfkit` --install-wkhtmltopdf
or inside RVM folder: export TO=`which pdfkit | sed 's:/pdfkit:/wkhtmltopdf:'` && pdfkit --install-wkhtmltopdf
(run pdfkit --help to see more options)
Now install the wkhtmltopdf binary
==================================
1. Install by hand (recomended):
https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF
2. Try using the wkhtmltopdf-binary gem (mac + linux i386)
gem install wkhtmltopdf-binary
******************************************************************}
s.rdoc_options = ["--charset=UTF-8"]
Expand Down

0 comments on commit add90fd

Please sign in to comment.