mergepdfs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/mergepdfs +24 -0
  3. data/lib/mergepdfs.rb +149 -0
  4. metadata +66 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8114d29c8330650423d9b4fc55831a271c4458db49b52997fa4028cbbd508d89
4
+ data.tar.gz: 5aaa651c281a803af3719840aa2faa026a70f5e50e4c3f381762fa958ddf8a31
5
+ SHA512:
6
+ metadata.gz: 4347bfabd9237fddadec1f7d8441effebd9a04ee704cfbbbf01301d034f08bd041690cc47d896c7f01d08d9c33fa06acfe5890716919428e8d9f5df3ec19030a
7
+ data.tar.gz: 4c76e69266b62ccf49bec832802f0e25159e48695e31cab4af64c3810eb3fc51721cf5601889fcce6fe862852394b0dca3e5993b9a2f3f12c2106b48b7622f88
data/bin/mergepdfs ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ ###
3
+ # mergepdfs combines multiple PDF files into one output PDF file. Empty pages
4
+ # are added to ensure that each merged PDF file starts at an odd page number
5
+ # in the output file. When printed double-sided, pages of the merged PDF files
6
+ # are never printed on the same sheet.
7
+ #
8
+ # Copyright (c) 2020 Huub de Beer <huub@campinecomputing.eu>
9
+ #
10
+ # This program is free software: you can redistribute it and/or modify it
11
+ # under the terms of the GNU Affero General Public License as published by the
12
+ # Free Software Foundation, either version 3 of the License, or (at your
13
+ # option) any later version.
14
+ #
15
+ # This program is distributed in the hope that it will be useful, but WITHOUT
16
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
18
+ # for more details.
19
+ #
20
+ # You should have received a copy of the GNU Affero General Public License
21
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
22
+ ##
23
+ require "mergepdfs"
24
+ MergePDFs.merge(MergePDFs.parse(ARGV))
data/lib/mergepdfs.rb ADDED
@@ -0,0 +1,149 @@
1
+ ##
2
+ # mergepdfs combines multiple PDF files into one output PDF file. Empty pages
3
+ # are added to ensure that each merged PDF file starts at an odd page number
4
+ # in the output file. When printed double-sided, pages of the merged PDF files
5
+ # are never printed on the same sheet.
6
+ #
7
+ # Copyright (c) 2020 Huub de Beer <huub@campinecomputing.eu>
8
+ #
9
+ # This program is free software: you can redistribute it and/or modify it
10
+ # under the terms of the GNU Affero General Public License as published by the
11
+ # Free Software Foundation, either version 3 of the License, or (at your
12
+ # option) any later version.
13
+ #
14
+ # This program is distributed in the hope that it will be useful, but WITHOUT
15
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
17
+ # for more details.
18
+ #
19
+ # You should have received a copy of the GNU Affero General Public License
20
+ # along with this program. If not, see <https://www.gnu.org/licenses/>.
21
+ ##
22
+ module MergePDFs
23
+ require "hexapdf"
24
+ require "optparse"
25
+
26
+ VERSION = "0.1.0"
27
+
28
+ RELEASE_DATE = "2020-04-24"
29
+
30
+ VERSION_MESSAGE = <<~END
31
+ mergepdfs #{VERSION} (#{RELEASE_DATE})
32
+ Copyright (C) Huub de Beer <huub@campinecomputing.eu>
33
+ mergepdfs is free software; mergepdfs is released under the AGPL 3.0
34
+ END
35
+
36
+ DEFAULT_OPTIONS = {
37
+ pdfs: [],
38
+ continue_on_error: false,
39
+ show_diagnostics: false
40
+ }
41
+
42
+ DEFAULT_OUTPUT_FILENAME = "output.pdf"
43
+
44
+ class Error < StandardError; end
45
+
46
+ ##
47
+ # Merge multiple PDF files into a single PDF file, starting each PDF file
48
+ # at an odd page.
49
+ #
50
+ # @param output [String] the filename to write the merged PDFs to
51
+ # @param pdfs [Array<String>] the filenames of the PDF files to merge
52
+ # @param continue_on_error [Boolean] if an input PDF file cannot be
53
+ # merged, continue merging the other input PDF files
54
+ # @param show_diagnostics [Boolean] show more detailed error information
55
+ def self.merge(output:, pdfs: [], continue_on_error: false, show_diagnostics: false)
56
+ merged_pdf = HexaPDF::Document.new
57
+
58
+ pdfs.each do |pdf|
59
+ # invariant: merged_pdf.pages.count.even?
60
+ begin
61
+ pdf_file = HexaPDF::Document.open(pdf)
62
+ pdf_file.pages.each do |page|
63
+ merged_page = merged_pdf.import(page)
64
+ merged_pdf.pages.add(merged_page)
65
+ end
66
+ merged_pdf.pages.add() if pdf_file.pages.count.odd?
67
+ rescue => e
68
+ warn "ERROR\t Unable to open or read '#{pdf}'. Make sure this file is readable, unencrypted, and a PDF file."
69
+ warn e.full_message() if show_diagnostics
70
+ return unless continue_on_error
71
+ end
72
+ end
73
+
74
+ begin
75
+ merged_pdf.write(output, optimize: true)
76
+ rescue => e
77
+ warn "ERROR\t Unable to write the merged PDF to the output file '#{output}'."
78
+ warn e.full_message() if show_diagnostics
79
+ end
80
+ end
81
+
82
+ ##
83
+ # Parse command-line parameters
84
+ #
85
+ # @param args [Array<String>] a list with command-line parameters
86
+ # @return [Hash] a map with options matching the parameters for #merge.
87
+ def self.parse(args = ARGV)
88
+ options = DEFAULT_OPTIONS
89
+
90
+ begin
91
+ OptionParser.new do |parser|
92
+ parser.banner = "Usage: mergepdfs --output FILENAME [OPTIONS] file1.pdf file2.pdf ... fileN.pdf"
93
+
94
+ parser.separator("");
95
+ parser.separator("Required:")
96
+
97
+ parser.on("-o FILENAME", "--output FILENAME",
98
+ "The name of the output PDF file. If no filename is specified,",
99
+ "'#{DEFAULT_OUTPUT_FILENAME}' is used by default.") do |output|
100
+ options[:output] = output
101
+ end
102
+
103
+ parser.separator("");
104
+ parser.separator("Optional:")
105
+
106
+ parser.on("-c", "--continue-on-error",
107
+ "When an error occurs merging a PDF file, mergepdfs tries",
108
+ "to continue merging the other PDF files. Default is false.") do
109
+ options[:continue_on_error] = true
110
+ end
111
+
112
+ parser.on("-d", "--diagnostics",
113
+ "Show detailed error messages. Default is false.") do
114
+ options[:show_diagnostics] = true
115
+ end
116
+
117
+ parser.separator("");
118
+ parser.separator("Common:")
119
+
120
+ parser.on_tail("-v", "--version", "Show the version.") do
121
+ puts VERSION_MESSAGE
122
+ exit
123
+ end
124
+
125
+ parser.on_tail("-h", "--help", "Show this help message.") do
126
+ puts parser
127
+ exit
128
+ end
129
+ end.parse!(args)
130
+
131
+ rescue OptionParser::InvalidOption => e
132
+ warn "ERROR\t #{e}."
133
+ warn e.full_message if options[:show_diagnostics]
134
+ rescue OptionParser::ParseError => e
135
+ warn "ERROR\t Problem while parsing the command-line parameters: #{e}."
136
+ warn e.full_message if options[:show_diagnostics]
137
+ end
138
+
139
+ if not options.key? :output
140
+ options[:output] = DEFAULT_OUTPUT_FILENAME
141
+ warn "ERROR\t No output filename specified. Defaulting to '#{DEFAULT_OUTPUT_FILENAME}'." if options[:show_diagnostics]
142
+ end
143
+
144
+ # The rest of the command-line options are treated as the input PDF
145
+ # files.
146
+ options[:pdfs] = args
147
+ options
148
+ end
149
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mergepdfs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Huub de Beer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hexapdf
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.5
27
+ description: mergepdfs combines multiple PDF files into one output PDF file. Empty
28
+ pages are added to ensure that each merged PDF file starts at an odd page number
29
+ in the output file. When printed double-sided, pages of the merged PDF files are
30
+ never printed on the same sheet.
31
+ email:
32
+ - huub@campinecomputing.eu
33
+ executables:
34
+ - mergepdfs
35
+ extensions: []
36
+ extra_rdoc_files: []
37
+ files:
38
+ - bin/mergepdfs
39
+ - lib/mergepdfs.rb
40
+ homepage: https://gitlab.com/CampineComputing/mergepdfs
41
+ licenses: []
42
+ metadata:
43
+ homepage_uri: https://gitlab.com/CampineComputing/mergepdfs
44
+ source_code_uri: https://gitlab.com/CampineComputing/mergepdfs
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.7
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: mergepdfs combines multiple PDF files into one output PDF file, each starting
65
+ at an odd page.
66
+ test_files: []