summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/pdfclean.c5
-rw-r--r--fitz/fitz.h3
-rw-r--r--pdf/pdf_write.c48
3 files changed, 51 insertions, 5 deletions
diff --git a/apps/pdfclean.c b/apps/pdfclean.c
index 409d4fbb..e892db29 100644
--- a/apps/pdfclean.c
+++ b/apps/pdfclean.c
@@ -162,11 +162,14 @@ int pdfclean_main(int argc, char **argv)
int subset;
fz_write_options opts;
int write_failed = 0;
+ int errors = 0;
opts.do_garbage = 0;
opts.do_expand = 0;
opts.do_ascii = 0;
opts.do_linear = 0;
+ opts.continue_on_error = 1;
+ opts.errors = &errors;
while ((c = fz_getopt(argc, argv, "adfgilp:")) != -1)
{
@@ -229,5 +232,7 @@ int pdfclean_main(int argc, char **argv)
fz_free_context(ctx);
+ if (errors)
+ write_failed = 1;
return write_failed ? 1 : 0;
}
diff --git a/fitz/fitz.h b/fitz/fitz.h
index 1bbedd05..6ece9251 100644
--- a/fitz/fitz.h
+++ b/fitz/fitz.h
@@ -2963,6 +2963,9 @@ struct fz_write_options_s
int do_garbage; /* If non-zero then attempt (where possible) to
garbage collect the file before writing. */
int do_linear; /* If non-zero then write linearised. */
+ int continue_on_error; /* If non-zero, errors are (optionally)
+ counted and writing continues. */
+ int *errors; /* Pointer to a place to store a count of errors */
};
/* An enumeration of bitflags to use in the above 'do_expand' field of
diff --git a/pdf/pdf_write.c b/pdf/pdf_write.c
index 98f0dd2e..a94bd044 100644
--- a/pdf/pdf_write.c
+++ b/pdf/pdf_write.c
@@ -56,6 +56,8 @@ struct pdf_write_options_s
int *ofs_list;
int *gen_list;
int *renumber_map;
+ int continue_on_error;
+ int *errors;
/* The following extras are required for linearization */
int *rev_renumber_map;
int *rev_gen_list;
@@ -1573,7 +1575,23 @@ static void writeobject(pdf_document *xref, pdf_write_options *opts, int num, in
pdf_obj *type;
fz_context *ctx = xref->ctx;
- obj = pdf_load_object(xref, num, gen);
+ fz_try(ctx)
+ {
+ obj = pdf_load_object(xref, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ if (opts->continue_on_error)
+ {
+ fprintf(opts->out, "%d %d obj\nnull\nendobj\n", num, gen);
+ if (opts->errors)
+ (*opts->errors)++;
+ fz_warn(ctx, fz_caught(ctx));
+ return;
+ }
+ else
+ fz_rethrow(ctx);
+ }
/* skip ObjStm and XRef objects */
if (pdf_is_dict(obj))
@@ -1634,10 +1652,28 @@ static void writeobject(pdf_document *xref, pdf_write_options *opts, int num, in
if (pdf_dict_gets(obj, "Width") != NULL && pdf_dict_gets(obj, "Height") != NULL)
dontexpand = !(opts->do_expand & fz_expand_images);
}
- if (opts->do_expand && !dontexpand && !pdf_is_jpx_image(ctx, obj))
- expandstream(xref, opts, obj, num, gen);
- else
- copystream(xref, opts, obj, num, gen);
+ fz_try(ctx)
+ {
+ if (opts->do_expand && !dontexpand && !pdf_is_jpx_image(ctx, obj))
+ expandstream(xref, opts, obj, num, gen);
+ else
+ copystream(xref, opts, obj, num, gen);
+ }
+ fz_catch(ctx)
+ {
+ if (opts->continue_on_error)
+ {
+ fprintf(opts->out, "%d %d obj\nnull\nendobj\n", num, gen);
+ if (opts->errors)
+ (*opts->errors)++;
+ fz_warn(ctx, fz_caught(ctx));
+ }
+ else
+ {
+ pdf_drop_obj(obj);
+ fz_rethrow(ctx);
+ }
+ }
}
pdf_drop_obj(obj);
@@ -2148,6 +2184,8 @@ void pdf_write_document(pdf_document *xref, char *filename, fz_write_options *fz
opts.renumber_map = fz_malloc_array(ctx, xref->len + 3, sizeof(int));
opts.rev_renumber_map = fz_malloc_array(ctx, xref->len + 3, sizeof(int));
opts.rev_gen_list = fz_malloc_array(ctx, xref->len + 3, sizeof(int));
+ opts.continue_on_error = fz_opts->continue_on_error;
+ opts.errors = fz_opts->errors;
for (num = 0; num < xref->len; num++)
{