pdfpages.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright (C) 2004-2021 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. * Information tool.
  24. * Print information about pages of a pdf.
  25. */
  26. #include "mupdf/fitz.h"
  27. #include "mupdf/pdf.h"
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. static int
  31. infousage(void)
  32. {
  33. fprintf(stderr,
  34. "usage: mutool pages [options] file.pdf [pages]\n"
  35. "\t-p -\tpassword for decryption\n"
  36. "\tpages\tcomma separated list of page numbers and ranges\n"
  37. );
  38. return 1;
  39. }
  40. static int
  41. showbox(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name)
  42. {
  43. fz_rect bbox;
  44. pdf_obj *obj;
  45. int failed = 0;
  46. fz_try(ctx)
  47. {
  48. obj = pdf_dict_get(ctx, page, name);
  49. if (!pdf_is_array(ctx, obj))
  50. break;
  51. bbox = pdf_to_rect(ctx, obj);
  52. fz_write_printf(ctx, out, "<%s l=\"%g\" b=\"%g\" r=\"%g\" t=\"%g\" />\n", text, bbox.x0, bbox.y0, bbox.x1, bbox.y1);
  53. }
  54. fz_catch(ctx)
  55. {
  56. failed = 1;
  57. }
  58. return failed;
  59. }
  60. static int
  61. shownum(fz_context *ctx, fz_output *out, pdf_obj *page, char *text, pdf_obj *name)
  62. {
  63. pdf_obj *obj;
  64. int failed = 0;
  65. fz_try(ctx)
  66. {
  67. obj = pdf_dict_get(ctx, page, name);
  68. if (!pdf_is_number(ctx, obj))
  69. break;
  70. fz_write_printf(ctx, out, "<%s v=\"%g\" />\n", text, pdf_to_real(ctx, obj));
  71. }
  72. fz_catch(ctx)
  73. {
  74. failed = 1;
  75. }
  76. return failed;
  77. }
  78. static int
  79. showpage(fz_context *ctx, pdf_document *doc, fz_output *out, int page)
  80. {
  81. pdf_obj *pageref;
  82. int failed = 0;
  83. fz_write_printf(ctx, out, "<page pagenum=\"%d\">\n", page);
  84. fz_try(ctx)
  85. {
  86. pageref = pdf_lookup_page_obj(ctx, doc, page-1);
  87. if (!pageref)
  88. fz_throw(ctx, FZ_ERROR_GENERIC, "cannot retrieve info from page %d", page);
  89. }
  90. fz_catch(ctx)
  91. {
  92. fz_write_printf(ctx, out, "Failed to gather information for page %d\n", page);
  93. failed = 1;
  94. }
  95. if (!failed)
  96. {
  97. failed |= showbox(ctx, out, pageref, "MediaBox", PDF_NAME(MediaBox));
  98. failed |= showbox(ctx, out, pageref, "CropBox", PDF_NAME(CropBox));
  99. failed |= showbox(ctx, out, pageref, "ArtBox", PDF_NAME(ArtBox));
  100. failed |= showbox(ctx, out, pageref, "BleedBox", PDF_NAME(BleedBox));
  101. failed |= showbox(ctx, out, pageref, "TrimBox", PDF_NAME(TrimBox));
  102. failed |= shownum(ctx, out, pageref, "Rotate", PDF_NAME(Rotate));
  103. failed |= shownum(ctx, out, pageref, "UserUnit", PDF_NAME(UserUnit));
  104. }
  105. fz_write_printf(ctx, out, "</page>\n");
  106. return failed;
  107. }
  108. static int
  109. showpages(fz_context *ctx, pdf_document *doc, fz_output *out, const char *pagelist)
  110. {
  111. int page, spage, epage;
  112. int pagecount;
  113. int ret = 0;
  114. if (!doc)
  115. return infousage();
  116. pagecount = pdf_count_pages(ctx, doc);
  117. while ((pagelist = fz_parse_page_range(ctx, pagelist, &spage, &epage, pagecount)))
  118. {
  119. int fail;
  120. if (spage > epage)
  121. page = spage, spage = epage, epage = page;
  122. for (page = spage; page <= epage; page++)
  123. {
  124. fail = showpage(ctx, doc, out, page);
  125. /* On the first failure, check for the pagecount having changed. */
  126. if (fail && !ret)
  127. {
  128. pagecount = pdf_count_pages(ctx, doc);
  129. if (epage > pagecount)
  130. epage = pagecount;
  131. }
  132. ret |= fail;
  133. }
  134. }
  135. return ret;
  136. }
  137. static int
  138. pdfpages_pages(fz_context *ctx, fz_output *out, char *filename, char *password, char *argv[], int argc)
  139. {
  140. enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state;
  141. int argidx = 0;
  142. pdf_document *doc = NULL;
  143. int ret = 0;
  144. state = NO_FILE_OPENED;
  145. while (argidx < argc)
  146. {
  147. if (state == NO_FILE_OPENED || !fz_is_page_range(ctx, argv[argidx]))
  148. {
  149. if (state == NO_INFO_GATHERED)
  150. {
  151. showpages(ctx, doc, out, "1-N");
  152. }
  153. pdf_drop_document(ctx, doc);
  154. filename = argv[argidx];
  155. fz_write_printf(ctx, out, "%s:\n", filename);
  156. doc = pdf_open_document(ctx, filename);
  157. if (pdf_needs_password(ctx, doc))
  158. if (!pdf_authenticate_password(ctx, doc, password))
  159. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot authenticate password: %s", filename);
  160. state = NO_INFO_GATHERED;
  161. }
  162. else
  163. {
  164. ret |= showpages(ctx, doc, out, argv[argidx]);
  165. state = INFO_SHOWN;
  166. }
  167. argidx++;
  168. }
  169. if (state == NO_INFO_GATHERED)
  170. showpages(ctx, doc, out, "1-N");
  171. pdf_drop_document(ctx, doc);
  172. return ret;
  173. }
  174. int pdfpages_main(int argc, char **argv)
  175. {
  176. char *filename = "";
  177. char *password = "";
  178. int c;
  179. int ret;
  180. fz_context *ctx;
  181. while ((c = fz_getopt(argc, argv, "p:")) != -1)
  182. {
  183. switch (c)
  184. {
  185. case 'p': password = fz_optarg; break;
  186. default:
  187. return infousage();
  188. }
  189. }
  190. if (fz_optind == argc)
  191. return infousage();
  192. ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
  193. if (!ctx)
  194. {
  195. fprintf(stderr, "cannot initialise context\n");
  196. exit(1);
  197. }
  198. ret = 0;
  199. fz_try(ctx)
  200. ret = pdfpages_pages(ctx, fz_stdout(ctx), filename, password, &argv[fz_optind], argc-fz_optind);
  201. fz_catch(ctx)
  202. {
  203. fz_report_error(ctx);
  204. ret = 1;
  205. }
  206. fz_drop_context(ctx);
  207. return ret;
  208. }