alego_pdf 2.0.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +7 -0
- data/README.md +57 -27
- data/lib/alego_pdf/merging.rb +31 -5
- data/lib/alego_pdf/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d12c47e9413bad6620a61d589ab7c2e739d42266978912d3808783a133ee43e
|
4
|
+
data.tar.gz: 8206d1a197fe6bf33c2cf60350d14a015fe2055695f13c62354ee550ca78d6ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b91b573155e8d17d5ba8f8a0fa2ca1dee2dd9044fd054c2715d4def4a44dfa32c3fd0fe7fd07fb5b46a49da5253f81e7f94ed6ac51c0a4ae199b443ff6afb881
|
7
|
+
data.tar.gz: 9068a35ab3ed0bacb15768eaaa05715145cd4104a40f1f8eac9b7786e82367d38275720da36f58031af97a0a3c53ba54007770589135bd62e6a5ddbce8c43658
|
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -32,42 +32,71 @@ require 'hexapdf'
|
|
32
32
|
require 'open-uri'
|
33
33
|
|
34
34
|
module AlegoPdf
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
35
|
+
class Merging
|
36
|
+
attr_reader :ids, :model, :attribute, :filename, :urls, :orderize, :target
|
37
|
+
|
38
|
+
def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url')
|
39
|
+
@ids = ids
|
40
|
+
@model = model
|
41
|
+
@attribute = attribute
|
42
|
+
@filename = filename
|
43
|
+
@urls = urls
|
44
|
+
@orderize = orderize
|
45
|
+
@target = HexaPDF::Document.new
|
46
|
+
end
|
44
47
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
def call
|
49
|
+
if orderize == 'url'
|
50
|
+
join_urls
|
51
|
+
join_array
|
52
|
+
else
|
53
|
+
join_array
|
54
|
+
join_urls
|
55
|
+
end
|
48
56
|
|
49
|
-
|
50
|
-
|
51
|
-
filepath = file.try(:url) || object.try(:arquivo_url)
|
57
|
+
target.write(filename, optimize: true)
|
58
|
+
end
|
52
59
|
|
53
|
-
|
60
|
+
def join_urls
|
61
|
+
urls.each do |filepath|
|
62
|
+
next if filepath.blank?
|
54
63
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
64
|
+
uri = URI.parse(filepath).open(&:read)
|
65
|
+
tempfile = Tempfile.new
|
66
|
+
tempfile.write(uri.force_encoding('UTF-8'))
|
67
|
+
tempfile.close
|
59
68
|
|
60
|
-
|
61
|
-
|
62
|
-
|
69
|
+
pdf = HexaPDF::Document.open(tempfile)
|
70
|
+
pdf.pages.each { |page| target.pages << target.import(page) }
|
71
|
+
end
|
72
|
+
end
|
63
73
|
|
64
|
-
|
65
|
-
|
74
|
+
def join_array
|
75
|
+
array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)
|
76
|
+
|
77
|
+
array.each do |object|
|
78
|
+
file = object[attribute]
|
79
|
+
filepath = file.try(:url) || object.try(:arquivo_url)
|
80
|
+
|
81
|
+
next if filepath.blank?
|
82
|
+
|
83
|
+
uri = URI.parse(filepath).open(&:read)
|
84
|
+
tempfile = Tempfile.new
|
85
|
+
tempfile.write(uri.force_encoding('UTF-8'))
|
86
|
+
tempfile.close
|
87
|
+
|
88
|
+
pdf = HexaPDF::Document.open(tempfile)
|
89
|
+
pdf.pages.each { |page| target.pages << target.import(page) }
|
90
|
+
end
|
66
91
|
end
|
92
|
+
end
|
67
93
|
end
|
68
94
|
```
|
69
95
|
Obs.: Os parâmetros 'ids', 'model' e 'attribute' são obrigatórios. O parâmetro 'filename' é por default 'tmp/merge_files.pdf', porém pode-se atribuir um caminho pré-definido. o atributo 'filename' será o local e nome onde será salvo o arquivo temporário.
|
70
|
-
|
96
|
+
O parâmetro 'urls' é um array de url's e serve para ler PDF's a partir de url e juntá-los num mesmo arquivo. Default = []. Utilizar os 'ids' e as 'urls', serve para juntar arquivos além de um model apenas, como acontece no projeto 'emendas-rails' da Alego.
|
97
|
+
Após instalar a gem 'alego_pdf', deve criar uma rota '/merge_arquivos' em seu projeto e deve buscar os ids dos arquivos e passá-los como parâmetro junto com o model, o atributo e as urls que onde é guardado os metadados dos arquivos. A gem procura também por um atributo chamado 'arquivo_url', caso não encontre a 'url' (ex.: attribute.try(:url)).
|
98
|
+
Ainda no método call, com o atributo 'orderize' podemos ordenar por urls ou ids no arquivo PDF. Default = 'url'.
|
99
|
+
Veja o exemplo a seguir:
|
71
100
|
```
|
72
101
|
# organogramas_controller.rb
|
73
102
|
def merge_arquivos
|
@@ -76,8 +105,9 @@ def merge_arquivos
|
|
76
105
|
respond_to do |format|
|
77
106
|
format.html
|
78
107
|
format.pdf do
|
108
|
+
url = 'https://teste.com/teste.pdf' # url precisa ser um PDF
|
79
109
|
filename = 'tmp/organogramas.pdf' # nome e local onde será salvo o arquivo temporário
|
80
|
-
AlegoPdf::Merging.new(organograma_ids, ::Transparencia::Organograma, :arquivo, filename).call
|
110
|
+
AlegoPdf::Merging.new(organograma_ids, ::Transparencia::Organograma, :arquivo, filename, [url].compact).call
|
81
111
|
|
82
112
|
pdf_filename = File.join(Rails.root, filename)
|
83
113
|
send_file(pdf_filename, filename: 'organogramas.pdf', type: 'application/pdf')
|
data/lib/alego_pdf/merging.rb
CHANGED
@@ -3,18 +3,46 @@ require 'open-uri'
|
|
3
3
|
|
4
4
|
module AlegoPdf
|
5
5
|
class Merging
|
6
|
-
attr_reader :ids, :model, :attribute, :filename
|
6
|
+
attr_reader :ids, :model, :attribute, :filename, :urls, :orderize, :target
|
7
7
|
|
8
|
-
def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf')
|
8
|
+
def initialize(ids, model, attribute, filename = 'tmp/merge_files.pdf', urls = [], orderize = 'url')
|
9
9
|
@ids = ids
|
10
10
|
@model = model
|
11
11
|
@attribute = attribute
|
12
12
|
@filename = filename
|
13
|
+
@urls = urls
|
14
|
+
@orderize = orderize
|
15
|
+
@target = HexaPDF::Document.new
|
13
16
|
end
|
14
17
|
|
15
18
|
def call
|
19
|
+
if orderize == 'url'
|
20
|
+
join_urls
|
21
|
+
join_array
|
22
|
+
else
|
23
|
+
join_array
|
24
|
+
join_urls
|
25
|
+
end
|
26
|
+
|
27
|
+
target.write(filename, optimize: true)
|
28
|
+
end
|
29
|
+
|
30
|
+
def join_urls
|
31
|
+
urls.each do |filepath|
|
32
|
+
next if filepath.blank?
|
33
|
+
|
34
|
+
uri = URI.parse(filepath).open(&:read)
|
35
|
+
tempfile = Tempfile.new
|
36
|
+
tempfile.write(uri.force_encoding('UTF-8'))
|
37
|
+
tempfile.close
|
38
|
+
|
39
|
+
pdf = HexaPDF::Document.open(tempfile)
|
40
|
+
pdf.pages.each { |page| target.pages << target.import(page) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def join_array
|
16
45
|
array = model.is_a?(String) ? model.camelize.constantize.where(id: ids) : model.where(id: ids)
|
17
|
-
target = HexaPDF::Document.new
|
18
46
|
|
19
47
|
array.each do |object|
|
20
48
|
file = object[attribute]
|
@@ -30,8 +58,6 @@ module AlegoPdf
|
|
30
58
|
pdf = HexaPDF::Document.open(tempfile)
|
31
59
|
pdf.pages.each { |page| target.pages << target.import(page) }
|
32
60
|
end
|
33
|
-
|
34
|
-
target.write(filename, optimize: true)
|
35
61
|
end
|
36
62
|
end
|
37
63
|
end
|
data/lib/alego_pdf/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: alego_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo de Sá
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-11-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -93,8 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
|
97
|
-
rubygems_version: 2.7.6
|
96
|
+
rubygems_version: 3.3.22
|
98
97
|
signing_key:
|
99
98
|
specification_version: 4
|
100
99
|
summary: Gem responsible for joining 1 or more PDF in the same file.
|