comparison mupdf-source/source/tools/muconvert.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright (C) 2004-2025 Artifex Software, Inc.
2 //
3 // This file is part of MuPDF.
4 //
5 // MuPDF is free software: you can redistribute it and/or modify it under the
6 // terms of the GNU Affero General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or (at your option)
8 // any later version.
9 //
10 // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
13 // details.
14 //
15 // You should have received a copy of the GNU Affero General Public License
16 // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
17 //
18 // Alternative licensing terms are available from the licensor.
19 // For commercial licensing, see <https://www.artifex.com/> or contact
20 // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
21 // CA 94129, USA, for further information.
22
23 /*
24 * muconvert -- command line tool for converting documents
25 */
26
27 #include "mupdf/fitz.h"
28
29 #include <stdlib.h>
30 #include <stdio.h>
31
32 /* input options */
33 static const char *password = "";
34 static int alphabits = 8;
35 static float layout_w = FZ_DEFAULT_LAYOUT_W;
36 static float layout_h = FZ_DEFAULT_LAYOUT_H;
37 static float layout_em = FZ_DEFAULT_LAYOUT_EM;
38 static char *layout_css = NULL;
39 static int layout_use_doc_css = 1;
40
41 /* output options */
42 static const char *output = NULL;
43 static const char *format = NULL;
44 static const char *options = "";
45
46 static fz_context *ctx;
47 static fz_document *doc;
48 static fz_document_writer *out;
49 static fz_box_type page_box = FZ_CROP_BOX;
50 static int count;
51
52 static int usage(void)
53 {
54 fprintf(stderr,
55 "Usage: mutool convert [options] file [pages]\n"
56 "\t-p -\tpassword\n"
57 "\n"
58 "\t-b -\tuse named page box (MediaBox, CropBox, BleedBox, TrimBox, or ArtBox)\n"
59 "\t-A -\tnumber of bits of antialiasing (0 to 8)\n"
60 "\t-W -\tpage width for EPUB layout\n"
61 "\t-H -\tpage height for EPUB layout\n"
62 "\t-S -\tfont size for EPUB layout\n"
63 "\t-U -\tfile name of user stylesheet for EPUB layout\n"
64 "\t-X\tdisable document styles for EPUB layout\n"
65 "\n"
66 "\t-o -\toutput file name (%%d for page number)\n"
67 "\t-F -\toutput format (default inferred from output file name)\n"
68 "\t\t\traster: cbz, png, pnm, pgm, ppm, pam, pbm, pkm.\n"
69 "\t\t\tprint-raster: pcl, pclm, ps, pwg.\n"
70 "\t\t\tvector: pdf, svg.\n"
71 "\t\t\ttext: html, xhtml, text, stext.\n"
72 "\t-O -\tcomma separated list of options for output format\n"
73 "\n"
74 "\tpages\tcomma separated list of page ranges (N=last page)\n"
75 "\n"
76 );
77 fputs(fz_draw_options_usage, stderr);
78 fputs(fz_pcl_write_options_usage, stderr);
79 fputs(fz_pclm_write_options_usage, stderr);
80 fputs(fz_pwg_write_options_usage, stderr);
81 fputs(fz_stext_options_usage, stderr);
82 #if FZ_ENABLE_PDF
83 fputs(fz_pdf_write_options_usage, stderr);
84 #endif
85 fputs(fz_svg_write_options_usage, stderr);
86 return 1;
87 }
88
89 static void runpage(int number)
90 {
91 fz_rect box;
92 fz_page *page;
93 fz_device *dev = NULL;
94 fz_matrix ctm;
95
96 page = fz_load_page(ctx, doc, number - 1);
97
98 fz_var(dev);
99
100 fz_try(ctx)
101 {
102 box = fz_bound_page_box(ctx, page, page_box);
103
104 // Realign page box on 0,0
105 ctm = fz_translate(-box.x0, -box.y0);
106 box = fz_transform_rect(box, ctm);
107
108 dev = fz_begin_page(ctx, out, box);
109 fz_run_page(ctx, page, dev, ctm, NULL);
110 fz_end_page(ctx, out);
111 }
112 fz_always(ctx)
113 {
114 fz_drop_page(ctx, page);
115 }
116 fz_catch(ctx)
117 fz_rethrow(ctx);
118 }
119
120 static void runrange(const char *range)
121 {
122 int start, end, i;
123
124 while ((range = fz_parse_page_range(ctx, range, &start, &end, count)))
125 {
126 if (start < end)
127 for (i = start; i <= end; ++i)
128 runpage(i);
129 else
130 for (i = start; i >= end; --i)
131 runpage(i);
132 }
133 }
134
135 int muconvert_main(int argc, char **argv)
136 {
137 int i, c;
138 int retval = EXIT_SUCCESS;
139
140 while ((c = fz_getopt(argc, argv, "p:A:W:H:S:U:Xo:F:O:b:")) != -1)
141 {
142 switch (c)
143 {
144 default: return usage();
145
146 case 'p': password = fz_optarg; break;
147 case 'A': alphabits = atoi(fz_optarg); break;
148 case 'W': layout_w = fz_atof(fz_optarg); break;
149 case 'H': layout_h = fz_atof(fz_optarg); break;
150 case 'S': layout_em = fz_atof(fz_optarg); break;
151 case 'U': layout_css = fz_optarg; break;
152 case 'X': layout_use_doc_css = 0; break;
153
154 case 'o': output = fz_optpath(fz_optarg); break;
155 case 'F': format = fz_optarg; break;
156 case 'O': options = fz_optarg; break;
157
158 case 'b':
159 page_box = fz_box_type_from_string(fz_optarg);
160 if (page_box == FZ_UNKNOWN_BOX)
161 {
162 fprintf(stderr, "Invalid box type: %s\n", fz_optarg);
163 return 1;
164 }
165 break;
166 }
167 }
168
169 if (fz_optind == argc || (!format && !output))
170 return usage();
171
172 /* Create a context to hold the exception stack and various caches. */
173 ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
174 if (!ctx)
175 {
176 fprintf(stderr, "cannot create mupdf context\n");
177 return EXIT_FAILURE;
178 }
179
180 /* Register the default file types to handle. */
181 fz_try(ctx)
182 fz_register_document_handlers(ctx);
183 fz_catch(ctx)
184 {
185 fz_report_error(ctx);
186 fprintf(stderr, "cannot register document handlers\n");
187 fz_drop_context(ctx);
188 return EXIT_FAILURE;
189 }
190
191 fz_set_aa_level(ctx, alphabits);
192
193 if (layout_css)
194 fz_load_user_css(ctx, layout_css);
195
196 fz_set_use_document_css(ctx, layout_use_doc_css);
197
198 /* Open the output document. */
199 fz_try(ctx)
200 out = fz_new_document_writer(ctx, output, format, options);
201 fz_catch(ctx)
202 {
203 fz_report_error(ctx);
204 fprintf(stderr, "cannot create document\n");
205 fz_drop_context(ctx);
206 return EXIT_FAILURE;
207 }
208
209 fz_var(doc);
210 fz_try(ctx)
211 {
212 for (i = fz_optind; i < argc; ++i)
213 {
214 doc = fz_open_document(ctx, argv[i]);
215 if (fz_needs_password(ctx, doc))
216 if (!fz_authenticate_password(ctx, doc, password))
217 fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot authenticate password: %s", argv[i]);
218 fz_layout_document(ctx, doc, layout_w, layout_h, layout_em);
219 count = fz_count_pages(ctx, doc);
220
221 if (i+1 < argc && fz_is_page_range(ctx, argv[i+1]))
222 runrange(argv[++i]);
223 else
224 runrange("1-N");
225
226 fz_drop_document(ctx, doc);
227 doc = NULL;
228 }
229 fz_close_document_writer(ctx, out);
230 }
231 fz_always(ctx)
232 {
233 fz_drop_document(ctx, doc);
234 fz_drop_document_writer(ctx, out);
235 }
236 fz_catch(ctx)
237 {
238 fz_report_error(ctx);
239 retval = EXIT_FAILURE;
240 }
241
242 fz_drop_context(ctx);
243 return retval;
244 }