Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
/*
2
* PDF cleaning tool: general purpose pdf syntax washer.
3
*
4
* Rewrite PDF with pretty printed objects.
5
* Garbage collect unreachable objects.
6
* Inflate compressed streams.
7
* Create subset documents.
8
*
9
* TODO: linearize document for fast web view
10
*/
11
12
#include "mupdf/pdf.h"
13
14
static void usage(void)
15
{
16
fprintf(stderr,
17
"usage: mutool clean [options] input.pdf [output.pdf] [pages]\n"
18
"\t-p -\tpassword\n"
19
"\t-g\tgarbage collect unused objects\n"
20
"\t-gg\tin addition to -g compact xref table\n"
21
"\t-ggg\tin addition to -gg merge duplicate objects\n"
22
"\t-s\tclean content streams\n"
23
"\t-d\tdecompress all streams\n"
24
"\t-l\tlinearize PDF\n"
25
"\t-i\ttoggle decompression of image streams\n"
26
"\t-f\ttoggle decompression of font streams\n"
27
"\t-a\tascii hex encode binary streams\n"
28
"\tpages\tcomma separated list of page numbers and ranges\n"
29
);
30
exit(1);
31
}
32
33
int pdfclean_main(int argc, char **argv)
34
{
35
char *infile;
36
char *outfile = "out.pdf";
37
char *password = "";
38
int c;
39
fz_write_options opts;
40
int errors = 0;
41
fz_context *ctx;
42
43
opts.do_incremental = 0;
44
opts.do_garbage = 0;
45
opts.do_expand = 0;
46
opts.do_ascii = 0;
47
opts.do_linear = 0;
48
opts.continue_on_error = 1;
49
opts.errors = &errors;
50
opts.do_clean = 0;
51
52
while ((c = fz_getopt(argc, argv, "adfgilp:s")) != -1)
53
{
54
switch (c)
55
{
56
case 'p': password = fz_optarg; break;
57
case 'g': opts.do_garbage ++; break;
58
case 'd': opts.do_expand ^= fz_expand_all; break;
59
case 'f': opts.do_expand ^= fz_expand_fonts; break;
60
case 'i': opts.do_expand ^= fz_expand_images; break;
61
case 'l': opts.do_linear ++; break;
62
case 'a': opts.do_ascii ++; break;
63
case 's': opts.do_clean ++; break;
64
default: usage(); break;
65
}
66
}
67
68
if (argc - fz_optind < 1)
69
usage();
70
71
infile = argv[fz_optind++];
72
73
if (argc - fz_optind > 0 &&
74
(strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF")))
75
{
76
outfile = argv[fz_optind++];
77
}
78
79
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
80
if (!ctx)
81
{
82
fprintf(stderr, "cannot initialise context\n");
83
exit(1);
84
}
85
86
fz_try(ctx)
87
{
88
pdf_clean_file(ctx, infile, outfile, password, &opts, &argv[fz_optind], argc - fz_optind);
89
}
90
fz_catch(ctx)
91
{
92
errors++;
93
}
94
fz_drop_context(ctx);
95
96
return errors != 0;
97
}
98
99