static int pdfpages_pages(fz_context *ctx, fz_output *out, char *filename, char *password, char *argv[], int argc) { enum { NO_FILE_OPENED, NO_INFO_GATHERED, INFO_SHOWN } state; int argidx = 0; pdf_document *doc = NULL; int ret = 0; state = NO_FILE_OPENED; while (argidx < argc) { if (state == NO_FILE_OPENED || !fz_is_page_range(ctx, argv[argidx])) { if (state == NO_INFO_GATHERED) { showpages(ctx, doc, out, "1-N"); } pdf_drop_document(ctx, doc); filename = argv[argidx]; fz_printf(ctx, out, "%s:\n", filename); doc = pdf_open_document(ctx, filename); if (pdf_needs_password(ctx, doc)) if (!pdf_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", filename); state = NO_INFO_GATHERED; } else { ret |= showpages(ctx, doc, out, argv[argidx]); state = INFO_SHOWN; } argidx++; } if (state == NO_INFO_GATHERED) showpages(ctx, doc, out, "1-N"); pdf_drop_document(ctx, doc); return ret; }
int pdfmerge_main(int argc, char **argv) { pdf_write_options opts = { 0 }; char *output = "out.pdf"; char *flags = ""; char *input; int c; while ((c = fz_getopt(argc, argv, "o:O:")) != -1) { switch (c) { case 'o': output = fz_optarg; break; case 'O': flags = fz_optarg; break; default: usage(); break; } } if (fz_optind == argc) usage(); ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "error: Cannot initialize MuPDF context.\n"); exit(1); } pdf_parse_write_options(ctx, &opts, flags); fz_try(ctx) { doc_des = pdf_create_document(ctx); } fz_catch(ctx) { fprintf(stderr, "error: Cannot create destination document.\n"); exit(1); } /* Step through the source files */ while (fz_optind < argc) { input = argv[fz_optind++]; fz_try(ctx) { pdf_drop_document(ctx, doc_src); doc_src = pdf_open_document(ctx, input); if (fz_optind == argc || !fz_is_page_range(ctx, argv[fz_optind])) merge_range("1-N"); else merge_range(argv[fz_optind++]); } fz_catch(ctx) { fprintf(stderr, "error: Cannot merge document '%s'.\n", input); exit(1); } } fz_try(ctx) { pdf_save_document(ctx, doc_des, output, &opts); } fz_always(ctx) { pdf_drop_document(ctx, doc_des); pdf_drop_document(ctx, doc_src); } fz_catch(ctx) { fprintf(stderr, "error: Cannot save output file: '%s'.\n", output); exit(1); } fz_flush_warnings(ctx); fz_drop_context(ctx); return 0; }
int muconvert_main(int argc, char **argv) { int i, c; while ((c = fz_getopt(argc, argv, "p:A:W:H:S:U:o:F:O:")) != -1) { switch (c) { default: usage(); break; case 'p': password = fz_optarg; break; case 'A': alphabits = atoi(fz_optarg); break; case 'W': layout_w = atof(fz_optarg); break; case 'H': layout_h = atof(fz_optarg); break; case 'S': layout_em = atof(fz_optarg); break; case 'U': layout_css = fz_optarg; break; case 'o': output = fz_optarg; break; case 'F': format = fz_optarg; break; case 'O': options = fz_optarg; break; } } if (fz_optind == argc || (!format && !output)) usage(); /* Create a context to hold the exception stack and various caches. */ ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED); if (!ctx) { fprintf(stderr, "cannot create mupdf context\n"); return EXIT_FAILURE; } /* Register the default file types to handle. */ fz_try(ctx) fz_register_document_handlers(ctx); fz_catch(ctx) { fprintf(stderr, "cannot register document handlers: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } fz_set_aa_level(ctx, alphabits); if (layout_css) { fz_buffer *buf = fz_read_file(ctx, layout_css); fz_write_buffer_byte(ctx, buf, 0); fz_set_user_css(ctx, (char*)buf->data); fz_drop_buffer(ctx, buf); } /* Open the output document. */ fz_try(ctx) out = fz_new_document_writer(ctx, output, format, options); fz_catch(ctx) { fprintf(stderr, "cannot create document: %s\n", fz_caught_message(ctx)); fz_drop_context(ctx); return EXIT_FAILURE; } for (i = fz_optind; i < argc; ++i) { doc = fz_open_document(ctx, argv[i]); if (fz_needs_password(ctx, doc)) if (!fz_authenticate_password(ctx, doc, password)) fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", argv[i]); fz_layout_document(ctx, doc, layout_w, layout_h, layout_em); count = fz_count_pages(ctx, doc); if (i+1 < argc && fz_is_page_range(ctx, argv[i+1])) runrange(argv[++i]); else runrange("1-N"); fz_drop_document(ctx, doc); } fz_drop_document_writer(ctx, out); fz_drop_context(ctx); return EXIT_SUCCESS; }