Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7643 views
1
/*
2
* PDF posteriser; split pages within a PDF file into smaller lumps.
3
*/
4
5
#include "mupdf/pdf.h"
6
7
static int x_factor = 0;
8
static int y_factor = 0;
9
10
static void usage(void)
11
{
12
fprintf(stderr,
13
"usage: mutool poster [options] input.pdf [output.pdf]\n"
14
"\t-p -\tpassword\n"
15
"\t-x\tx decimation factor\n"
16
"\t-y\ty decimation factor\n");
17
exit(1);
18
}
19
20
/*
21
* Recreate page tree with our posterised pages in.
22
*/
23
24
static void decimatepages(fz_context *ctx, pdf_document *doc)
25
{
26
pdf_obj *oldroot, *root, *pages, *kids, *parent;
27
int num_pages = pdf_count_pages(ctx, doc);
28
int page, kidcount;
29
30
oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME_Root);
31
pages = pdf_dict_get(ctx, oldroot, PDF_NAME_Pages);
32
33
root = pdf_new_dict(ctx, doc, 2);
34
pdf_dict_put(ctx, root, PDF_NAME_Type, pdf_dict_get(ctx, oldroot, PDF_NAME_Type));
35
pdf_dict_put(ctx, root, PDF_NAME_Pages, pdf_dict_get(ctx, oldroot, PDF_NAME_Pages));
36
37
pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root);
38
39
pdf_drop_obj(ctx, root);
40
41
/* Create a new kids array with our new pages in */
42
parent = pdf_new_indirect(ctx, doc, pdf_to_num(ctx, pages), pdf_to_gen(ctx, pages));
43
kids = pdf_new_array(ctx, doc, 1);
44
45
kidcount = 0;
46
for (page=0; page < num_pages; page++)
47
{
48
pdf_page *page_details = pdf_load_page(ctx, doc, page);
49
int xf = x_factor, yf = y_factor;
50
int x, y;
51
float w = page_details->mediabox.x1 - page_details->mediabox.x0;
52
float h = page_details->mediabox.y1 - page_details->mediabox.y0;
53
54
if (xf == 0 && yf == 0)
55
{
56
/* Nothing specified, so split along the long edge */
57
if (w > h)
58
xf = 2, yf = 1;
59
else
60
xf = 1, yf = 2;
61
}
62
else if (xf == 0)
63
xf = 1;
64
else if (yf == 0)
65
yf = 1;
66
67
for (y = yf-1; y >= 0; y--)
68
{
69
for (x = 0; x < xf; x++)
70
{
71
pdf_obj *newpageobj, *newpageref, *newmediabox;
72
fz_rect mb;
73
int num;
74
75
newpageobj = pdf_copy_dict(ctx, pdf_lookup_page_obj(ctx, doc, page));
76
num = pdf_create_object(ctx, doc);
77
pdf_update_object(ctx, doc, num, newpageobj);
78
newpageref = pdf_new_indirect(ctx, doc, num, 0);
79
80
newmediabox = pdf_new_array(ctx, doc, 4);
81
82
mb.x0 = page_details->mediabox.x0 + (w/xf)*x;
83
if (x == xf-1)
84
mb.x1 = page_details->mediabox.x1;
85
else
86
mb.x1 = page_details->mediabox.x0 + (w/xf)*(x+1);
87
mb.y0 = page_details->mediabox.y0 + (h/yf)*y;
88
if (y == yf-1)
89
mb.y1 = page_details->mediabox.y1;
90
else
91
mb.y1 = page_details->mediabox.y0 + (h/yf)*(y+1);
92
93
pdf_array_push(ctx, newmediabox, pdf_new_real(ctx, doc, mb.x0));
94
pdf_array_push(ctx, newmediabox, pdf_new_real(ctx, doc, mb.y0));
95
pdf_array_push(ctx, newmediabox, pdf_new_real(ctx, doc, mb.x1));
96
pdf_array_push(ctx, newmediabox, pdf_new_real(ctx, doc, mb.y1));
97
98
pdf_dict_put(ctx, newpageobj, PDF_NAME_Parent, parent);
99
pdf_dict_put(ctx, newpageobj, PDF_NAME_MediaBox, newmediabox);
100
101
/* Store page object in new kids array */
102
pdf_array_push(ctx, kids, newpageref);
103
104
kidcount++;
105
}
106
}
107
}
108
109
pdf_drop_obj(ctx, parent);
110
111
/* Update page count and kids array */
112
pdf_dict_put(ctx, pages, PDF_NAME_Count, pdf_new_int(ctx, doc, kidcount));
113
pdf_dict_put(ctx, pages, PDF_NAME_Kids, kids);
114
pdf_drop_obj(ctx, kids);
115
}
116
117
int pdfposter_main(int argc, char **argv)
118
{
119
char *infile;
120
char *outfile = "out.pdf";
121
char *password = "";
122
int c;
123
fz_write_options opts = { 0 };
124
pdf_document *doc;
125
fz_context *ctx;
126
127
opts.do_incremental = 0;
128
opts.do_garbage = 0;
129
opts.do_expand = 0;
130
opts.do_ascii = 0;
131
opts.do_linear = 0;
132
133
while ((c = fz_getopt(argc, argv, "x:y:")) != -1)
134
{
135
switch (c)
136
{
137
case 'p': password = fz_optarg; break;
138
case 'x': x_factor = atoi(fz_optarg); break;
139
case 'y': y_factor = atoi(fz_optarg); break;
140
default: usage(); break;
141
}
142
}
143
144
if (argc - fz_optind < 1)
145
usage();
146
147
infile = argv[fz_optind++];
148
149
if (argc - fz_optind > 0 &&
150
(strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF")))
151
{
152
outfile = argv[fz_optind++];
153
}
154
155
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
156
if (!ctx)
157
{
158
fprintf(stderr, "cannot initialise context\n");
159
exit(1);
160
}
161
162
doc = pdf_open_document(ctx, infile);
163
if (pdf_needs_password(ctx, doc))
164
if (!pdf_authenticate_password(ctx, doc, password))
165
fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile);
166
167
decimatepages(ctx, doc);
168
169
pdf_write_document(ctx, doc, outfile, &opts);
170
171
pdf_close_document(ctx, doc);
172
fz_drop_context(ctx);
173
return 0;
174
}
175
176