compile_pdf 1.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWYyMGNiYjAyMjMxNzliY2QxMzBmOGViNTM1MThiNGViOTM1OTdiYg==
5
+ data.tar.gz: !binary |-
6
+ Y2VjZGI1OTk2Y2U1Yjg2ODk2Mzg3MDhiYTYxMWUyMWFiZjE4N2Q1OA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZmU4NTBmMTU0YzBjYTkzODg4Mjg5MzVkOGQwZWExYzc0ODFkMWQwYjIxMTJj
10
+ NDc4ODlmZmFlYmIwMjY4MDY1M2Y4YWUwNjRiMTc5MzlkYzE5YTEyYmZhMTBk
11
+ MTg0ZTcwYjdjMTNmM2UyYjI2NTM0OGRkMjUyMzJkMTA1MWVmZTM=
12
+ data.tar.gz: !binary |-
13
+ YTk4ZWI1OTUwNjY0YzlhZTYzZDI0ZjJmYjNlM2EyNjllMDFiYTEzZDZkNDFk
14
+ MTRkMTNhMzdkNmY0ZmNkODBkZjRlYzJlN2Y3MGZmZmI4OWEwMmU5OTIyMDU2
15
+ YzZjNmNkNTYyOGUxYjZhYTJkMGMxZjFhYjQwNDI3NDUzNWQzOWE=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in compile_pdf.gemspec
4
+ gemspec
5
+
6
+ gem "yomu", "~> 0.1.9" , :platforms => :ruby
7
+ gem "prawn", "~> 1.0.0.rc2" , :platforms => :ruby
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tanmay
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Compile Pdf
2
+
3
+ If you have multiple format files like Image files(Eg. .png, .jpg, .jpeg etc.), Document files(Eg. .doc, .docx, text etc.)
4
+
5
+ or PDF files and now you want to make a single PDF with all format of files, this gem will help you to compile multiple
6
+
7
+ format files into single pdf.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'compile_pdf'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install compile_pdf
23
+
24
+ ## Usage
25
+
26
+ Currently this is only supported with following format files:-
27
+
28
+ 1. Image Files(Eg. .PNG, .JPG, .JPEG etc.)
29
+ 2. PDF files(Eg. .pdf)
30
+ 3. Text and Note files(Eg. .txt etc. )
31
+ 4. Document files(Eg. .docx, .doc etc.)
32
+
33
+ Open Rails console:-
34
+
35
+ > require 'compile_pdf'
36
+
37
+ > extend CompilePdf
38
+
39
+ > compile_pdf_module(file_array, attachment_object_name, file_name = nil)
40
+
41
+ file_array: Take the required files which you want to convert into a pdf
42
+
43
+ attachment_object_name: If you are using PaperClip as File uploader module
44
+ has_attached_file :attachment
45
+ Then your attachment_object_name will be "attachment"
46
+
47
+ file_name: Default will be Time.now or else you can specify your own PDF name.
48
+
49
+
50
+ For Example:--
51
+
52
+ file_array = File.all
53
+
54
+ In your File model if
55
+ has_attached_file :attachment
56
+
57
+ attachment_object_name = "attachment"
58
+
59
+ file_name = "my_attachment"
60
+
61
+ compile_pdf_module(file_array, attachment_object_name, file_name)
62
+
63
+ Default file location will be Your Project root folder "tmp/" folder
64
+
65
+ I have tested with PaperClip and Aws S3 and its working fine.
66
+
67
+ Soon I will release for "Dragonfly", "attachment_fu" and "CarrierWave" attachment gem.
68
+
69
+
70
+ ## Dependency
71
+
72
+ This gem is highly dependent on :-
73
+
74
+ 1. gem "yomu", "~> 0.1.9" , :platforms => :ruby
75
+ 2. gem "prawn", "~> 1.0.0.rc2" , :platforms => :ruby
76
+
77
+ If you have any other version of Prawn it may conflict with 'Compile Pdf' gem.
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'compile_pdf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "compile_pdf"
8
+ spec.version = CompilePdf::VERSION
9
+ spec.authors = ["Tanmay Bagchi"]
10
+ spec.email = ["tbagchi85@gmail.com"]
11
+ spec.description = %q{If you have multiple format files like Image files(Eg. .png, .jpg, .jpeg etc.), Document files(Eg. .doc, .docx, text etc.)
12
+ or PDF files and now you want to make a single PDF with all format of files, this gem will help you to compile multiple
13
+ format files into single pdf.}
14
+ spec.summary = %q{If you have multiple format files like Image files(Eg. .png, .jpg, .jpeg etc.), Document files(Eg. .doc, .docx, text etc.)
15
+ or PDF files and now you want to make a single PDF with all format of files, this gem will help you to compile multiple
16
+ format files into single pdf.}
17
+ spec.homepage = ""
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "yomu", "~> 0.1.9"
26
+ spec.add_dependency "prawn", "~> 1.0.0.rc2"
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency 'rake', '~> 10.1', '>= 10.1.0'
29
+ end
@@ -0,0 +1,93 @@
1
+ module CompilePdf
2
+
3
+ def compile_pdf_module(attachments, attachment_type, file_name = nil)
4
+ arr = []
5
+ attachments.each do |a|
6
+ if file_type(a, attachment_type) == "application" && is_pdf?(a, attachment_type)
7
+ arr << attachment_type(a, attachment_type).to_s
8
+ end
9
+ end
10
+
11
+ file_name = file_name_generate(file_name)
12
+
13
+ Prawn::Document.generate(file_name) do |phr|
14
+ attachments.each do |a|
15
+ if file_type(a, attachment_type) == "application" && is_pdf?(a, attachment_type)
16
+ generate_pdf(phr,a, attachment_type)
17
+ elsif a.file_type == "image"
18
+ generate_image(phr,a, attachment_type)
19
+ elsif file_type(a, attachment_type) != "not_found" && is_pdf?(a, attachment_type) != "not_found"
20
+ generate_text(phr,a, attachment_type)
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def attachment_type(file, attachment_type)
27
+ file.send(attachment_type)
28
+ end
29
+
30
+ def file_name_generate(file_name)
31
+ if file_name
32
+ "#{Rails.root}/tmp/#{file_name}.pdf"
33
+ else
34
+ "#{Rails.root}/tmp/#{Time.now}_phr.pdf"
35
+ end
36
+ end
37
+
38
+ def merge(pdf_paths, destination)
39
+ first_pdf_path = pdf_paths.delete_at(0)
40
+ Prawn::Document.generate(destination, :template => first_pdf_path) do |pdf|
41
+ pdf_paths.each do |pdf_path|
42
+ pdf.go_to_page(pdf.page_count)
43
+ template_page_count = count_pdf_pages(pdf_path)
44
+ (1..template_page_count).each do |template_page_number|
45
+ pdf.start_new_page(:template => pdf_path, :template_page => template_page_number)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def count_pdf_pages(pdf_file_path)
52
+ pdf = Prawn::Document.new(:template => pdf_file_path)
53
+ pdf.page_count
54
+ end
55
+
56
+ def generate_image(phr,a, attachment_type)
57
+ phr.go_to_page(phr.page_count)
58
+ phr.start_new_page
59
+ file_path = attachment_type(a, attachment_type).to_s
60
+ phr.move_down 5
61
+ phr.image open(file_path, 'User-Agent' => 'ruby'), :position => :center, :fit => [600, 700]
62
+ end
63
+
64
+ def generate_pdf(phr,a, attachment_type)
65
+ file_path = attachment_type(a, attachment_type).to_s
66
+ file_path = open(file_path, 'User-Agent' => 'ruby')
67
+ phr.go_to_page(phr.page_count)
68
+ template_page_count = count_pdf_pages(file_path)
69
+ (1..template_page_count).each do |template_page_number|
70
+ phr.start_new_page(:template => file_path, :template_page => template_page_number,:size => "LEGAL", :layout => :landscape, :margin => 0)
71
+ end
72
+ end
73
+
74
+ def generate_text(phr, a, attachment_type)
75
+ phr.go_to_page(phr.page_count)
76
+ phr.start_new_page
77
+ path = attachment_type(a, attachment_type).to_s
78
+ open_file = open(path, 'User-Agent' => 'ruby')
79
+ data = open_file.read
80
+ content = Yomu.read :text, data
81
+ phr.move_down 5
82
+ phr.text content
83
+ end
84
+
85
+ def file_type(attachment, attachment_type)
86
+ attachment.respond_to?("#{attachment_type}_content_type") ? attachment.send("#{attachment_type}_content_type").split('/').first : "not_found"
87
+ end
88
+
89
+ def is_pdf?(attachment, attachment_type)
90
+ attachment.respond_to?("#{attachment_type}_content_type") ? attachment.send("#{attachment_type}_content_type").split('/').last == "pdf" : "not_found"
91
+ end
92
+
93
+ end
@@ -0,0 +1,3 @@
1
+ module CompilePdf
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'prawn'
2
+ require 'yomu'
3
+ require 'open-uri'
4
+ require "compile_pdf/version"
5
+ require 'compile_pdf/generate_pdf'
6
+
7
+
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compile_pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tanmay Bagchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: yomu
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: prawn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0.rc2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0.rc2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.1'
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: 10.1.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ~>
70
+ - !ruby/object:Gem::Version
71
+ version: '10.1'
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: 10.1.0
75
+ description: ! "If you have multiple format files like Image files(Eg. .png, .jpg,
76
+ .jpeg etc.), Document files(Eg. .doc, .docx, text etc.)\n or
77
+ PDF files and now you want to make a single PDF with all format of files, this gem
78
+ will help you to compile multiple\n format files into single
79
+ pdf."
80
+ email:
81
+ - tbagchi85@gmail.com
82
+ executables: []
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - compile_pdf.gemspec
92
+ - lib/compile_pdf.rb
93
+ - lib/compile_pdf/generate_pdf.rb
94
+ - lib/compile_pdf/version.rb
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: If you have multiple format files like Image files(Eg. .png, .jpg, .jpeg
119
+ etc.), Document files(Eg. .doc, .docx, text etc.) or PDF files and now you want
120
+ to make a single PDF with all format of files, this gem will help you to compile
121
+ multiple format files into single pdf.
122
+ test_files: []