Exemplo n.º 1
0
void pdf_clean_file(fz_context *ctx, char *infile, char *outfile, char *password, pdf_write_options *opts, char *argv[], int argc)
{
	globals glo = { 0 };

	glo.ctx = ctx;

	fz_try(ctx)
	{
		glo.doc = pdf_open_document(ctx, infile);
		if (pdf_needs_password(ctx, glo.doc))
			if (!pdf_authenticate_password(ctx, glo.doc, password))
				fz_throw(glo.ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile);

		/* Only retain the specified subset of the pages */
		if (argc)
			retainpages(ctx, &glo, argc, argv);

		pdf_save_document(ctx, glo.doc, outfile, opts);
	}
	fz_always(ctx)
	{
		pdf_drop_document(ctx, glo.doc);
	}
	fz_catch(ctx)
	{
		if (opts && opts->errors)
			*opts->errors = *opts->errors+1;
	}
}
Exemplo n.º 2
0
int pdfposter_main(int argc, char **argv)
{
	char *infile;
	char *outfile = "out.pdf";
	char *password = "";
	int c;
	pdf_write_options opts = { 0 };
	pdf_document *doc;
	fz_context *ctx;

	opts.do_incremental = 0;
	opts.do_garbage = 0;
	opts.do_expand = 0;
	opts.do_ascii = 0;
	opts.do_linear = 0;

	while ((c = fz_getopt(argc, argv, "x:y:")) != -1)
	{
		switch (c)
		{
		case 'p': password = fz_optarg; break;
		case 'x': x_factor = atoi(fz_optarg); break;
		case 'y': y_factor = atoi(fz_optarg); break;
		default: usage(); break;
		}
	}

	if (argc - fz_optind < 1)
		usage();

	infile = argv[fz_optind++];

	if (argc - fz_optind > 0 &&
		(strstr(argv[fz_optind], ".pdf") || strstr(argv[fz_optind], ".PDF")))
	{
		outfile = argv[fz_optind++];
	}

	ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
	if (!ctx)
	{
		fprintf(stderr, "cannot initialise context\n");
		exit(1);
	}

	doc = pdf_open_document(ctx, infile);
	if (pdf_needs_password(ctx, doc))
		if (!pdf_authenticate_password(ctx, doc, password))
			fz_throw(ctx, FZ_ERROR_GENERIC, "cannot authenticate password: %s", infile);

	decimatepages(ctx, doc);

	pdf_save_document(ctx, doc, outfile, &opts);

	pdf_drop_document(ctx, doc);
	fz_drop_context(ctx);
	return 0;
}
Exemplo n.º 3
0
int pdfmerge_main(int argc, char **argv)
{
	pdf_write_options opts = { 0 };
	char *output = "out.pdf";
	char *infile_src;
	int c;

	while ((c = fz_getopt(argc, argv, "adlszo:")) != -1)
	{
		switch (c)
		{
		case 'o': output = fz_optarg; break;
		case 'a': opts.do_ascii ++; break;
		case 'd': opts.do_expand ^= PDF_EXPAND_ALL; break;
		case 'l': opts.do_linear ++; break;
		case 's': opts.do_clean ++; break;
		case 'z': opts.do_deflate ++; break;
		default: usage(); break;
		}
	}

	if (fz_optind == argc)
		usage();

	ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
	if (!ctx)
	{
		fprintf(stderr, "Cannot initialise context\n");
		exit(1);
	}

	fz_try(ctx)
	{
		doc_des = pdf_create_document(ctx);
	}
	fz_catch(ctx)
	{
		fprintf(stderr, "Failed to allocate destination document file %s\n", output);
		exit(1);
	}

	/* Step through the source files */
	while (fz_optind < argc)
	{
		fz_try(ctx)
		{
			infile_src = argv[fz_optind++];
			pdf_drop_document(ctx, doc_src);
			doc_src = pdf_open_document(ctx, infile_src);

			if (fz_optind == argc || !isrange(argv[fz_optind]))
				merge_range("1-");
			else
				merge_range(argv[fz_optind++]);
		}
		fz_catch(ctx)
		{
			fprintf(stderr, "Failed merging document %s\n", infile_src);
			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 encountered during file save.\n");
		exit(1);
	}
	fz_flush_warnings(ctx);
	fz_drop_context(ctx);

	return 0;
}
Exemplo n.º 4
0
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;
}