peasy-pdf 0.1.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,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40a4bf4f44381119a36e034d7f00c6825a8f2bfd9b94b7a95730c4112ce73a39
4
+ data.tar.gz: '018524728166a215ba4306c56c266803b065b793774e55701c37f28a40cd5955'
5
+ SHA512:
6
+ metadata.gz: bf4b63c24c91416fddaef7b77c3aa63366f028168d7f0988c521aaee1a5326b7d497993272a443d14dfb2e0fa6b2ed935688148d02f15110c049befc501576fa
7
+ data.tar.gz: 536f94cac911494f217de70573310986a4180ad0dc748052bdd8f40c5bf08c68c921305435862e3434753c314a6302bb4a7aa9ce176dd0fb426cad66bdd758fa
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "json"
5
+ require "tempfile"
6
+
7
+ module PeasyPDF
8
+ module_function
9
+
10
+ def info(path)
11
+ out, err, status = Open3.capture3("pdfinfo", path.to_s)
12
+ raise Error, "pdfinfo failed: #{err}" unless status.success?
13
+ parse_pdfinfo(out)
14
+ end
15
+
16
+ def page_count(path)
17
+ info(path)[:pages]
18
+ end
19
+
20
+ def merge(inputs, output: nil)
21
+ output ||= "merged_#{Time.now.to_i}.pdf"
22
+ args = ["qpdf", "--empty", "--pages", *inputs.map(&:to_s), "--", output]
23
+ _out, err, status = Open3.capture3(*args)
24
+ raise Error, "merge failed: #{err}" unless status.success?
25
+ output
26
+ end
27
+
28
+ def split(input, output_dir: ".")
29
+ pages = page_count(input)
30
+ results = []
31
+ (1..pages).each do |p|
32
+ out = File.join(output_dir, "page_#{p}.pdf")
33
+ _o, err, st = Open3.capture3("qpdf", input.to_s, "--pages", input.to_s, p.to_s, "--", out)
34
+ raise Error, "split page #{p} failed: #{err}" unless st.success?
35
+ results << out
36
+ end
37
+ results
38
+ end
39
+
40
+ def rotate(input, degrees: 90, output: nil)
41
+ output ||= input.to_s.sub(/\.pdf$/i, "_rotated.pdf")
42
+ _o, err, st = Open3.capture3("qpdf", input.to_s, "--rotate=+#{degrees}", "--", output)
43
+ raise Error, "rotate failed: #{err}" unless st.success?
44
+ output
45
+ end
46
+
47
+ def compress(input, output: nil)
48
+ output ||= input.to_s.sub(/\.pdf$/i, "_compressed.pdf")
49
+ _o, err, st = Open3.capture3("qpdf", "--linearize", input.to_s, output)
50
+ raise Error, "compress failed: #{err}" unless st.success?
51
+ output
52
+ end
53
+
54
+ class Error < StandardError; end
55
+
56
+ def parse_pdfinfo(text)
57
+ data = {}
58
+ text.each_line do |line|
59
+ key, val = line.split(":", 2).map(&:strip)
60
+ next unless key && val
61
+ case key
62
+ when "Pages" then data[:pages] = val.to_i
63
+ when "Page size" then data[:page_size] = val
64
+ when "File size" then data[:file_size] = val
65
+ when "Title" then data[:title] = val
66
+ when "Author" then data[:author] = val
67
+ when "Creator" then data[:creator] = val
68
+ when "Producer" then data[:producer] = val
69
+ when "PDF version" then data[:pdf_version] = val
70
+ end
71
+ end
72
+ data
73
+ end
74
+ private_class_method :parse_pdfinfo
75
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PeasyPDF
4
+ VERSION = "0.1.1"
5
+ end
data/lib/peasy_pdf.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "peasy_pdf/version"
4
+ require_relative "peasy_pdf/engine"
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peasy-pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - PeasyTools
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: PDF manipulation library for Ruby — merge, split, compress, rotate, and
13
+ watermark PDF files. Wraps system PDF tools (qpdf, poppler-utils) with a clean Ruby
14
+ API.
15
+ email:
16
+ - hello@peasytools.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/peasy_pdf.rb
22
+ - lib/peasy_pdf/engine.rb
23
+ - lib/peasy_pdf/version.rb
24
+ homepage: https://peasypdf.com
25
+ licenses:
26
+ - MIT
27
+ metadata:
28
+ homepage_uri: https://peasypdf.com
29
+ source_code_uri: https://github.com/peasytools/peasy-pdf-rb
30
+ changelog_uri: https://github.com/peasytools/peasy-pdf-rb/blob/main/CHANGELOG.md
31
+ documentation_uri: https://peasypdf.com
32
+ bug_tracker_uri: https://github.com/peasytools/peasy-pdf-rb/issues
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 4.0.3
48
+ specification_version: 4
49
+ summary: PDF manipulation — merge, split, compress, rotate, watermark
50
+ test_files: []