pdfclean.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. * PDF cleaning tool: general purpose pdf syntax washer.
  24. *
  25. * Rewrite PDF with pretty printed objects.
  26. * Garbage collect unreachable objects.
  27. * Inflate compressed streams.
  28. * Create subset documents.
  29. *
  30. * TODO: linearize document for fast web view
  31. */
  32. #include "mupdf/fitz.h"
  33. #include "mupdf/pdf.h"
  34. #include <string.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. static int usage(void)
  38. {
  39. fprintf(stderr,
  40. "usage: mutool clean [options] input.pdf [output.pdf] [pages]\n"
  41. "\t-p -\tpassword\n"
  42. "\t-g\tgarbage collect unused objects\n"
  43. "\t-gg\tin addition to -g compact xref table\n"
  44. "\t-ggg\tin addition to -gg merge duplicate objects\n"
  45. "\t-gggg\tin addition to -ggg check streams for duplication\n"
  46. "\t-l\tlinearize PDF (no longer supported!)\n"
  47. "\t-D\tsave file without encryption\n"
  48. "\t-E -\tsave file with new encryption (rc4-40, rc4-128, aes-128, or aes-256)\n"
  49. "\t-O -\towner password (only if encrypting)\n"
  50. "\t-U -\tuser password (only if encrypting)\n"
  51. "\t-P -\tpermission flags (only if encrypting)\n"
  52. "\t-a\tascii hex encode binary streams\n"
  53. "\t-d\tdecompress streams\n"
  54. "\t-z\tdeflate uncompressed streams\n"
  55. "\t-e -\tcompression \"effort\" (0 = default, 1 = min, 100 = max)\n"
  56. "\t-f\tcompress font streams\n"
  57. "\t-i\tcompress image streams\n"
  58. "\t-c\tclean content streams\n"
  59. "\t-s\tsanitize content streams\n"
  60. "\t-t\tcompact object syntax\n"
  61. "\t-tt\tindented object syntax\n"
  62. "\t-L\twrite object labels\n"
  63. "\t-A\tcreate appearance streams for annotations\n"
  64. "\t-AA\trecreate appearance streams for annotations\n"
  65. "\t-m\tpreserve metadata\n"
  66. "\t-S\tsubset fonts if possible [EXPERIMENTAL!]\n"
  67. "\t-Z\tuse objstms if possible for extra compression\n"
  68. "\t--{color,gray,bitonal}-{,lossy-,lossless-}image-subsample-method -\n\t\taverage, bicubic\n"
  69. "\t--{color,gray,bitonal}-{,lossy-,lossless-}image-subsample-dpi -[,-]\n\t\tDPI at which to subsample [+ target dpi]\n"
  70. "\t--{color,gray,bitonal}-{,lossy-,lossless-}image-recompress-method -[:quality]\n\t\tnever, same, lossless, jpeg, j2k, fax, jbig2\n"
  71. "\t--structure=keep|drop\tKeep or drop the structure tree\n"
  72. "\tpages\tcomma separated list of page numbers and ranges\n"
  73. );
  74. return 1;
  75. }
  76. static int encrypt_method_from_string(const char *name)
  77. {
  78. if (!strcmp(name, "rc4-40")) return PDF_ENCRYPT_RC4_40;
  79. if (!strcmp(name, "rc4-128")) return PDF_ENCRYPT_RC4_128;
  80. if (!strcmp(name, "aes-128")) return PDF_ENCRYPT_AES_128;
  81. if (!strcmp(name, "aes-256")) return PDF_ENCRYPT_AES_256;
  82. return PDF_ENCRYPT_UNKNOWN;
  83. }
  84. int pdfclean_main(int argc, char **argv)
  85. {
  86. char *infile;
  87. char *outfile = "out.pdf";
  88. char *password = "";
  89. int c;
  90. int pretty = -1;
  91. pdf_clean_options opts = { 0 };
  92. int errors = 0;
  93. fz_context *ctx;
  94. int structure;
  95. const fz_getopt_long_options longopts[] =
  96. {
  97. { "color-lossy-image-subsample-method=average|bicubic", &opts.image.color_lossy_image_subsample_method, (void *)1 },
  98. { "color-lossless-image-subsample-method=average|bicubic", &opts.image.color_lossless_image_subsample_method, (void *)2 },
  99. { "color-image-subsample-method=average|bicubic", &opts.image.color_lossy_image_subsample_method, (void *)3 },
  100. { "color-lossy-image-subsample-dpi:", &opts.image.color_lossy_image_subsample_threshold, (void *)4 },
  101. { "color-lossless-image-subsample-dpi:", &opts.image.color_lossless_image_subsample_threshold, (void *)5 },
  102. { "color-image-subsample-dpi:", &opts.image.color_lossless_image_subsample_threshold, (void *)6 },
  103. { "color-lossy-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.color_lossy_image_recompress_method, (void *)7 },
  104. { "color-lossless-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.color_lossless_image_recompress_method, (void *)8 },
  105. { "color-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.color_lossless_image_recompress_method, (void *)9 },
  106. { "gray-lossy-image-subsample-method=average|bicubic", &opts.image.gray_lossy_image_subsample_method, (void *)10 },
  107. { "gray-lossless-image-subsample-method=average|bicubic", &opts.image.gray_lossless_image_subsample_method, (void *)11 },
  108. { "gray-image-subsample-method=average|bicubic", &opts.image.gray_lossy_image_subsample_method, (void *)12 },
  109. { "gray-lossy-image-subsample-dpi:", &opts.image.gray_lossy_image_subsample_threshold, (void *)13 },
  110. { "gray-lossless-image-subsample-dpi:", &opts.image.gray_lossless_image_subsample_threshold, (void *)14 },
  111. { "gray-image-subsample-dpi:", &opts.image.gray_lossless_image_subsample_threshold, (void *)15 },
  112. { "gray-lossy-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.gray_lossy_image_recompress_method, (void *)16 },
  113. { "gray-lossless-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.gray_lossless_image_recompress_method, (void *)17 },
  114. { "gray-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.gray_lossless_image_recompress_method, (void *)18 },
  115. { "bitonal-image-subsample-method=average|bicubic", &opts.image.bitonal_image_subsample_method, (void *)19 },
  116. { "bitonal-image-subsample-dpi:", &opts.image.bitonal_image_subsample_threshold, (void *)20 },
  117. { "bitonal-image-recompress-method=never|same|lossless|jpeg:|j2k:|fax|jbig2", &opts.image.bitonal_image_recompress_method, (void *)21 },
  118. { "structure=drop|keep", &structure, (void *)22 },
  119. { NULL, NULL, NULL }
  120. };
  121. opts.write = pdf_default_write_options;
  122. opts.write.dont_regenerate_id = 1;
  123. while ((c = fz_getopt_long(argc, argv, "ade:fgilmp:stczDAE:LO:U:P:SZ", longopts)) != -1)
  124. {
  125. switch (c)
  126. {
  127. case 'p': password = fz_optarg; break;
  128. case 'd': opts.write.do_decompress += 1; break;
  129. case 'z': opts.write.do_compress += 1; break;
  130. case 'f': opts.write.do_compress_fonts += 1; break;
  131. case 'i': opts.write.do_compress_images += 1; break;
  132. case 'a': opts.write.do_ascii += 1; break;
  133. case 'e': opts.write.compression_effort = fz_atoi(fz_optarg); break;
  134. case 'g': opts.write.do_garbage += 1; break;
  135. case 'l': opts.write.do_linear += 1; break;
  136. case 'c': opts.write.do_clean += 1; break;
  137. case 's': opts.write.do_sanitize += 1; break;
  138. case 't': pretty = (pretty < 0) ? 0 : 1; break;
  139. case 'A': opts.write.do_appearance += 1; break;
  140. case 'L': opts.write.do_labels = 1; break;
  141. case 'D': opts.write.do_encrypt = PDF_ENCRYPT_NONE; break;
  142. case 'E': opts.write.do_encrypt = encrypt_method_from_string(fz_optarg); break;
  143. case 'P': opts.write.permissions = fz_atoi(fz_optarg); break;
  144. case 'O': fz_strlcpy(opts.write.opwd_utf8, fz_optarg, sizeof opts.write.opwd_utf8); break;
  145. case 'U': fz_strlcpy(opts.write.upwd_utf8, fz_optarg, sizeof opts.write.upwd_utf8); break;
  146. case 'm': opts.write.do_preserve_metadata = 1; break;
  147. case 'S': opts.subset_fonts = 1; break;
  148. case 'Z': opts.write.do_use_objstms = 1; break;
  149. case 0:
  150. {
  151. switch((int)(intptr_t)fz_optlong->opaque)
  152. {
  153. default:
  154. case 0:
  155. assert(!"Never happens");
  156. break;
  157. case 1: /* color-lossy-image-subsample-method */
  158. case 2: /* color-lossless-image-subsample-method */
  159. break;
  160. case 3: /* color-image-subsample-method */
  161. opts.image.color_lossless_image_subsample_method = opts.image.color_lossy_image_subsample_method;
  162. break;
  163. case 4: /* color-lossy-image-subsample-dpi */
  164. opts.image.color_lossy_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.color_lossy_image_subsample_threshold);
  165. break;
  166. case 5: /* color-lossless-image-subsample-dpi */
  167. opts.image.color_lossless_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.color_lossless_image_subsample_threshold);
  168. break;
  169. case 6: /* color-image-subsample-dpi */
  170. opts.image.color_lossless_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.color_lossless_image_subsample_threshold);
  171. opts.image.color_lossy_image_subsample_threshold = opts.image.color_lossless_image_subsample_threshold;
  172. opts.image.color_lossy_image_subsample_to = opts.image.color_lossless_image_subsample_to;
  173. break;
  174. case 7: /* color-lossy-image-recompress-method */
  175. opts.image.color_lossless_image_recompress_quality = fz_optarg;
  176. break;
  177. case 8: /* color-lossless-image-recompress-method */
  178. opts.image.color_lossy_image_recompress_quality = fz_optarg;
  179. break;
  180. case 9: /* color-image-recompress-method */
  181. opts.image.color_lossless_image_recompress_quality = fz_optarg;
  182. opts.image.color_lossy_image_recompress_method = opts.image.color_lossless_image_recompress_method;
  183. opts.image.color_lossy_image_recompress_quality = opts.image.color_lossless_image_recompress_quality;
  184. break;
  185. case 10: /* gray-lossy-image-subsample-method */
  186. case 11: /* gray-lossless-image-subsample-method */
  187. break;
  188. case 12: /* gray-image-subsample-method */
  189. opts.image.gray_lossless_image_subsample_method = opts.image.gray_lossy_image_subsample_method;
  190. break;
  191. case 13: /* gray-lossy-image-subsample-dpi */
  192. opts.image.gray_lossy_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.gray_lossless_image_subsample_threshold);
  193. break;
  194. case 14: /* gray-lossless-image-subsample-dpi */
  195. opts.image.gray_lossless_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.gray_lossless_image_subsample_threshold);
  196. break;
  197. case 15: /* gray-image-subsample-dpi */
  198. opts.image.gray_lossless_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.gray_lossy_image_subsample_threshold);
  199. opts.image.gray_lossy_image_subsample_threshold = opts.image.gray_lossless_image_subsample_threshold;
  200. opts.image.gray_lossy_image_subsample_to = opts.image.gray_lossless_image_subsample_to;
  201. break;
  202. case 16: /* gray-lossy-image-recompress-method */
  203. opts.image.gray_lossless_image_recompress_quality = fz_optarg;
  204. break;
  205. case 17: /* gray-lossless-image-recompress-method */
  206. opts.image.gray_lossy_image_recompress_quality = fz_optarg;
  207. break;
  208. case 18: /* gray-image-recompress-method */
  209. opts.image.gray_lossless_image_recompress_quality = fz_optarg;
  210. opts.image.gray_lossy_image_recompress_method = opts.image.gray_lossless_image_recompress_method;
  211. opts.image.gray_lossy_image_recompress_quality = opts.image.gray_lossless_image_recompress_quality;
  212. break;
  213. case 19: /* bitonal-image-subsample-method */
  214. break;
  215. case 20: /* bitonal-image-subsample-dpi */
  216. opts.image.bitonal_image_subsample_to = (fz_optarg ? fz_atoi(fz_optarg) : opts.image.bitonal_image_subsample_threshold);
  217. break;
  218. case 21: /* bitonal-image-recompress-method */
  219. opts.image.bitonal_image_recompress_quality = fz_optarg;
  220. if (fz_optarg)
  221. return usage();
  222. break;
  223. case 22: /* structure */
  224. opts.structure = structure; /* Allow for int/enum size mismatch. */
  225. break;
  226. }
  227. break;
  228. }
  229. default: return usage();
  230. }
  231. }
  232. if (pretty < 0)
  233. {
  234. if ((opts.write.do_ascii || opts.write.do_decompress) && !opts.write.do_compress)
  235. pretty = 1;
  236. else
  237. pretty = 0;
  238. }
  239. opts.write.do_pretty = pretty;
  240. if (argc - fz_optind < 1)
  241. return usage();
  242. infile = argv[fz_optind++];
  243. if (argc - fz_optind > 0 &&
  244. (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF")))
  245. {
  246. outfile = argv[fz_optind++];
  247. }
  248. ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
  249. if (!ctx)
  250. {
  251. fprintf(stderr, "cannot initialise context\n");
  252. exit(1);
  253. }
  254. if (opts.write.do_compress > 1)
  255. fz_warn(ctx, "Brotli compression is currently non-standard and experimental. Files may not be readable in other software.");
  256. fz_try(ctx)
  257. {
  258. pdf_clean_file(ctx, infile, outfile, password, &opts, argc - fz_optind, &argv[fz_optind]);
  259. }
  260. fz_catch(ctx)
  261. {
  262. fz_report_error(ctx);
  263. errors++;
  264. }
  265. fz_drop_context(ctx);
  266. return errors != 0;
  267. }