More linearizer work

This commit is contained in:
John Whitington 2014-10-02 13:32:30 +01:00
parent ab460c0161
commit fbf1dc069c
2 changed files with 54 additions and 54 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@
._d/ ._d/
cpdf cpdf
*.annot *.annot
cpdflin

View File

@ -9,17 +9,15 @@ open Pdfutil
open Pdfio open Pdfio
(* Find the location of the cpdflin binary, either in a provided place (with (* Find the location of the cpdflin binary, either in a provided place (with
-cpdflin), or using argv[0] or in the current directory *) -cpdflin), or using argv[0] or in the current directory. *)
let find_cpdflin provided = let find_cpdflin provided =
match provided with match provided with
Some x -> Some x Some x -> Some x
| None -> | None ->
match Sys.argv.(0) with match Sys.argv.(0) with
"cpdf" -> "cpdf" ->
Printf.printf "Argv no use, looking in current directory\n";
if Sys.file_exists "cpdflin" then Some "cpdflin" else None if Sys.file_exists "cpdflin" then Some "cpdflin" else None
| s -> | s ->
Printf.printf "Found location from argv at %s\n%!" s;
try try
let fullname = let fullname =
Filename.dirname s ^ Filename.dir_sep ^ "cpdflin" Filename.dirname s ^ Filename.dir_sep ^ "cpdflin"
@ -35,7 +33,6 @@ let call_cpdflin cpdflin temp output best_password =
cpdflin ^ " " ^ Filename.quote temp ^ cpdflin ^ " " ^ Filename.quote temp ^
" \"" ^ best_password ^ "\" " ^ Filename.quote output " \"" ^ best_password ^ "\" " ^ Filename.quote output
in in
print_endline command;
Sys.command command Sys.command command
(* Wrap up the file reading functions to exit with code 1 when an encryption (* Wrap up the file reading functions to exit with code 1 when an encryption
@ -1800,18 +1797,7 @@ let rec writing_ok outname =
else else
outname outname
let write_pdf mk_id pdf = let really_write_pdf ?(encryption = None) mk_id pdf outname =
if args.create_objstm && not args.keepversion
then pdf.Pdf.minor <- max pdf.Pdf.minor 5;
let mk_id = args.makenewid || mk_id in
match args.out with
| NoOutputSpecified ->
output_pdfs =| pdf
| File outname ->
let outname = writing_ok outname in
let pdf = Cpdf.recompress_pdf <| nobble pdf in
if args.squeeze then Cpdf.squeeze pdf;
Pdf.remove_unreferenced pdf;
let outname' = let outname' =
if args.linearize if args.linearize
then Filename.temp_file "cpdflin" ".pdf" then Filename.temp_file "cpdflin" ".pdf"
@ -1820,29 +1806,57 @@ let write_pdf mk_id pdf =
Pdfwrite.pdf_to_file_options Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm ~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm ~generate_objstm:args.create_objstm
false None mk_id pdf outname'; false encryption mk_id pdf outname';
if args.linearize then
let cpdflin = let cpdflin =
match find_cpdflin None with match find_cpdflin None with
Some x -> x Some x -> x
| None -> raise (Pdf.PDFError "Could not find cpdflin") | None -> raise (Pdf.PDFError "Could not find cpdflin")
in in
if args.linearize then let best_password = if args.owner <> "" then args.owner else args.user in
let code = call_cpdflin cpdflin outname' outname "" in let code = call_cpdflin cpdflin outname' outname best_password in
begin try Sys.remove outname' with _ -> () end; begin try Sys.remove outname' with _ -> () end;
if code > 0 then if code > 0 then
begin begin
begin try Sys.remove outname with _ -> () end; begin try Sys.remove outname with _ -> () end;
raise (Pdf.PDFError "linearizer failed") raise (Pdf.PDFError "linearizer failed")
end end
| Stdout ->
let write_pdf ?(encryption = None) mk_id pdf =
if args.create_objstm && not args.keepversion
then pdf.Pdf.minor <- max pdf.Pdf.minor 5;
let mk_id = args.makenewid || mk_id in
match args.out with
| NoOutputSpecified ->
output_pdfs =| pdf
| File outname ->
let outname = writing_ok outname in
begin match encryption with
None ->
let pdf = Cpdf.recompress_pdf <| nobble pdf in let pdf = Cpdf.recompress_pdf <| nobble pdf in
if args.squeeze then Cpdf.squeeze pdf; if args.squeeze then Cpdf.squeeze pdf;
Pdf.remove_unreferenced pdf; Pdf.remove_unreferenced pdf;
Pdfwrite.pdf_to_channel really_write_pdf mk_id pdf outname
~preserve_objstm:args.preserve_objstm | Some _ ->
~generate_objstm:args.create_objstm really_write_pdf ~encryption mk_id pdf outname
false (*FIXLIN args.linearize*) None mk_id pdf stdout; end
flush stdout (*r For Windows *) | Stdout ->
let temp = Filename.temp_file "cpdflin" ".pdf" in
begin match encryption with
None ->
let pdf = Cpdf.recompress_pdf <| nobble pdf in
if args.squeeze then Cpdf.squeeze pdf;
Pdf.remove_unreferenced pdf;
really_write_pdf ~encryption mk_id pdf temp;
| Some _ ->
really_write_pdf ~encryption mk_id pdf temp
end;
let temp_file = open_in_bin temp in
try
while true do output_char stdout (input_char temp_file) done;
assert false
with
End_of_file -> flush stdout (*r For Windows *)
let pdf_of_stdin user_pw owner_pw = let pdf_of_stdin user_pw owner_pw =
let user_pw = Some user_pw let user_pw = Some user_pw
@ -3408,22 +3422,7 @@ let go () =
let newversion = if args.create_objstm then 5 else newversion in let newversion = if args.create_objstm then 5 else newversion in
pdf.Pdf.minor <- max pdf.Pdf.minor newversion pdf.Pdf.minor <- max pdf.Pdf.minor newversion
end; end;
begin match args.out with write_pdf ~encryption:(Some encryption) args.makenewid pdf
| NoOutputSpecified ->
error "encrypt: no output specified"
| File outname ->
let outname = writing_ok outname in
Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
(*FIXLIN args.linearize*)false (Some encryption) args.makenewid pdf outname
| Stdout ->
Pdfwrite.pdf_to_channel
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
(*FIXLIN args.linearize*)false (Some encryption) args.makenewid pdf stdout;
flush stdout;
end
| Some Decrypt -> | Some Decrypt ->
write_pdf false (get_single_pdf args.op false) write_pdf false (get_single_pdf args.op false)
| Some RemoveMetadata -> | Some RemoveMetadata ->