evil_pdf 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +30 -0
- data/lib/evil_pdf.rb +61 -0
- metadata +69 -0
data/README.rdoc
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
== EvilPDF
|
2
|
+
|
3
|
+
Generate a PDF file from an array of URLs
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
# this is here for clarity, this record creation can be internalized into EvilPDF
|
9
|
+
pdf_record = PdfRecord.new :name => 'file_name_no_ext'
|
10
|
+
|
11
|
+
evil_pdf = EvilPdf.new pdf_record,
|
12
|
+
:async => true, # run in delayed job, updates record automatically
|
13
|
+
|
14
|
+
evil_pdf.from_urls ['http://google.com']
|
15
|
+
|
16
|
+
# save to db manually
|
17
|
+
pdf_record.pdf = evil_pdf.file_handle
|
18
|
+
pdf_record.save
|
19
|
+
|
20
|
+
# immediate download, must not be async
|
21
|
+
send_file evil_pdf.file_path,
|
22
|
+
:type => "application/pdf",
|
23
|
+
:disposition => "inline"
|
24
|
+
|
25
|
+
== Dependencies
|
26
|
+
|
27
|
+
- PDFKit[https://github.com/pdfkit/pdfkit] wrapper
|
28
|
+
- wkhtmltopdf[http://github.com/antialize/wkhtmltopdf] binary
|
29
|
+
- Ghostscript[http://www.ghostscript.com/] to combine PDFs into one file.
|
30
|
+
- delayed_job
|
data/lib/evil_pdf.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
class EvilPdf
|
2
|
+
require 'open-uri'
|
3
|
+
require 'pdfkit'
|
4
|
+
attr_reader :file_handle
|
5
|
+
|
6
|
+
def initialize(name, options = {})
|
7
|
+
@record = PdfRecord.create :name => name
|
8
|
+
@options = options
|
9
|
+
Dir.mkdir './tmp' unless Dir.exists? './tmp'
|
10
|
+
self.class.handle_asynchronously :from_urls if options[:async]
|
11
|
+
end
|
12
|
+
|
13
|
+
def from_urls(urls)
|
14
|
+
@tmp_files = []
|
15
|
+
urls.each_with_index do |url, i|
|
16
|
+
retrieve(url) and generate(i)
|
17
|
+
end
|
18
|
+
combine
|
19
|
+
|
20
|
+
if @options[:async]
|
21
|
+
@record.update_attributes :pdf => file_handle
|
22
|
+
else
|
23
|
+
file_handle
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_file(name)
|
28
|
+
PDFKit.new(@html, @options).to_file name
|
29
|
+
end
|
30
|
+
|
31
|
+
def file_handle
|
32
|
+
@file_handle ||= File.open file_path, 'r'
|
33
|
+
end
|
34
|
+
|
35
|
+
def file_path
|
36
|
+
@file_path ||= "./tmp/#{@record.name.parameterize}-#{Time.now.to_i}.pdf"
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def retrieve(url)
|
42
|
+
@html = open(url).read
|
43
|
+
rescue OpenURI::HTTPError => e
|
44
|
+
@record.errors.add :pdf, "HTTPError #{e.message}: #{url}"
|
45
|
+
false
|
46
|
+
end
|
47
|
+
|
48
|
+
def generate(i)
|
49
|
+
tmp = "./tmp/partial-#{Time.now.to_i}-#{i}.pdf"
|
50
|
+
to_file tmp
|
51
|
+
@tmp_files << tmp
|
52
|
+
end
|
53
|
+
|
54
|
+
# using ghostscript to combine multiple pdfs into 1
|
55
|
+
def combine
|
56
|
+
gs_opts = "-q -dNOPAUSE -sDEVICE=pdfwrite"
|
57
|
+
gs_cmd = "gs #{gs_opts} -sOutputFile=#{file_path} -dBATCH #{@tmp_files.join(' ')}"
|
58
|
+
Rails.logger.debug "Combining PDFs: #{gs_cmd}"
|
59
|
+
system gs_cmd
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: evil_pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Diego Salazar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-29 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pdfkit
|
16
|
+
requirement: &70354072908840 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70354072908840
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pdfkit-heroku
|
27
|
+
requirement: &70354072908360 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70354072908360
|
36
|
+
description: Generate a PDF from an array of URLs
|
37
|
+
email: diego@pixelheadstudio.com
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README.rdoc
|
42
|
+
files:
|
43
|
+
- lib/evil_pdf.rb
|
44
|
+
- README.rdoc
|
45
|
+
homepage: https://github.com/DiegoSalazar/EvilPDF-Gem/
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.9.2
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.10
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Generate a PDF from an array of URLs
|
69
|
+
test_files: []
|