pludoni_pdfutils 0.1.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/lib/pludoni/pdfutils/active_storage_wrapper.rb +1 -1
- data/lib/pludoni/pdfutils/compressor.rb +3 -2
- data/lib/pludoni/pdfutils/convert_document_to_pdf.rb +40 -0
- data/lib/pludoni/pdfutils/{convert_to_pdf.rb → convert_image_to_pdf.rb} +4 -3
- data/lib/pludoni/pdfutils/file_wrapper.rb +6 -4
- data/lib/pludoni/pdfutils/joiner.rb +5 -4
- data/lib/pludoni/pdfutils/local_file_wrapper.rb +3 -1
- data/lib/pludoni/pdfutils.rb +10 -3
- data/pludoni_pdfutils.gemspec +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 439ff3f6cb56782dc05fe363a19d8c1f2f416b4943a201fba1c94fb72db9d9f8
|
4
|
+
data.tar.gz: 3501da1a290946bf56e8cde0daae07ae63d72a5c05aed5f653c245a7826e1087
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f29026a2d7c8f40bf9a14ecad38a458ff621904f0a94fb6856a4635f0eec105027e9f9f04ba18088e8b1695cbd0469ea840ccea59f5539c46da9a34526eb7b4
|
7
|
+
data.tar.gz: 54b4b7b4b3096e3b8485d36a6a5becb7219af56f08f3b0b55c8c4bfd9be0f401d255a6293c1a2f03b465de1eedc974841ddf335c6618c488580540f50f6dc9bf
|
data/README.md
CHANGED
@@ -21,6 +21,8 @@ Or install it yourself as:
|
|
21
21
|
|
22
22
|
requires ghostscript installed.
|
23
23
|
|
24
|
+
Conversion of document formats (docx etc.) requires soffice installed
|
25
|
+
|
24
26
|
## Usage
|
25
27
|
|
26
28
|
All method take either ActiveStorage::Blob or File/Tempfile as argument and return a Tempfile/File
|
@@ -41,7 +43,10 @@ Pludoni::Pdfutils.convert_all_to_pdf_and_join_max_size(blobs, max_files: 3, max_
|
|
41
43
|
Individual classes:
|
42
44
|
|
43
45
|
```ruby
|
44
|
-
|
46
|
+
# jpg
|
47
|
+
tempfile = Pludoni::Pdfutils::ConvertImageToPdf.new(image_file).run
|
48
|
+
# docx etc.
|
49
|
+
tempfile = Pludoni::Pdfutils::ConvertDocumentToPdf.new(image_file).run
|
45
50
|
|
46
51
|
|
47
52
|
tempfile = Pludoni::Pdfutils::Joiner.new(job_application.uploads.map(&:blob)).run
|
@@ -6,7 +6,7 @@ module Pludoni
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_tf
|
9
|
-
file = Tempfile.new([
|
9
|
+
file = Tempfile.new([@file.filename.base, @file.filename.extension_with_delimiter])
|
10
10
|
ActiveStorage::Downloader.new(@file.service).send(:download, @file.key, file)
|
11
11
|
file
|
12
12
|
end
|
@@ -7,7 +7,8 @@ module Pludoni::Pdfutils
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def run
|
10
|
-
|
10
|
+
fname = File.basename(@blob.filename.to_s, '.*')
|
11
|
+
tf = Tempfile.new([fname, '.pdf'])
|
11
12
|
tf.binmode
|
12
13
|
input = @blob.to_tf
|
13
14
|
cli = "gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=#{Shellwords.escape tf.path} #{Shellwords.escape input.path}"
|
@@ -17,7 +18,7 @@ module Pludoni::Pdfutils
|
|
17
18
|
raise CompressionFailed, "PDF Compression failed: \nStdout: #{stdout}\nStderr: #{stderr}"
|
18
19
|
end
|
19
20
|
|
20
|
-
FileWrapper.make(tf)
|
21
|
+
FileWrapper.make(tf, filename: fname + ".pdf")
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'open3'
|
2
|
+
|
3
|
+
module Pludoni::Pdfutils
|
4
|
+
class ConvertDocumentToPdf
|
5
|
+
def initialize(blob)
|
6
|
+
@blob = FileWrapper.make(blob)
|
7
|
+
end
|
8
|
+
|
9
|
+
def run(&block)
|
10
|
+
fname = File.basename(@blob.filename.to_s, File.extname(@blob.filename.to_s))
|
11
|
+
@blob.open do |source|
|
12
|
+
# convert image to pdf
|
13
|
+
tf = Tempfile.new([fname, '.pdf'])
|
14
|
+
tf.binmode
|
15
|
+
|
16
|
+
command = ['soffice']
|
17
|
+
command << '--headless'
|
18
|
+
command << '--convert-to'
|
19
|
+
command << 'pdf'
|
20
|
+
command << '--outdir'
|
21
|
+
command << File.dirname(source.path)
|
22
|
+
command << source.path
|
23
|
+
stdout, stderr, status = Open3.capture3(*command)
|
24
|
+
unless status.success?
|
25
|
+
raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
|
26
|
+
end
|
27
|
+
pdf_path = source.path.sub(/\.[a-z0-9]+$/i, '.pdf')
|
28
|
+
unless File.exist?(pdf_path)
|
29
|
+
raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
|
30
|
+
end
|
31
|
+
|
32
|
+
FileUtils.move(pdf_path, tf.path)
|
33
|
+
|
34
|
+
tf.open
|
35
|
+
|
36
|
+
FileWrapper.make(tf, filename: "#{fname}.pdf")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
require 'open3'
|
2
2
|
|
3
3
|
module Pludoni::Pdfutils
|
4
|
-
class
|
4
|
+
class ConvertImageToPdf
|
5
5
|
def initialize(blob)
|
6
6
|
@blob = FileWrapper.make(blob)
|
7
7
|
end
|
8
8
|
|
9
9
|
def run(&block)
|
10
|
+
fname = File.basename(@blob.filename.to_s, '.*')
|
10
11
|
@blob.open do |source|
|
11
12
|
# convert image to pdf
|
12
|
-
tf = Tempfile.new([
|
13
|
+
tf = Tempfile.new([fname, '.pdf'])
|
13
14
|
tf.binmode
|
14
15
|
cli = "gs -dNOSAFER -dPDFSETTINGS=/prepress -sDEVICE=pdfwrite -o #{tf.path} viewjpeg.ps -c \\(#{source.path}\\) viewJPEG"
|
15
16
|
|
@@ -18,7 +19,7 @@ module Pludoni::Pdfutils
|
|
18
19
|
raise ConversionFailedError, "PDF convertion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
|
19
20
|
end
|
20
21
|
|
21
|
-
FileWrapper.make(tf)
|
22
|
+
FileWrapper.make(tf, filename: "#{fname}.pdf")
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
@@ -5,12 +5,16 @@ module Pludoni
|
|
5
5
|
@file = file
|
6
6
|
end
|
7
7
|
|
8
|
-
def self.make(blob_or_file)
|
8
|
+
def self.make(blob_or_file, filename: nil)
|
9
9
|
case blob_or_file
|
10
10
|
when ActiveStorage::Blob
|
11
11
|
ActiveStorageWrapper.new(blob_or_file)
|
12
12
|
when File, Tempfile
|
13
|
-
LocalFileWrapper.new(blob_or_file)
|
13
|
+
fw = LocalFileWrapper.new(blob_or_file)
|
14
|
+
if filename
|
15
|
+
fw.filename = filename
|
16
|
+
end
|
17
|
+
fw
|
14
18
|
when FileWrapper
|
15
19
|
blob_or_file
|
16
20
|
else
|
@@ -32,5 +36,3 @@ module Pludoni
|
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
35
|
-
|
36
|
-
|
@@ -7,17 +7,18 @@ module Pludoni::Pdfutils
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def run
|
10
|
-
|
10
|
+
fname = @blobs.map(&:filename).map { |i| i.split('.').first[0..20] }.join('-')
|
11
|
+
|
12
|
+
tf = Tempfile.new([fname, '.pdf'])
|
11
13
|
tf.binmode
|
12
14
|
tfs = @blobs.map { |i| i.to_tf }
|
13
15
|
cli = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=#{tf.path} #{tfs.map(&:path).join(' ')}"
|
14
|
-
|
15
|
-
stdout, stderr, status = Open3.capture3(cli)
|
16
|
+
stdout, stderr, status = Open3.capture3(cli)
|
16
17
|
unless status.success?
|
17
18
|
raise JoiningFailedError, "PDF Joining failed: \nStdout: #{stdout}\nStderr: #{stderr}"
|
18
19
|
end
|
19
20
|
|
20
|
-
FileWrapper.make(tf)
|
21
|
+
FileWrapper.make(tf, filename: "#{fname}.pdf")
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|
data/lib/pludoni/pdfutils.rb
CHANGED
@@ -46,10 +46,17 @@ module Pludoni
|
|
46
46
|
|
47
47
|
def convert_to_pdf(blob, &block)
|
48
48
|
blob = FileWrapper.make(blob)
|
49
|
-
|
50
|
-
|
49
|
+
case blob.content_type.to_s
|
50
|
+
when /image/
|
51
51
|
block.call("converting #{blob.filename.to_s} to pdf") if block_given?
|
52
|
-
|
52
|
+
ConvertImageToPdf.new(blob).run(&block)
|
53
|
+
when "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
54
|
+
"application/msword",
|
55
|
+
"application/vnd.ms-powerpoint",
|
56
|
+
"application/vnd.oasis.opendocument.text",
|
57
|
+
"application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
58
|
+
block.call("converting #{blob.filename.to_s} to pdf") if block_given?
|
59
|
+
ConvertDocumentToPdf.new(blob).run(&block)
|
53
60
|
else
|
54
61
|
blob
|
55
62
|
end
|
data/pludoni_pdfutils.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pludoni_pdfutils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Wienert
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activestorage
|
@@ -99,7 +99,8 @@ files:
|
|
99
99
|
- lib/pludoni/pdfutils.rb
|
100
100
|
- lib/pludoni/pdfutils/active_storage_wrapper.rb
|
101
101
|
- lib/pludoni/pdfutils/compressor.rb
|
102
|
-
- lib/pludoni/pdfutils/
|
102
|
+
- lib/pludoni/pdfutils/convert_document_to_pdf.rb
|
103
|
+
- lib/pludoni/pdfutils/convert_image_to_pdf.rb
|
103
104
|
- lib/pludoni/pdfutils/file_wrapper.rb
|
104
105
|
- lib/pludoni/pdfutils/joiner.rb
|
105
106
|
- lib/pludoni/pdfutils/local_file_wrapper.rb
|