Cpdflin with passwords

This commit is contained in:
John Whitington 2014-11-19 17:36:02 +00:00
parent 73d624967f
commit 1f52689047
2 changed files with 38 additions and 105 deletions

84
cpdf.ml
View File

@ -36,13 +36,8 @@ let find_cpdflin provided =
of the cpdflin binary. Returns the exit code. *)
let call_cpdflin cpdflin temp output best_password =
let command =
match Sys.os_type with
"Win32" ->
cpdflin ^ " --linearize " ^
Filename.quote temp ^ " " ^ Filename.quote output
| _ ->
cpdflin ^ " " ^ Filename.quote temp ^
" \"" ^ best_password ^ "\" " ^ Filename.quote output
cpdflin ^ " --linearize " ^ " --password=" ^ best_password ^ " " ^
Filename.quote temp ^ " " ^ Filename.quote output
in
Sys.command command
@ -1166,66 +1161,6 @@ let name_of_spec marks (pdf : Pdf.t) splitlevel spec n filename startpage endpag
let stem s =
implode (rev (tail_no_fail (dropwhile (neq '.') (rev (explode (Filename.basename s))))))
let really_write_pdf ~preserve_objstm ~create_objstm ?(encryption = None) ?(cpdflin = None) linearize mk_id pdf outname =
let outname' =
if linearize
then Filename.temp_file "cpdflin" ".pdf"
else outname
in
Pdfwrite.pdf_to_file_options
~preserve_objstm
~generate_objstm:create_objstm
false encryption mk_id pdf outname';
if linearize then
let cpdflin =
match find_cpdflin cpdflin with
Some x -> x
| None -> raise (Pdf.PDFError "Could not find cpdflin")
in
let best_password =
match encryption with
None -> ""
| Some x ->
if x.Pdfwrite.owner_password <> ""
then x.Pdfwrite.owner_password
else x.Pdfwrite.user_password
in
let code = call_cpdflin cpdflin outname' outname best_password in
begin try Sys.remove outname' with _ -> () end;
if code > 0 then
begin
begin try Sys.remove outname with _ -> () end;
raise (Pdf.PDFError "linearizer failed")
end
(*let fast_write_split_pdfs
recrypt was_encrypted was_decrypted_with_owner enc splitlevel
original_filename linearize ?(cpdflin = None) preserve_objstm
create_objstm sq nobble spec main_pdf pagenums pdf_pages
=
let marks = Pdfmarks.read_bookmarks main_pdf in
iter2
(fun number pagenums ->
let pdf = nobble (Pdfpage.pdf_of_pages main_pdf pagenums) in
let startpage, endpage = extremes pagenums in
let name = name_of_spec marks main_pdf splitlevel spec number (stem original_filename) startpage endpage in
Pdf.remove_unreferenced pdf;
if sq then squeeze pdf;
really_write_pdf ~preserve_objstm ~create_objstm ~encryption:enc linearize (not (enc = None)) pdf name)
(indx pagenums)
pagenums
let split_pdf
recrypt was_encrypted was_decrypted_with_owner enc original_filename
chunksize linearize ~cpdflin ~preserve_objstm ~create_objstm ~squeeze
nobble spec pdf
=
let pdf_pages = Pdfpage.pages_of_pagetree pdf in
fast_write_split_pdfs
recrypt was_encrypted was_decrypted_with_owner
enc 0 original_filename linearize preserve_objstm create_objstm
squeeze nobble spec pdf (splitinto chunksize (indx pdf_pages)) pdf_pages*)
(* Return list, in order, a *set* of page numbers of bookmarks at a given level *)
let bookmark_pages level pdf =
setify_preserving_order
@ -1233,21 +1168,6 @@ let bookmark_pages level pdf =
(function l when l.Pdfmarks.level = level -> Some (Pdfpage.pagenumber_of_target pdf l.Pdfmarks.target) | _ -> None)
(Pdfmarks.read_bookmarks pdf))
(*let split_at_bookmarks
recrypt was_encrypted was_decrypted_with_owner original_filename linearize
~cpdflin ~preserve_objstm ~create_objstm ~squeeze nobble level spec pdf
=
let pdf_pages = Pdfpage.pages_of_pagetree pdf in
let points = bookmark_pages level pdf in
let points =
lose (fun x -> x <= 0 || x > Pdfpage.endpage pdf) (map pred points)
in
let pts = splitat points (indx pdf_pages) in
fast_write_split_pdfs
recrypt was_encrypted was_decrypted_with_owner None level
original_filename linearize preserve_objstm create_objstm
squeeze nobble spec pdf pts pdf_pages*)
(* Called from cpdflib.ml - different from above *)
let split_on_bookmarks pdf level =
let points = lose (eq 0) (map pred (bookmark_pages level pdf))

View File

@ -1935,20 +1935,22 @@ let get_single_pdf_nodecrypt read_lazy =
let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outname =
if args.debugcrypt then Printf.printf "really_write_pdf\n%!";
let outname' =
if args.linearize
then Filename.temp_file "cpdflin" ".pdf"
else outname
if args.linearize then Filename.temp_file "cpdflin" ".pdf" else outname
in
if args.debugcrypt then
Printf.printf "args.recrypt = %b, args.was_encrypted = %b\n" args.recrypt args.was_encrypted;
Printf.printf "args.recrypt = %b, args.was_encrypted = %b\n"
args.recrypt args.was_encrypted;
begin
if args.recrypt && args.was_encrypted then
begin
if args.debugcrypt then Printf.printf "Recrypting in really_write_pdf\n";
if args.debugcrypt then
Printf.printf "Recrypting in really_write_pdf\n";
match args.inputs with
[] -> raise (Pdf.PDFError "no input in recryption")
| (_, _, _, user_pw, owner_pw, _)::_ ->
let best_password = if owner_pw <> "" then owner_pw else user_pw in
let best_password =
if owner_pw <> "" then owner_pw else user_pw
in
Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
@ -1960,15 +1962,17 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna
begin
if not args.was_encrypted || args.was_decrypted_with_owner then
begin
if args.debugcrypt then Printf.printf "Pdf to file in really_write_pdf\n";
Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
~compress_objstm:(not is_decompress)
false encryption mk_id pdf outname'
if args.debugcrypt then
Printf.printf "Pdf to file in really_write_pdf\n";
Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
~compress_objstm:(not is_decompress)
false encryption mk_id pdf outname'
end
else
soft_error "You must supply -recrypt here, or provide the owner password."
soft_error
"You must supply -recrypt here, or provide the owner password."
end
end;
begin
@ -1978,16 +1982,25 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna
Some x -> x
| None -> raise (Pdf.PDFError "Could not find cpdflin")
in
let best_password = if args.owner <> "" then args.owner else args.user in
let code = Cpdf.call_cpdflin cpdflin outname' outname best_password in
if code > 0 then
begin
begin try Sys.remove outname with _ -> () end;
Sys.rename outname' outname;
soft_error "Linearizer failed with above error. File written without linearization."
end
else
begin try Sys.remove outname' with _ -> () end;
match args.inputs with
[] -> raise (Pdf.PDFError "no input in recryption")
| (_, _, _, user_pw, owner_pw, _)::_ ->
let best_password =
if owner_pw <> "" then owner_pw else user_pw
in
let code =
Cpdf.call_cpdflin cpdflin outname' outname best_password
in
if code > 0 then
begin
begin try Sys.remove outname with _ -> () end;
Sys.rename outname' outname;
soft_error
"Linearizer failed with above error. \
File written without linearization."
end
else
begin try Sys.remove outname' with _ -> () end;
end;
if args.squeeze then
let s = filesize outname in