Cpdflin with passwords
This commit is contained in:
parent
73d624967f
commit
1f52689047
84
cpdf.ml
84
cpdf.ml
|
@ -36,13 +36,8 @@ let find_cpdflin provided =
|
||||||
of the cpdflin binary. Returns the exit code. *)
|
of the cpdflin binary. Returns the exit code. *)
|
||||||
let call_cpdflin cpdflin temp output best_password =
|
let call_cpdflin cpdflin temp output best_password =
|
||||||
let command =
|
let command =
|
||||||
match Sys.os_type with
|
cpdflin ^ " --linearize " ^ " --password=" ^ best_password ^ " " ^
|
||||||
"Win32" ->
|
Filename.quote temp ^ " " ^ Filename.quote output
|
||||||
cpdflin ^ " --linearize " ^
|
|
||||||
Filename.quote temp ^ " " ^ Filename.quote output
|
|
||||||
| _ ->
|
|
||||||
cpdflin ^ " " ^ Filename.quote temp ^
|
|
||||||
" \"" ^ best_password ^ "\" " ^ Filename.quote output
|
|
||||||
in
|
in
|
||||||
Sys.command command
|
Sys.command command
|
||||||
|
|
||||||
|
@ -1166,66 +1161,6 @@ let name_of_spec marks (pdf : Pdf.t) splitlevel spec n filename startpage endpag
|
||||||
let stem s =
|
let stem s =
|
||||||
implode (rev (tail_no_fail (dropwhile (neq '.') (rev (explode (Filename.basename 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 *)
|
(* Return list, in order, a *set* of page numbers of bookmarks at a given level *)
|
||||||
let bookmark_pages level pdf =
|
let bookmark_pages level pdf =
|
||||||
setify_preserving_order
|
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)
|
(function l when l.Pdfmarks.level = level -> Some (Pdfpage.pagenumber_of_target pdf l.Pdfmarks.target) | _ -> None)
|
||||||
(Pdfmarks.read_bookmarks pdf))
|
(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 *)
|
(* Called from cpdflib.ml - different from above *)
|
||||||
let split_on_bookmarks pdf level =
|
let split_on_bookmarks pdf level =
|
||||||
let points = lose (eq 0) (map pred (bookmark_pages level pdf))
|
let points = lose (eq 0) (map pred (bookmark_pages level pdf))
|
||||||
|
|
|
@ -1935,20 +1935,22 @@ let get_single_pdf_nodecrypt read_lazy =
|
||||||
let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outname =
|
let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outname =
|
||||||
if args.debugcrypt then Printf.printf "really_write_pdf\n%!";
|
if args.debugcrypt then Printf.printf "really_write_pdf\n%!";
|
||||||
let outname' =
|
let outname' =
|
||||||
if args.linearize
|
if args.linearize then Filename.temp_file "cpdflin" ".pdf" else outname
|
||||||
then Filename.temp_file "cpdflin" ".pdf"
|
|
||||||
else outname
|
|
||||||
in
|
in
|
||||||
if args.debugcrypt then
|
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
|
begin
|
||||||
if args.recrypt && args.was_encrypted then
|
if args.recrypt && args.was_encrypted then
|
||||||
begin
|
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
|
match args.inputs with
|
||||||
[] -> raise (Pdf.PDFError "no input in recryption")
|
[] -> raise (Pdf.PDFError "no input in recryption")
|
||||||
| (_, _, _, user_pw, owner_pw, _)::_ ->
|
| (_, _, _, 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
|
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
|
||||||
|
@ -1960,15 +1962,17 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna
|
||||||
begin
|
begin
|
||||||
if not args.was_encrypted || args.was_decrypted_with_owner then
|
if not args.was_encrypted || args.was_decrypted_with_owner then
|
||||||
begin
|
begin
|
||||||
if args.debugcrypt then Printf.printf "Pdf to file in really_write_pdf\n";
|
if args.debugcrypt then
|
||||||
Pdfwrite.pdf_to_file_options
|
Printf.printf "Pdf to file in really_write_pdf\n";
|
||||||
~preserve_objstm:args.preserve_objstm
|
Pdfwrite.pdf_to_file_options
|
||||||
~generate_objstm:args.create_objstm
|
~preserve_objstm:args.preserve_objstm
|
||||||
~compress_objstm:(not is_decompress)
|
~generate_objstm:args.create_objstm
|
||||||
false encryption mk_id pdf outname'
|
~compress_objstm:(not is_decompress)
|
||||||
|
false encryption mk_id pdf outname'
|
||||||
end
|
end
|
||||||
else
|
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
|
||||||
end;
|
end;
|
||||||
begin
|
begin
|
||||||
|
@ -1978,16 +1982,25 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna
|
||||||
Some x -> x
|
Some x -> x
|
||||||
| None -> raise (Pdf.PDFError "Could not find cpdflin")
|
| None -> raise (Pdf.PDFError "Could not find cpdflin")
|
||||||
in
|
in
|
||||||
let best_password = if args.owner <> "" then args.owner else args.user in
|
match args.inputs with
|
||||||
let code = Cpdf.call_cpdflin cpdflin outname' outname best_password in
|
[] -> raise (Pdf.PDFError "no input in recryption")
|
||||||
if code > 0 then
|
| (_, _, _, user_pw, owner_pw, _)::_ ->
|
||||||
begin
|
let best_password =
|
||||||
begin try Sys.remove outname with _ -> () end;
|
if owner_pw <> "" then owner_pw else user_pw
|
||||||
Sys.rename outname' outname;
|
in
|
||||||
soft_error "Linearizer failed with above error. File written without linearization."
|
let code =
|
||||||
end
|
Cpdf.call_cpdflin cpdflin outname' outname best_password
|
||||||
else
|
in
|
||||||
begin try Sys.remove outname' with _ -> () end;
|
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;
|
end;
|
||||||
if args.squeeze then
|
if args.squeeze then
|
||||||
let s = filesize outname in
|
let s = filesize outname in
|
||||||
|
|
Loading…
Reference in New Issue