1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! `sealwax`: inspect PDF documents from the command line.
mod annots;
mod census;
mod corpus;
mod page;
mod pages;
mod pdfa;
mod save;
mod sign;
mod text;
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
use sealwax::{Document, OpenOptions, PageGeometry};
use serde::Serialize;
#[derive(Debug, Parser)]
#[command(
name = "sealwax",
version,
about = "Inspect PDF documents with sealwax"
)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
/// Print the page count and the geometry of every page, as JSON.
Info {
/// The PDF to read.
file: PathBuf,
/// Password of an encrypted document.
#[arg(long, default_value = "")]
password: String,
},
/// Survey every PDF under the given files or directories: one JSON line per document.
Census {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents surveyed at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Page count and geometry of every PDF under the given files or directories, one JSON line
/// per document.
Pages {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents read at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Save every PDF under the given files or directories, for the judges of
/// `benchmark/judges/`: outputs named by the SHA-256 of their input, one JSON line each.
Save {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// Where the saved PDFs go.
#[arg(long)]
out_dir: PathBuf,
/// How to save.
#[arg(long, value_enum, default_value = "stream")]
mode: save::Mode,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents saved at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Convert every PDF under the given files or directories to PDF/A-3: outputs named by the
/// SHA-256 of their input, one JSON line each with what was changed and what was left.
Pdfa {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// Where the converted PDFs go.
#[arg(long)]
out_dir: PathBuf,
/// The conformance level to declare.
#[arg(long, value_enum, default_value = "b")]
level: pdfa::Conformance,
/// At level a, the language of documents that declare none (`fr-CA`); else it is told
/// from their words when they leave no doubt.
#[arg(long)]
lang: Option<String>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents converted at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Flatten the annotations of every PDF under the given files or directories: outputs named by
/// the SHA-256 of their input, one JSON line each.
Flatten {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// Where the flattened PDFs go.
#[arg(long)]
out_dir: PathBuf,
/// Then convert to PDF/A-3 at this level.
#[arg(long, value_enum)]
pdfa: Option<pdfa::Conformance>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents processed at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Merge PDFs into one, in the given order, each flattened first.
Merge {
/// The PDFs, in order.
#[arg(required = true)]
inputs: Vec<PathBuf>,
/// The merged PDF.
#[arg(long)]
out: PathBuf,
},
/// Apply to every PDF under the given files or directories the XFDF `<xfdf-dir>/<sha256>.xfdf`
/// of the same SHA-256, flattening first, as the final PDF task does.
Xfdf {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// Where the XFDF are, named by the SHA-256 of their PDF.
#[arg(long)]
xfdf_dir: PathBuf,
/// Where the output PDFs go.
#[arg(long)]
out_dir: PathBuf,
/// Then convert to PDF/A-3 at this level.
#[arg(long, value_enum)]
pdfa: Option<pdfa::Conformance>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents processed at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// What the glyphs of every PDF under the given files or directories map to in Unicode (PDF/A
/// clauses 6.2.11.7.2 and 6.2.11.7.3), one JSON line per document.
TextCensus {
/// Files or directories (searched recursively for *.pdf).
#[arg(required = true)]
paths: Vec<PathBuf>,
/// JSON lines output.
#[arg(long)]
out: PathBuf,
/// Documents read at once.
#[arg(long, default_value_t = 8)]
jobs: usize,
},
/// Markdown report of a text-census run.
TextCensusReport {
/// The JSON lines a text-census run wrote.
results: PathBuf,
},
/// Markdown report of a census run.
CensusReport {
/// The JSON lines a census run wrote.
results: PathBuf,
},
/// Append a signature to a document, in an incremental revision: an invisible field on the
/// first page, a certification (DocMDP P=3) or an approval. Its value is made by the signer
/// command, as an HSM makes it: the digest to sign in hexadecimal on its standard input, the
/// signature value in hexadecimal on its standard output. With `--tsa`, the value is
/// timestamped (RFC 3161): the TSA command reads the request (DER) on its standard input and
/// writes the response (DER) on its standard output; what became of the timestamp is printed
/// as JSON.
Sign(Box<sign::Arguments>),
/// Add a signature's validation data (PAdES B-LT) in an incremental revision: a proof that
/// each certificate of the signer's and the timestamp authority's paths was not revoked, an
/// OCSP response or a CRL, checked, then written into the document security store with the
/// signature's VRI entry. The fetch command gets `ocsp URL` (the request, DER, on its standard
/// input) or `crl URL`, and writes the response or the CRL (DER) on its standard output. What
/// was added is printed as JSON.
Ltv(Box<sign::LtvArguments>),
/// Add a document timestamp (PAdES B-LTA) in an incremental revision: an RFC 3161 token over
/// the whole document, in an invisible field. The TSA command reads the request (DER) on its
/// standard input and writes the response (DER) on its standard output. Given after the
/// signatures' validation data, it keeps that data provable once the certificates expire.
Timestamp(Box<sign::TimestampArguments>),
/// Check the integrity of the signatures of each PDF: the byte range covers the revision,
/// the digest matches, the value verifies, and how many revisions followed. Whom the signers
/// are is not looked at. One JSON line per signature.
Verify {
/// The PDFs.
#[arg(required = true)]
files: Vec<PathBuf>,
},
/// Write the glyphs every page draws, in the order of its content: one JSON object per glyph
/// and line (its page from 1, its text, origin, advance, box, size, direction, font, and
/// whether it is invisible, drawn over itself, replaced by an /ActualText, or an
/// annotation's).
Glyphs {
/// The PDF to read.
file: PathBuf,
/// Also the glyphs the annotations' appearances draw.
#[arg(long)]
annotations: bool,
/// Where to write them (standard output without it).
#[arg(long)]
out: Option<PathBuf>,
/// Password of an encrypted document.
#[arg(long, default_value = "")]
password: String,
},
/// Render one page to a PNG image (8-bit RGB): its crop box as shown, annotations included.
Render {
/// The PDF to read.
file: PathBuf,
/// The page, from 1.
#[arg(long, default_value_t = 1)]
page: usize,
/// Pixels per point (150 / 72 = 2.0833 for 150 dpi).
#[arg(long, default_value_t = 1.0)]
scale: f64,
/// The PNG to write.
#[arg(long)]
out: PathBuf,
/// Password of an encrypted document.
#[arg(long, default_value = "")]
password: String,
},
}
#[derive(Debug, Serialize)]
struct Info {
page_count: usize,
pages: Vec<PageGeometry>,
}
fn main() -> ExitCode {
let cli = Cli::parse();
match cli.command {
Command::Sign(arguments) => match sign::run(&arguments) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Ltv(arguments) => match sign::run_ltv(&arguments) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Timestamp(arguments) => match sign::run_timestamp(&arguments) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Glyphs {
file,
annotations,
out,
password,
} => match page::glyphs(&file, &password, annotations, out.as_deref()) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Render {
file,
page,
scale,
out,
password,
} => match page::render(&file, &password, page, scale, &out) {
Ok(()) => ExitCode::SUCCESS,
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Verify { files } => match sign::run_verify(&files) {
Ok(true) => ExitCode::SUCCESS,
Ok(false) => ExitCode::from(2),
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Census { paths, out, jobs } => match census::run(&paths, &out, jobs) {
Ok(n) => {
eprintln!("sealwax: {n} documents surveyed into {}", out.display());
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Pages { paths, out, jobs } => match pages::run(&paths, &out, jobs) {
Ok(n) => {
eprintln!("sealwax: {n} documents measured into {}", out.display());
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Save {
paths,
out_dir,
mode,
out,
jobs,
} => match save::run(&paths, &out_dir, mode, &out, jobs) {
Ok(n) => {
eprintln!("sealwax: {n} documents saved into {}", out_dir.display());
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Pdfa {
paths,
out_dir,
level,
lang,
out,
jobs,
} => match pdfa::run(&paths, &out_dir, level, lang.as_deref(), &out, jobs) {
Ok(n) => {
eprintln!(
"sealwax: {n} documents converted into {}",
out_dir.display()
);
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Flatten {
paths,
out_dir,
pdfa,
out,
jobs,
} => match annots::run_flatten(&paths, &out_dir, pdfa, &out, jobs) {
Ok(n) => {
eprintln!(
"sealwax: {n} documents flattened into {}",
out_dir.display()
);
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Merge { inputs, out } => match annots::merge(&inputs, &out) {
Ok(pages) => {
eprintln!("sealwax: {pages} pages merged into {}", out.display());
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Xfdf {
paths,
xfdf_dir,
out_dir,
pdfa,
out,
jobs,
} => match annots::run_xfdf(&paths, &xfdf_dir, &out_dir, pdfa, &out, jobs) {
Ok(n) => {
eprintln!(
"sealwax: XFDF applied to {n} documents into {}",
out_dir.display()
);
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::TextCensus { paths, out, jobs } => match text::run(&paths, &out, jobs) {
Ok(n) => {
eprintln!("sealwax: {n} documents read into {}", out.display());
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::TextCensusReport { results } => match text::report(&results) {
Ok(report) => {
print!("{report}");
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::CensusReport { results } => match census::report(&results) {
Ok(report) => {
print!("{report}");
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
},
Command::Info { file, password } => {
match Document::open_path(&file, &OpenOptions::default().password(password)) {
Ok(doc) => {
let info = Info {
page_count: doc.page_count(),
pages: doc.geometry(),
};
match serde_json::to_string_pretty(&info) {
Ok(json) => {
println!("{json}");
ExitCode::SUCCESS
}
Err(error) => {
eprintln!("sealwax: {error}");
ExitCode::FAILURE
}
}
}
Err(error) => {
eprintln!("sealwax: {}: {error}", file.display());
ExitCode::FAILURE
}
}
}
}
}