From 5025ff0d5b5b7575d46fad105ebccb3ae7400949 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 7 Jan 2015 20:29:39 +0000 Subject: [PATCH] Squeeze logging --- cpdf.ml | 49 +++++++++++++++++++++++++++++-------------------- cpdf.mli | 2 +- cpdfcommand.ml | 12 +++++++++--- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index ce094c4..28def5f 100755 --- a/cpdf.ml +++ b/cpdf.ml @@ -241,26 +241,35 @@ let squeeze_all_content_streams pdf = (* We run squeeze enough times to reach a fixed point in the cardinality of the * object map *) -let squeeze pdf = - try - let n = ref (Pdf.objcard pdf) in - Printf.printf "Beginning squeeze: %i objects\n%!" (Pdf.objcard pdf); - while !n > (ignore (really_squeeze pdf); Pdf.objcard pdf) do - n := Pdf.objcard pdf; - Printf.printf "Squeezing... Down to %i objects\n%!" (Pdf.objcard pdf); - done; - Printf.printf "Squeezing page data and xobjects\n%!"; - squeeze_all_content_streams pdf; - Printf.printf "Recompressing document\n%!"; - Pdfcodec.flate_level := 9; - ignore (recompress_pdf pdf) - with - e -> - raise - (Pdf.PDFError - (Printf.sprintf - "Squeeze failed. No output written.\n Proximate error was:\n %s" - (Printexc.to_string e))) +let squeeze ?logto pdf = + let log x = + match logto with + None -> print_string x; flush stdout + | Some s -> + let fh = open_out_gen [Open_wronly; Open_creat] 0o666 s in + seek_out fh (out_channel_length fh); + output_string fh x; + close_out fh + in + try + let n = ref (Pdf.objcard pdf) in + log (Printf.sprintf "Beginning squeeze: %i objects\n" (Pdf.objcard pdf)); + while !n > (ignore (really_squeeze pdf); Pdf.objcard pdf) do + n := Pdf.objcard pdf; + log (Printf.sprintf "Squeezing... Down to %i objects\n" (Pdf.objcard pdf)); + done; + log (Printf.sprintf "Squeezing page data and xobjects\n"); + squeeze_all_content_streams pdf; + log (Printf.sprintf "Recompressing document\n"); + Pdfcodec.flate_level := 9; + ignore (recompress_pdf pdf) + with + e -> + raise + (Pdf.PDFError + (Printf.sprintf + "Squeeze failed. No output written.\n Proximate error was:\n %s" + (Printexc.to_string e))) type encoding = | Raw diff --git a/cpdf.mli b/cpdf.mli index 9147e2d..8593b6e 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -459,7 +459,7 @@ val blackfills : int list -> Pdf.t -> Pdf.t val draft : bool -> int list -> Pdf.t -> Pdf.t (** Squeeze a PDF *) -val squeeze : Pdf.t -> unit +val squeeze : ?logto:string -> Pdf.t -> unit (**/**) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 3f40228..3d8334e 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1333,6 +1333,11 @@ let setrecrypt () = let setremovedictentry s = args.op <- Some (RemoveDictEntry s) +let logto = ref None + +let setsqueezelogto s = + logto := Some s + (* Parse a control file, make an argv, and then make Arg parse it. *) let rec make_control_argv_and_parse filename = control_args := !control_args @ parse_control_file filename @@ -1850,6 +1855,7 @@ and specs = ("-debug-crypt", Arg.Unit setdebugcrypt, ""); ("-fix-prince", Arg.Unit (setop RemoveUnusedResources), ""); ("-squeeze", Arg.Unit setsqueeze, ""); + ("-squeeze-log-to", Arg.String setsqueezelogto, ""); ("-extract-text", Arg.Unit (setop ExtractText), "")] and usage_msg = @@ -2031,7 +2037,7 @@ let write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf = if not is_decompress then begin ignore (Cpdf.recompress_pdf pdf); - if args.squeeze then Cpdf.squeeze pdf; + if args.squeeze then Cpdf.squeeze ?logto:!logto pdf; end; Pdf.remove_unreferenced pdf; really_write_pdf ~is_decompress mk_id pdf outname @@ -2046,7 +2052,7 @@ let write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf = if not is_decompress then begin ignore (Cpdf.recompress_pdf pdf); - if args.squeeze then Cpdf.squeeze pdf; + if args.squeeze then Cpdf.squeeze ?logto:!logto pdf; Pdf.remove_unreferenced pdf end; really_write_pdf ~encryption ~is_decompress mk_id pdf temp; @@ -2136,7 +2142,7 @@ let fast_write_split_pdfs (stem original_filename) startpage endpage in Pdf.remove_unreferenced pdf; - if sq then Cpdf.squeeze pdf; + if sq then Cpdf.squeeze ?logto:!logto pdf; really_write_pdf ~encryption:enc (not (enc = None)) pdf name) (indx pagenums) pagenums