pdfposter.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright (C) 2004-2024 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 posteriser; split pages within a PDF file into smaller lumps.
  24. */
  25. #include "mupdf/fitz.h"
  26. #include "mupdf/pdf.h"
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. static int x_factor = 0;
  31. static int y_factor = 0;
  32. static float margin = 0;
  33. static int margin_is_percent = 0;
  34. static int x_dir = 1;
  35. static int usage(void)
  36. {
  37. fprintf(stderr,
  38. "usage: mutool poster [options] input.pdf [output.pdf]\n"
  39. "\t-p -\tpassword\n"
  40. "\t-m -\tmargin (overlap) between pages (pts, or %%)\n"
  41. "\t-x\tx decimation factor\n"
  42. "\t-y\ty decimation factor\n"
  43. "\t-r\tsplit right-to-left\n"
  44. );
  45. return 1;
  46. }
  47. static void
  48. intersect_box(fz_context *ctx, pdf_document *doc, pdf_obj *page, pdf_obj *box_name, fz_rect mb)
  49. {
  50. pdf_obj *box = pdf_dict_get(ctx, page, box_name);
  51. pdf_obj *newbox;
  52. fz_rect old_rect;
  53. if (box == NULL)
  54. return;
  55. old_rect.x0 = pdf_array_get_real(ctx, box, 0);
  56. old_rect.y0 = pdf_array_get_real(ctx, box, 1);
  57. old_rect.x1 = pdf_array_get_real(ctx, box, 2);
  58. old_rect.y1 = pdf_array_get_real(ctx, box, 3);
  59. if (old_rect.x0 < mb.x0)
  60. old_rect.x0 = mb.x0;
  61. if (old_rect.y0 < mb.y0)
  62. old_rect.y0 = mb.y0;
  63. if (old_rect.x1 > mb.x1)
  64. old_rect.x1 = mb.x1;
  65. if (old_rect.y1 > mb.y1)
  66. old_rect.y1 = mb.y1;
  67. newbox = pdf_new_array(ctx, doc, 4);
  68. pdf_array_push_real(ctx, newbox, old_rect.x0);
  69. pdf_array_push_real(ctx, newbox, old_rect.y0);
  70. pdf_array_push_real(ctx, newbox, old_rect.x1);
  71. pdf_array_push_real(ctx, newbox, old_rect.y1);
  72. pdf_dict_put_drop(ctx, page, box_name, newbox);
  73. }
  74. /*
  75. * Recreate page tree with our posterised pages in.
  76. */
  77. static void decimatepages(fz_context *ctx, pdf_document *doc)
  78. {
  79. pdf_obj *oldroot, *root, *pages, *kids;
  80. int num_pages = pdf_count_pages(ctx, doc);
  81. int page, kidcount;
  82. fz_rect mediabox, cropbox;
  83. int rotate;
  84. oldroot = pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root));
  85. pages = pdf_dict_get(ctx, oldroot, PDF_NAME(Pages));
  86. root = pdf_new_dict(ctx, doc, 2);
  87. pdf_dict_put(ctx, root, PDF_NAME(Type), pdf_dict_get(ctx, oldroot, PDF_NAME(Type)));
  88. pdf_dict_put(ctx, root, PDF_NAME(Pages), pdf_dict_get(ctx, oldroot, PDF_NAME(Pages)));
  89. pdf_update_object(ctx, doc, pdf_to_num(ctx, oldroot), root);
  90. pdf_drop_obj(ctx, root);
  91. /* Create a new kids array with our new pages in */
  92. kids = pdf_new_array(ctx, doc, 1);
  93. kidcount = 0;
  94. for (page=0; page < num_pages; page++)
  95. {
  96. pdf_obj *page_obj = pdf_lookup_page_obj(ctx, doc, page);
  97. int xf = x_factor, yf = y_factor;
  98. float w, h;
  99. int x, y;
  100. int xd, yd, y0, y1;
  101. float xmargin, ymargin;
  102. rotate = pdf_to_int(ctx, pdf_dict_get_inheritable(ctx, page_obj, PDF_NAME(Rotate)));
  103. mediabox = pdf_to_rect(ctx, pdf_dict_get_inheritable(ctx, page_obj, PDF_NAME(MediaBox)));
  104. cropbox = pdf_to_rect(ctx, pdf_dict_get_inheritable(ctx, page_obj, PDF_NAME(CropBox)));
  105. if (fz_is_empty_rect(mediabox))
  106. mediabox = fz_make_rect(0, 0, 612, 792);
  107. if (!fz_is_empty_rect(cropbox))
  108. mediabox = fz_intersect_rect(mediabox, cropbox);
  109. w = mediabox.x1 - mediabox.x0;
  110. h = mediabox.y1 - mediabox.y0;
  111. if (rotate == 90 || rotate == 270)
  112. {
  113. xf = y_factor;
  114. yf = x_factor;
  115. yd = -x_dir;
  116. xd = 1;
  117. }
  118. else
  119. {
  120. xf = x_factor;
  121. yf = y_factor;
  122. xd = x_dir;
  123. yd = -1;
  124. }
  125. if (xf == 0 && yf == 0)
  126. {
  127. /* Nothing specified, so split along the long edge */
  128. if (w > h)
  129. xf = 2, yf = 1;
  130. else
  131. xf = 1, yf = 2;
  132. }
  133. else if (xf == 0)
  134. xf = 1;
  135. else if (yf == 0)
  136. yf = 1;
  137. xmargin = xf == 1 ? 0 : margin * (margin_is_percent ? .001 * w/xf : 1);
  138. ymargin = yf == 1 ? 0 : margin * (margin_is_percent ? .001 * h/yf : 1);
  139. y0 = (yd > 0) ? 0 : yf-1;
  140. y1 = (yd > 0) ? yf : -1;
  141. for (y = y0; y != y1; y += yd)
  142. {
  143. int x0 = (xd > 0) ? 0 : xf-1;
  144. int x1 = (xd > 0) ? xf : -1;
  145. for (x = x0; x != x1; x += xd)
  146. {
  147. pdf_obj *newpageobj, *newpageref, *newmediabox;
  148. fz_rect mb;
  149. newpageobj = pdf_copy_dict(ctx, pdf_lookup_page_obj(ctx, doc, page));
  150. pdf_flatten_inheritable_page_items(ctx, newpageobj);
  151. newpageref = pdf_add_object(ctx, doc, newpageobj);
  152. newmediabox = pdf_new_array(ctx, doc, 4);
  153. mb.x0 = mediabox.x0 + (w/xf)*x;
  154. if (x == xf-1)
  155. mb.x1 = mediabox.x1;
  156. else
  157. mb.x1 = mb.x0 + (w/xf) + xmargin;
  158. mb.y0 = mediabox.y0 + (h/yf)*y;
  159. if (y == yf-1)
  160. mb.y1 = mediabox.y1;
  161. else
  162. mb.y1 = mb.y0 + (h/yf) + ymargin;
  163. pdf_array_push_real(ctx, newmediabox, mb.x0);
  164. pdf_array_push_real(ctx, newmediabox, mb.y0);
  165. pdf_array_push_real(ctx, newmediabox, mb.x1);
  166. pdf_array_push_real(ctx, newmediabox, mb.y1);
  167. pdf_dict_put(ctx, newpageobj, PDF_NAME(Parent), pages);
  168. pdf_dict_put_drop(ctx, newpageobj, PDF_NAME(MediaBox), newmediabox);
  169. intersect_box(ctx, doc, newpageobj, PDF_NAME(CropBox), mb);
  170. intersect_box(ctx, doc, newpageobj, PDF_NAME(BleedBox), mb);
  171. intersect_box(ctx, doc, newpageobj, PDF_NAME(TrimBox), mb);
  172. intersect_box(ctx, doc, newpageobj, PDF_NAME(ArtBox), mb);
  173. /* Store page object in new kids array */
  174. pdf_drop_obj(ctx, newpageobj);
  175. pdf_array_push_drop(ctx, kids, newpageref);
  176. kidcount++;
  177. }
  178. }
  179. }
  180. /* Update page count and kids array */
  181. pdf_dict_put_int(ctx, pages, PDF_NAME(Count), kidcount);
  182. pdf_dict_put_drop(ctx, pages, PDF_NAME(Kids), kids);
  183. }
  184. int pdfposter_main(int argc, char **argv)
  185. {
  186. char *infile;
  187. char *outfile = "out.pdf";
  188. char *password = "";
  189. int c;
  190. pdf_write_options opts = pdf_default_write_options;
  191. pdf_document *doc;
  192. fz_context *ctx;
  193. int ret = 0;
  194. while ((c = fz_getopt(argc, argv, "m:x:y:p:r")) != -1)
  195. {
  196. switch (c)
  197. {
  198. case 'p': password = fz_optarg; break;
  199. case 'm': margin = atof(fz_optarg); margin_is_percent = (strchr(fz_optarg, '%') != NULL); break;
  200. case 'x': x_factor = atoi(fz_optarg); break;
  201. case 'y': y_factor = atoi(fz_optarg); break;
  202. case 'r': x_dir = -1; break;
  203. default: return usage();
  204. }
  205. }
  206. if (argc - fz_optind < 1)
  207. return usage();
  208. infile = argv[fz_optind++];
  209. if (argc - fz_optind > 0 &&
  210. (strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF")))
  211. {
  212. outfile = argv[fz_optind++];
  213. }
  214. ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
  215. if (!ctx)
  216. {
  217. fprintf(stderr, "cannot initialise context\n");
  218. exit(1);
  219. }
  220. fz_var(doc);
  221. fz_try(ctx)
  222. {
  223. doc = pdf_open_document(ctx, infile);
  224. if (pdf_needs_password(ctx, doc))
  225. if (!pdf_authenticate_password(ctx, doc, password))
  226. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot authenticate password: %s", infile);
  227. decimatepages(ctx, doc);
  228. pdf_save_document(ctx, doc, outfile, &opts);
  229. }
  230. fz_always(ctx)
  231. pdf_drop_document(ctx, doc);
  232. fz_catch(ctx)
  233. {
  234. fz_report_error(ctx);
  235. ret = 1;
  236. }
  237. fz_drop_context(ctx);
  238. return ret;
  239. }