pdfmult 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -1
- data/Rakefile +1 -1
- data/lib/pdfmult.rb +60 -17
- data/man/pdfmult.1 +9 -1
- data/pdfmult.gemspec +2 -1
- data/test/test_latex_document.rb +18 -0
- data/test/{test_pdfmult.rb → test_optionparser.rb} +9 -42
- data/test/test_pdfinfo.rb +35 -0
- metadata +11 -7
data/README.md
CHANGED
@@ -11,6 +11,9 @@ If `pdfmult` succeeds in obtaining the page count it will rearrange all pages,
|
|
11
11
|
if not, only the first page is processed
|
12
12
|
(unless the page count was specified via command line option).
|
13
13
|
|
14
|
+
If the `--latex` option is used, `pdflatex` is not run and
|
15
|
+
a LaTeX file is created instead of a PDF.
|
16
|
+
|
14
17
|
Examples
|
15
18
|
--------
|
16
19
|
|
@@ -41,7 +44,7 @@ Installation
|
|
41
44
|
|
42
45
|
Use `gem install pdfmult`.
|
43
46
|
|
44
|
-
Or
|
47
|
+
Or copy `lib/pdfmult.rb` under the name `pdfmult` into your search path.
|
45
48
|
|
46
49
|
On a Linux system you can use `[sudo] rake install`
|
47
50
|
to install `pdfmult` and its man page to `/usr/local`.
|
@@ -53,6 +56,8 @@ As of now, `pdfmult` has only been tested on a Linux system.
|
|
53
56
|
|
54
57
|
- `pdfmult` is written in [Ruby][Ruby], so Ruby must be installed on your system.
|
55
58
|
- `pdfmult` uses `pdflatex` with the `pdfpages` package, so both have to be installed on the system.
|
59
|
+
(If `pdfmult` can not find the `pdflatex` command on your system
|
60
|
+
you might want to use the `--latex` option.)
|
56
61
|
- `pdfmult` tries to obtain the page count of PDF files with `pdfinfo`.
|
57
62
|
If it fails, by default only the first page of a PDF file will be processed.
|
58
63
|
|
data/Rakefile
CHANGED
data/lib/pdfmult.rb
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
#
|
21
21
|
# +pdfmult+ uses +pdflatex+ with the +pdfpages+ package,
|
22
22
|
# so both have to be installed on the system.
|
23
|
+
# If the --latex option is used, though, +pdflatex+ is not run
|
24
|
+
# and a LaTeX file is created instead of a PDF.
|
23
25
|
#
|
24
26
|
# == Options
|
25
27
|
#
|
@@ -30,6 +32,8 @@
|
|
30
32
|
# -p, --pages:: Number of pages to convert.
|
31
33
|
# If given, +pdfmult+ does not try to obtain the page count from the source PDF.
|
32
34
|
#
|
35
|
+
# -l, --latex:: Create a LaTeX file instead of a PDF file (default: infile_NUMBER.tex).
|
36
|
+
#
|
33
37
|
# -h, --help:: Prints a brief help message and exits.
|
34
38
|
#
|
35
39
|
# -v, --version:: Prints a brief version information and exits.
|
@@ -57,8 +61,8 @@ require 'fileutils'
|
|
57
61
|
module Pdfmult
|
58
62
|
|
59
63
|
PROGNAME = 'pdfmult'
|
60
|
-
VERSION = '1.
|
61
|
-
DATE = '2012-03-
|
64
|
+
VERSION = '1.1.0'
|
65
|
+
DATE = '2012-03-16'
|
62
66
|
COPYRIGHT = "Copyright (C) 2011-2012 Marcus Stollsteimer.\n" +
|
63
67
|
"License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n" +
|
64
68
|
"This is free software: you are free to change and redistribute it.\n" +
|
@@ -83,8 +87,9 @@ module Pdfmult
|
|
83
87
|
options = {
|
84
88
|
:number => 2,
|
85
89
|
:infile => nil,
|
90
|
+
:latex => false,
|
86
91
|
:outfile => nil,
|
87
|
-
:pages
|
92
|
+
:pages => nil
|
88
93
|
}
|
89
94
|
|
90
95
|
opt_parser = OptionParser.new do |opt|
|
@@ -100,6 +105,11 @@ module Pdfmult
|
|
100
105
|
opt.separator 'if not, only the first page is processed'
|
101
106
|
opt.separator '(unless the page count was specified via command line option).'
|
102
107
|
opt.separator ''
|
108
|
+
opt.separator 'pdfmult uses pdflatex with the pdfpages package,'
|
109
|
+
opt.separator 'so both have to be installed on the system.'
|
110
|
+
opt.separator 'If the --latex option is used, though, pdflatex is not run'
|
111
|
+
opt.separator 'and a LaTeX file is created instead of a PDF.'
|
112
|
+
opt.separator ''
|
103
113
|
opt.separator 'Options'
|
104
114
|
opt.separator ''
|
105
115
|
|
@@ -135,6 +145,10 @@ module Pdfmult
|
|
135
145
|
options[:pages] = p
|
136
146
|
end
|
137
147
|
|
148
|
+
opt.on('-l', '--latex', 'Create a LaTeX file instead of a PDF file (default: file_2.tex).') do
|
149
|
+
options[:latex] = true
|
150
|
+
end
|
151
|
+
|
138
152
|
opt.separator ''
|
139
153
|
end
|
140
154
|
opt_parser.parse!(argv)
|
@@ -145,7 +159,8 @@ module Pdfmult
|
|
145
159
|
options[:infile] = argv.pop
|
146
160
|
|
147
161
|
# set output file unless set by option
|
148
|
-
|
162
|
+
ext = options[:latex] ? 'tex' : 'pdf'
|
163
|
+
options[:outfile] ||= options[:infile].gsub(/(.pdf)$/, '') + "_#{options[:number].to_s}.#{ext}"
|
149
164
|
|
150
165
|
options
|
151
166
|
end
|
@@ -272,19 +287,26 @@ module Pdfmult
|
|
272
287
|
usage_fail(e.message)
|
273
288
|
end
|
274
289
|
|
275
|
-
# tests
|
276
|
-
general_fail("`#{PDFLATEX}' seems not to be installed") unless command_available?("#{PDFLATEX} --version")
|
277
|
-
general_fail("`pdfpages.sty' seems not to be installed") unless command_available?("#{KPSEWHICH} pdfpages.sty")
|
278
|
-
|
279
|
-
# main body #
|
280
|
-
|
281
290
|
infile = options[:infile]
|
282
291
|
outfile = options[:outfile]
|
283
292
|
|
293
|
+
# test for pdflatex installation
|
294
|
+
unless options[:latex]
|
295
|
+
message = 'seems not to be installed (you might try using the -l option)'
|
296
|
+
general_fail("`#{PDFLATEX}' #{message}") unless command_available?("#{PDFLATEX} --version")
|
297
|
+
general_fail("`pdfpages.sty' #{message}") unless command_available?("#{KPSEWHICH} pdfpages.sty")
|
298
|
+
end
|
299
|
+
|
284
300
|
# test input file
|
285
301
|
usage_fail("no such file: `#{infile}'") unless File.exist?(infile)
|
286
302
|
usage_fail("specified input not of the type `file'") unless File.ftype(infile) == 'file'
|
287
303
|
|
304
|
+
# test for existing output file
|
305
|
+
if File.exist?(outfile)
|
306
|
+
overwrite_ok = ask("File `#{outfile}' already exists. Overwrite?")
|
307
|
+
exit unless overwrite_ok
|
308
|
+
end
|
309
|
+
|
288
310
|
# set page number (get PDF info if necessary)
|
289
311
|
pages = options[:pages]
|
290
312
|
pages ||= PDFInfo.new(infile).page_count
|
@@ -293,15 +315,36 @@ module Pdfmult
|
|
293
315
|
# create LaTeX document
|
294
316
|
document = LaTeXDocument.new(infile, options[:number], pages)
|
295
317
|
|
296
|
-
|
297
|
-
open(
|
298
|
-
pdfpath = "#{dir}/pdfmult.pdf"
|
318
|
+
if options[:latex]
|
319
|
+
open(outfile, 'w') do |f|
|
299
320
|
f.write(document.to_s)
|
300
|
-
f.flush
|
301
|
-
system("/usr/bin/pdflatex -output-directory #{dir} pdfmult.tex")
|
302
|
-
puts "Writing on #{outfile}."
|
303
|
-
FileUtils::mv(pdfpath, outfile)
|
304
321
|
end
|
322
|
+
else
|
323
|
+
Dir.mktmpdir('pdfmult') do |dir|
|
324
|
+
open("#{dir}/pdfmult.tex", 'w') do |f|
|
325
|
+
pdfpath = "#{dir}/pdfmult.pdf"
|
326
|
+
f.write(document.to_s)
|
327
|
+
f.flush
|
328
|
+
system("#{PDFLATEX} -output-directory #{dir} pdfmult.tex")
|
329
|
+
puts "Writing on #{outfile}."
|
330
|
+
FileUtils::mv(pdfpath, outfile)
|
331
|
+
end
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
# Asks for yes or no (y/n).
|
337
|
+
#
|
338
|
+
# +question+ - string to be printed
|
339
|
+
#
|
340
|
+
# Returns +true+ if the answer is yes.
|
341
|
+
def self.ask(question) # :nodoc:
|
342
|
+
while true
|
343
|
+
print question + ' [y/n] '
|
344
|
+
reply = $stdin.gets.chomp.downcase # $stdin: avoids gets / ARGV problem
|
345
|
+
return true if reply == 'y'
|
346
|
+
return false if reply == 'n'
|
347
|
+
puts "Please answer `y' or `n'."
|
305
348
|
end
|
306
349
|
end
|
307
350
|
|
data/man/pdfmult.1
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
.\" DO NOT MODIFY THIS FILE! It was generated by help2man 1.40.4.
|
2
|
-
.TH PDFMULT "1" "March 2012" "pdfmult 1.
|
2
|
+
.TH PDFMULT "1" "March 2012" "pdfmult 1.1.0" "User Commands"
|
3
3
|
.SH NAME
|
4
4
|
pdfmult \- puts multiple copies of a PDF page on one page
|
5
5
|
.SH SYNOPSIS
|
@@ -15,6 +15,11 @@ The input PDF file may consist of several pages.
|
|
15
15
|
If pdfmult succeeds in obtaining the page count it will rearrange all pages,
|
16
16
|
if not, only the first page is processed
|
17
17
|
(unless the page count was specified via command line option).
|
18
|
+
.PP
|
19
|
+
pdfmult uses pdflatex with the pdfpages package,
|
20
|
+
so both have to be installed on the system.
|
21
|
+
If the \fB\-\-latex\fR option is used, though, pdflatex is not run
|
22
|
+
and a LaTeX file is created instead of a PDF.
|
18
23
|
.SH OPTIONS
|
19
24
|
.TP
|
20
25
|
\fB\-n\fR, \fB\-\-number\fR NUMBER
|
@@ -27,6 +32,9 @@ Output file (default: file_2.pdf).
|
|
27
32
|
Number of pages to convert.
|
28
33
|
If given, pdfmult does not try to obtain the page count from the source PDF.
|
29
34
|
.TP
|
35
|
+
\fB\-l\fR, \fB\-\-latex\fR
|
36
|
+
Create a LaTeX file instead of a PDF file (default: file_2.tex).
|
37
|
+
.TP
|
30
38
|
\fB\-h\fR, \fB\-\-help\fR
|
31
39
|
Prints a brief help message and exits.
|
32
40
|
.TP
|
data/pdfmult.gemspec
CHANGED
@@ -10,7 +10,8 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.rubyforge_project = 'pdfmult'
|
11
11
|
|
12
12
|
s.description = 'pdfmult is a command line tool that rearranges ' +
|
13
|
-
'multiple copies of a PDF page (shrunken) on one page.'
|
13
|
+
'multiple copies of a PDF page (shrunken) on one page. ' +
|
14
|
+
'It is a wrapper for pdflatex with the pdfpages package.'
|
14
15
|
s.summary = 'pdfmult - puts multiple copies of a PDF page on one page'
|
15
16
|
|
16
17
|
s.authors = ['Marcus Stollsteimer']
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# test_latex_document.rb: Unit tests for the pdfmult script.
|
3
|
+
#
|
4
|
+
# Copyright (C) 2011-2012 Marcus Stollsteimer
|
5
|
+
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'pdfmult'
|
9
|
+
|
10
|
+
|
11
|
+
describe Pdfmult::LaTeXDocument do
|
12
|
+
|
13
|
+
it 'should return the expected LaTeX code' do
|
14
|
+
document = Pdfmult::LaTeXDocument.new('sample.pdf', 8, 3)
|
15
|
+
document.to_s.split(/\n/)[0].must_equal "\\documentclass[a4paper,landscape]{article}"
|
16
|
+
document.to_s.split(/\n/)[-2].must_equal "\\includepdf[pages={3,3,3,3,3,3,3,3},nup=4x2]{sample.pdf}%"
|
17
|
+
end
|
18
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
#!/usr/bin/ruby -w
|
2
|
-
#
|
2
|
+
# test_optionparser.rb: Unit tests for the pdfmult script.
|
3
3
|
#
|
4
4
|
# Copyright (C) 2011-2012 Marcus Stollsteimer
|
5
5
|
|
@@ -7,11 +7,6 @@ require 'minitest/spec'
|
|
7
7
|
require 'minitest/autorun'
|
8
8
|
require 'pdfmult'
|
9
9
|
|
10
|
-
PROGNAME = 'test_pdfmult.rb'
|
11
|
-
PROGVERSION = '0.0.1'
|
12
|
-
|
13
|
-
SRCPATH = File.dirname(__FILE__)
|
14
|
-
|
15
10
|
|
16
11
|
describe Pdfmult::Optionparser do
|
17
12
|
|
@@ -21,7 +16,8 @@ describe Pdfmult::Optionparser do
|
|
21
16
|
:infile => 'sample.pdf',
|
22
17
|
:outfile => 'sample_2.pdf',
|
23
18
|
:number => 2,
|
24
|
-
:pages => nil
|
19
|
+
:pages => nil,
|
20
|
+
:latex => false
|
25
21
|
}
|
26
22
|
options.must_equal expected
|
27
23
|
end
|
@@ -46,6 +42,12 @@ describe Pdfmult::Optionparser do
|
|
46
42
|
options[:pages].must_equal 4
|
47
43
|
end
|
48
44
|
|
45
|
+
it 'should recognize the -l option and set the corresponding output filename' do
|
46
|
+
options = Pdfmult::Optionparser.parse!(['sample.pdf', '-l'])
|
47
|
+
options[:outfile].must_equal 'sample_2.tex'
|
48
|
+
options[:latex].must_equal true
|
49
|
+
end
|
50
|
+
|
49
51
|
it 'should only accept positive -p option values' do
|
50
52
|
lambda { Pdfmult::Optionparser.parse!(['sample.pdf', '-p', '0.5']) }.must_raise OptionParser::InvalidArgument
|
51
53
|
lambda { Pdfmult::Optionparser.parse!(['sample.pdf', '-p', '0']) }.must_raise OptionParser::InvalidArgument
|
@@ -62,38 +64,3 @@ describe Pdfmult::Optionparser do
|
|
62
64
|
lambda { Pdfmult::Optionparser.parse!(['-x']) }.must_raise OptionParser::InvalidOption
|
63
65
|
end
|
64
66
|
end
|
65
|
-
|
66
|
-
|
67
|
-
describe Pdfmult::LaTeXDocument do
|
68
|
-
|
69
|
-
it 'should return the expected LaTeX code' do
|
70
|
-
document = Pdfmult::LaTeXDocument.new('sample.pdf', 8, 3)
|
71
|
-
document.to_s.split(/\n/)[0].must_equal "\\documentclass[a4paper,landscape]{article}"
|
72
|
-
document.to_s.split(/\n/)[-2].must_equal "\\includepdf[pages={3,3,3,3,3,3,3,3},nup=4x2]{sample.pdf}%"
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
describe Pdfmult::PDFInfo do
|
78
|
-
|
79
|
-
before do
|
80
|
-
@sample_pdf = File.expand_path("#{SRCPATH}/sample.pdf")
|
81
|
-
end
|
82
|
-
|
83
|
-
describe 'when asked about the page count' do
|
84
|
-
it 'should return the page count for existing file and system tool' do
|
85
|
-
infocmd = Pdfmult::PDFInfo::PDFINFOCMD
|
86
|
-
skip("Skipped: `#{infocmd}' not available on the system") unless Pdfmult::PDFInfo.infocmd_available?
|
87
|
-
Pdfmult::PDFInfo.new(@sample_pdf).page_count.must_equal 3
|
88
|
-
Pdfmult::PDFInfo.new(@sample_pdf, :pdfinfocmd => infocmd).page_count.must_equal 3
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'should return nil for non-existent files' do
|
92
|
-
Pdfmult::PDFInfo.new('not_a_file.pdf').page_count.must_be_nil
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should return nil for non-existent `pdfinfo' system tool" do
|
96
|
-
Pdfmult::PDFInfo.new(@sample_pdf, :pdfinfocmd => 'not_a_command').page_count.must_be_nil
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
# test_pdfinfo.rb: Unit tests for the pdfmult script.
|
3
|
+
#
|
4
|
+
# Copyright (C) 2011-2012 Marcus Stollsteimer
|
5
|
+
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'pdfmult'
|
9
|
+
|
10
|
+
SRCPATH = File.dirname(__FILE__)
|
11
|
+
|
12
|
+
|
13
|
+
describe Pdfmult::PDFInfo do
|
14
|
+
|
15
|
+
before do
|
16
|
+
@sample_pdf = File.expand_path("#{SRCPATH}/sample.pdf")
|
17
|
+
end
|
18
|
+
|
19
|
+
describe 'when asked about the page count' do
|
20
|
+
it 'should return the page count for existing file and system tool' do
|
21
|
+
infocmd = Pdfmult::PDFInfo::PDFINFOCMD
|
22
|
+
skip("Skipped: `#{infocmd}' not available on the system") unless Pdfmult::PDFInfo.infocmd_available?
|
23
|
+
Pdfmult::PDFInfo.new(@sample_pdf).page_count.must_equal 3
|
24
|
+
Pdfmult::PDFInfo.new(@sample_pdf, :pdfinfocmd => infocmd).page_count.must_equal 3
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should return nil for non-existent files' do
|
28
|
+
Pdfmult::PDFInfo.new('not_a_file.pdf').page_count.must_be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return nil for non-existent `pdfinfo' system tool" do
|
32
|
+
Pdfmult::PDFInfo.new(@sample_pdf, :pdfinfocmd => 'not_a_command').page_count.must_be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdfmult
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 1.0.0
|
10
|
+
version: 1.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Marcus Stollsteimer
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-03-
|
18
|
+
date: 2012-03-16 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rake
|
@@ -45,7 +45,7 @@ dependencies:
|
|
45
45
|
version: "0"
|
46
46
|
type: :development
|
47
47
|
version_requirements: *id002
|
48
|
-
description: pdfmult is a command line tool that rearranges multiple copies of a PDF page (shrunken) on one page.
|
48
|
+
description: pdfmult is a command line tool that rearranges multiple copies of a PDF page (shrunken) on one page. It is a wrapper for pdflatex with the pdfpages package.
|
49
49
|
email: sto.mar@web.de
|
50
50
|
executables:
|
51
51
|
- pdfmult
|
@@ -65,8 +65,10 @@ files:
|
|
65
65
|
- bin/pdfmult
|
66
66
|
- lib/pdfmult.rb
|
67
67
|
- man/pdfmult.1
|
68
|
+
- test/test_pdfinfo.rb
|
69
|
+
- test/test_latex_document.rb
|
68
70
|
- test/sample.tex
|
69
|
-
- test/
|
71
|
+
- test/test_optionparser.rb
|
70
72
|
- test/sample.pdf
|
71
73
|
homepage: https://github.com/stomar/pdfmult/
|
72
74
|
licenses:
|
@@ -102,4 +104,6 @@ signing_key:
|
|
102
104
|
specification_version: 3
|
103
105
|
summary: pdfmult - puts multiple copies of a PDF page on one page
|
104
106
|
test_files:
|
105
|
-
- test/
|
107
|
+
- test/test_pdfinfo.rb
|
108
|
+
- test/test_latex_document.rb
|
109
|
+
- test/test_optionparser.rb
|