pdfbake.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 baking tool: Bake interactive form and/or annotation content into static graphics.
  24. */
  25. #include "mupdf/fitz.h"
  26. #include "mupdf/pdf.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. static int usage(void)
  31. {
  32. fprintf(stderr,
  33. "usage: mutool bake [options] input.pdf [output.pdf]\n"
  34. "\t-A\tkeep annotations\n"
  35. "\t-F\tkeep forms\n"
  36. "\t-O -\tcomma separated list of output options\n"
  37. );
  38. return 1;
  39. }
  40. int pdfbake_main(int argc, char **argv)
  41. {
  42. fz_context *ctx;
  43. pdf_document *doc;
  44. pdf_write_options opts = pdf_default_write_options;
  45. int bake_annots = 1;
  46. int bake_widgets = 1;
  47. char *output = "out.pdf";
  48. char *flags = "garbage";
  49. char *input;
  50. int c;
  51. while ((c = fz_getopt(argc, argv, "AFO:")) != -1)
  52. {
  53. switch (c)
  54. {
  55. case 'A':
  56. bake_annots = 0;
  57. break;
  58. case 'F':
  59. bake_widgets = 0;
  60. break;
  61. case 'O':
  62. flags = fz_optarg;
  63. break;
  64. default:
  65. return usage();
  66. }
  67. }
  68. if (argc - fz_optind < 1)
  69. return usage();
  70. input = argv[fz_optind++];
  71. if (argc - fz_optind > 0)
  72. output = argv[fz_optind++];
  73. ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
  74. if (!ctx)
  75. {
  76. fprintf(stderr, "error: Cannot initialize MuPDF context.\n");
  77. exit(1);
  78. }
  79. fz_try(ctx)
  80. {
  81. doc = pdf_open_document(ctx, input);
  82. pdf_bake_document(ctx, doc, bake_annots, bake_widgets);
  83. pdf_parse_write_options(ctx, &opts, flags);
  84. pdf_save_document(ctx, doc, output, &opts);
  85. }
  86. fz_catch(ctx)
  87. {
  88. fz_report_error(ctx);
  89. return 1;
  90. }
  91. pdf_drop_document(ctx, doc);
  92. fz_flush_warnings(ctx);
  93. fz_drop_context(ctx);
  94. return 0;
  95. }