mirror of
https://github.com/johnwhitington/cpdf-source.git
synced 2025-02-16 11:50:54 +01:00
Simplifying linearizer finding
This commit is contained in:
parent
68c1516c4b
commit
2edfe9ff1b
54
cpdf.ml
54
cpdf.ml
@ -2,49 +2,18 @@
|
|||||||
open Pdfutil
|
open Pdfutil
|
||||||
open Pdfio
|
open Pdfio
|
||||||
|
|
||||||
let system_path_separator =
|
(* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume
|
||||||
match Sys.os_type with "Win32" -> ';' | _ -> ':'
|
installed at a system place *)
|
||||||
|
|
||||||
(* Find the location of the cpdflin binary, either in a provided place (with
|
|
||||||
-cpdflin), or in the current directory or in $PATH. *)
|
|
||||||
let rec paths_of_chars prev = function
|
|
||||||
[] -> keep (fun x -> x <> "") (map implode (rev (map rev prev)))
|
|
||||||
| pathsep::t when pathsep = system_path_separator ->
|
|
||||||
paths_of_chars ([] :: prev) t
|
|
||||||
| h::t ->
|
|
||||||
match prev with
|
|
||||||
[] -> paths_of_chars [[h]] t
|
|
||||||
| ph::pt -> paths_of_chars ((h :: ph) :: pt) t
|
|
||||||
|
|
||||||
let paths () =
|
|
||||||
paths_of_chars [] (explode (Sys.getenv "PATH"))
|
|
||||||
|
|
||||||
let is_at_path file path =
|
|
||||||
let full = path ^ Filename.dir_sep ^ file in
|
|
||||||
Printf.printf "Looking for %s\n%!" full;
|
|
||||||
if Sys.file_exists full then Some full else None
|
|
||||||
|
|
||||||
let find_cpdflin provided =
|
let find_cpdflin provided =
|
||||||
match provided with
|
match provided with
|
||||||
Some x -> Some x
|
Some x -> x
|
||||||
| None ->
|
| None ->
|
||||||
let dotslash = match Sys.os_type with "Win32" -> "" | _ -> "./" in
|
let dotslash = match Sys.os_type with "Win32" -> "" | _ -> "./" in
|
||||||
if Sys.file_exists "cpdflin" then Some (dotslash ^ "cpdflin") else
|
if Sys.file_exists "cpdflin" then (dotslash ^ "cpdflin") else
|
||||||
if Sys.file_exists "cpdflin.exe" then Some (dotslash ^ "cpdflin.exe") else
|
if Sys.file_exists "cpdflin.exe" then (dotslash ^ "cpdflin.exe") else
|
||||||
if Sys.file_exists "qpdf" then Some (dotslash ^ "qpdf") else
|
match Sys.os_type with
|
||||||
if Sys.file_exists "qpdf.exe" then Some (dotslash ^ "qpdf.exe") else
|
"Win32" -> "cpdflin.exe"
|
||||||
match option_map (is_at_path "cpdflin") (paths ()) with
|
| _ -> "cpdflin"
|
||||||
h::_ -> Some (Filename.quote h)
|
|
||||||
| _ ->
|
|
||||||
match option_map (is_at_path "cpdflin.exe") (paths ()) with
|
|
||||||
h::_ -> Some (Filename.quote h)
|
|
||||||
| _ ->
|
|
||||||
match option_map (is_at_path "qpdf") (paths ()) with
|
|
||||||
h::_ -> Some (Filename.quote h)
|
|
||||||
| _ ->
|
|
||||||
match option_map (is_at_path "qpdf") (paths ()) with
|
|
||||||
h::_ -> Some (Filename.quote h)
|
|
||||||
| _ -> None
|
|
||||||
|
|
||||||
(* Call cpdflin, given the (temp) input name, the output name, and the location
|
(* Call cpdflin, given the (temp) input name, the output name, and the location
|
||||||
of the cpdflin binary. Returns the exit code. *)
|
of the cpdflin binary. Returns the exit code. *)
|
||||||
@ -59,8 +28,11 @@ let call_cpdflin cpdflin temp output best_password =
|
|||||||
(* On windows, don't use LD_LIBRARY_PATH - it will happen automatically *)
|
(* On windows, don't use LD_LIBRARY_PATH - it will happen automatically *)
|
||||||
Sys.command command
|
Sys.command command
|
||||||
| _ ->
|
| _ ->
|
||||||
(* On linux, set LD_LIBRARY_PATH to the stem of cpdflin *)
|
(* On other platforms, if -cpdflin was provided, or cpdflin was in the
|
||||||
Sys.command ("LD_LIBRARY_PATH=" ^ Filename.dirname cpdflin ^ " " ^ command)
|
current folder, set up LD_LIBRARY_PATH: *)
|
||||||
|
match cpdflin with
|
||||||
|
"cpdflin" -> Sys.command (cpdflin ^ " " ^ command)
|
||||||
|
| _ -> Sys.command ("LD_LIBRARY_PATH=" ^ Filename.dirname cpdflin ^ " " ^ command)
|
||||||
|
|
||||||
(* Recompress anything which isn't compressed, unless it's metadata. *)
|
(* Recompress anything which isn't compressed, unless it's metadata. *)
|
||||||
let recompress_stream pdf = function
|
let recompress_stream pdf = function
|
||||||
|
2
cpdf.mli
2
cpdf.mli
@ -472,7 +472,7 @@ val custom_csp2 : float -> Pdf.t -> Pdf.t
|
|||||||
(** Nobble a page, given pdf, pagenumber and page *)
|
(** Nobble a page, given pdf, pagenumber and page *)
|
||||||
val nobble_page : Pdf.t -> 'a -> Pdfpage.t -> Pdfpage.t
|
val nobble_page : Pdf.t -> 'a -> Pdfpage.t -> Pdfpage.t
|
||||||
|
|
||||||
val find_cpdflin : string option -> string option
|
val find_cpdflin : string option -> string
|
||||||
|
|
||||||
val call_cpdflin : string -> string -> string -> string -> int
|
val call_cpdflin : string -> string -> string -> string -> int
|
||||||
|
|
||||||
|
@ -1963,11 +1963,7 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna
|
|||||||
end;
|
end;
|
||||||
begin
|
begin
|
||||||
if args.linearize then
|
if args.linearize then
|
||||||
let cpdflin =
|
let cpdflin = Cpdf.find_cpdflin args.cpdflin in
|
||||||
match Cpdf.find_cpdflin args.cpdflin with
|
|
||||||
Some x -> x
|
|
||||||
| None -> raise (Pdf.PDFError "Could not find cpdflin")
|
|
||||||
in
|
|
||||||
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, _)::_ ->
|
||||||
|
Loading…
x
Reference in New Issue
Block a user