pludoni_pdfutils 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5e7d468305c410e8f8f695110f38775daa2fef7af07d9bc4e0a5f97510fb8b1
4
- data.tar.gz: fa005b2369e0c10da01c7dced6e97fe99303bceaa5a676033b01c2664a102208
3
+ metadata.gz: 561f53e393a627ae11963fa8254bb39b8445d574dd38b05a558c3088b87ab7d5
4
+ data.tar.gz: baa9dedf3eb2f0c76630c1f598ad8acc99b6eeba5c6ce5a6b6370f793f6263e9
5
5
  SHA512:
6
- metadata.gz: d5cd8e9a78961acafeb93e1528c33adfbcfec271c8c6e3a819ba9c68662068a1e9b5831fe717cadcb9d9f09711c7ad2dd4e3e968219d555a44c9b6abdbc39ae3
7
- data.tar.gz: b1454820999b261d8d0045dc0847a0ad0adbc3d0564242d392c58d744d603c4c983a2e8aba0c45bce5eb05f598d36723e3db546bc4939132783c808a923906f6
6
+ metadata.gz: 06e78bd0c4608b45415f79f132be1ac7b4adbd70dba1c2df0069fa2216582c1e71ecbf156b891692e0b49b43c071f3b32a2c18209bac52d626379311e88711fa
7
+ data.tar.gz: 6f69bf3240182880b623f31cb7621809df0e48d41c241db917b76b03c8e547bd8d4e3457778131d77066e758670220c90f611f541323695ca38e2694c8db5991
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
- tempfile = Pludoni::Pdfutils::ConvertToPdf.new(image_file).run
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
@@ -0,0 +1,39 @@
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
+ @blob.open do |source|
11
+ # convert image to pdf
12
+ tf = Tempfile.new(['convert', '.pdf'])
13
+ tf.binmode
14
+
15
+ command = ['soffice']
16
+ command << '--headless'
17
+ command << '--convert-to'
18
+ command << 'pdf'
19
+ command << '--outdir'
20
+ command << File.dirname(source.path)
21
+ command << source.path
22
+ stdout, stderr, status = Open3.capture3(*command)
23
+ unless status.success?
24
+ raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
25
+ end
26
+ pdf_path = source.path.sub(/\.[a-z0-9]+$/i, '.pdf')
27
+ unless File.exist?(pdf_path)
28
+ raise ConversionFailedError, "PDF conversion failed: Command: #{cli}\nStdout: #{stdout}\nStderr: #{stderr}"
29
+ end
30
+
31
+ FileUtils.move(pdf_path, tf.path)
32
+
33
+ tf.open
34
+
35
+ FileWrapper.make(tf)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,7 +1,7 @@
1
1
  require 'open3'
2
2
 
3
3
  module Pludoni::Pdfutils
4
- class ConvertToPdf
4
+ class ConvertImageToPdf
5
5
  def initialize(blob)
6
6
  @blob = FileWrapper.make(blob)
7
7
  end
@@ -1,4 +1,3 @@
1
- require "pludoni/pdfutils/version"
2
1
  require 'mimemagic'
3
2
 
4
3
  module Pludoni
@@ -47,10 +46,17 @@ module Pludoni
47
46
 
48
47
  def convert_to_pdf(blob, &block)
49
48
  blob = FileWrapper.make(blob)
50
- # TODO: convert odt docx
51
- if blob.content_type.to_s['image']
49
+ case blob.content_type.to_s
50
+ when /image/
52
51
  block.call("converting #{blob.filename.to_s} to pdf") if block_given?
53
- ConvertToPdf.new(blob).run(&block)
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)
54
60
  else
55
61
  blob
56
62
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = "pludoni_pdfutils"
4
- spec.version = "0.1.1"
4
+ spec.version = "0.2.0"
5
5
  spec.authors = ["Stefan Wienert"]
6
6
  spec.email = ["info@stefanwienert.de"]
7
7
 
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.1.1
4
+ version: 0.2.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-09-28 00:00:00.000000000 Z
11
+ date: 2022-10-06 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/convert_to_pdf.rb
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