Implemented -info-json

This commit is contained in:
John Whitington 2023-11-01 14:55:14 +00:00
parent 45c8213e80
commit c082deacfa
3 changed files with 86 additions and 37 deletions

View File

@ -3471,19 +3471,41 @@ let go () =
| (AlreadyInMemory pdf, _, _, _, _, _) as input::_ -> pdf, "", input | (AlreadyInMemory pdf, _, _, _, _, _) as input::_ -> pdf, "", input
| _ -> raise (Arg.Bad "cpdf: No input specified.\n") | _ -> raise (Arg.Bad "cpdf: No input specified.\n")
in in
Printf.printf "Encryption: %s\n" (getencryption pdf); let json = ref [] in
Printf.printf "Permissions: %s\n" (getpermissions pdf); if args.format_json
then json =| ("Encryption", `String (getencryption pdf))
else Printf.printf "Encryption: %s\n" (getencryption pdf);
if args.format_json
then json =| ("Permissions", `List (map (fun p -> `String (string_of_permission p)) (Pdfread.permissions pdf)))
else Printf.printf "Permissions: %s\n" (getpermissions pdf);
if inname <> "" then if inname <> "" then
Printf.printf "Linearized: %b\n" (Pdfread.is_linearized (Pdfio.input_of_channel (open_in_bin inname))); let lin = Pdfread.is_linearized (Pdfio.input_of_channel (open_in_bin inname)) in
Printf.printf "Object streams: %b\n" (length (list_of_hashtbl pdf.Pdf.objects.Pdf.object_stream_ids) > 0); if args.format_json then
Printf.printf "ID: %s\n" json =| ("Linearized", `Bool lin) else Printf.printf "Linearized: %b\n" lin;
(match let objstm = length (list_of_hashtbl pdf.Pdf.objects.Pdf.object_stream_ids) > 0 in
Pdf.lookup_direct pdf "/ID" pdf.Pdf.trailerdict with if args.format_json
| Some (Pdf.Array [Pdf.String s; Pdf.String s']) -> Printf.sprintf "%s %s" (Pdfwrite.make_hex_pdf_string s) (Pdfwrite.make_hex_pdf_string s'); then json =| ("Object streams", `Bool objstm)
| _ -> "None"); else Printf.printf "Object streams: %b\n" objstm;
let ida, idb =
match Pdf.lookup_direct pdf "/ID" pdf.Pdf.trailerdict with
| Some (Pdf.Array [Pdf.String s; Pdf.String s']) ->
(Pdfwrite.make_hex_pdf_string s, Pdfwrite.make_hex_pdf_string s')
| _ -> "", ""
in
let fixid s = implode (rev (tl (rev (tl (explode s))))) in
if args.format_json
then json =| ("ID", if ida ^ idb = "" then `Null else `List [`String (fixid ida); `String (fixid idb)])
else (if ida ^ idb = "" then Printf.printf "ID: None" else Printf.printf "ID: %s %s\n" ida idb);
let pdf = decrypt_if_necessary input (Some Info) pdf in let pdf = decrypt_if_necessary input (Some Info) pdf in
Cpdfmetadata.output_info args.encoding pdf; if args.format_json then
Cpdfmetadata.output_xmp_info args.encoding pdf Cpdfmetadata.output_info ~json Cpdfmetadata.UTF8 pdf;
Cpdfmetadata.output_xmp_info ~json Cpdfmetadata.UTF8 pdf;
flprint (Cpdfyojson.Safe.pretty_to_string (`Assoc (rev !json)))
else
begin
Cpdfmetadata.output_info args.encoding pdf;
Cpdfmetadata.output_xmp_info args.encoding pdf
end
| Some PageInfo -> | Some PageInfo ->
begin match args.inputs, args.out with begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ -> | (_, pagespec, _, _, _, _)::_, _ ->

View File

@ -188,28 +188,49 @@ let get_viewer_pref_item name pdf =
end end
| _ -> "" | _ -> ""
let output_info encoding pdf = let output_info ?(json=ref [("none", `Null)]) encoding pdf =
let notjson = !json = [("none", `Null)] in
let getstring = getstring encoding pdf in let getstring = getstring encoding pdf in
Printf.printf "Version: %i.%i\n" pdf.Pdf.major pdf.Pdf.minor; if notjson then Printf.printf "Version: %i.%i\n" pdf.Pdf.major pdf.Pdf.minor;
Printf.printf "Pages: %i\n" (Pdfpage.endpage pdf); json =| ("Version", `List [`Int pdf.Pdf.major; `Int pdf.Pdf.minor]);
Printf.printf "Title: %s\n" (getstring "/Title"); if notjson then Printf.printf "Pages: %i\n" (Pdfpage.endpage pdf);
Printf.printf "Author: %s\n" (getstring "/Author"); json =| ("Pages", `Int (Pdfpage.endpage pdf));
Printf.printf "Subject: %s\n" (getstring "/Subject"); if notjson then Printf.printf "Title: %s\n" (getstring "/Title");
Printf.printf "Keywords: %s\n" (getstring "/Keywords"); json =| ("Title", `String (getstring "/Title"));
Printf.printf "Creator: %s\n" (getstring "/Creator"); if notjson then Printf.printf "Author: %s\n" (getstring "/Author");
Printf.printf "Producer: %s\n" (getstring "/Producer"); json =| ("Author", `String (getstring "/Author"));
Printf.printf "Created: %s\n" (getstring "/CreationDate"); if notjson then Printf.printf "Subject: %s\n" (getstring "/Subject");
Printf.printf "Modified: %s\n" (getstring "/ModDate"); json =| ("Subject", `String (getstring "/Subject"));
Printf.printf "Trapped: %s\n" (getstring "/Trapped"); if notjson then Printf.printf "Keywords: %s\n" (getstring "/Keywords");
Printf.printf "PageMode: %s\n" (get_catalog_item "/PageMode" pdf); json =| ("Keywords", `String (getstring "/Keywords"));
Printf.printf "PageLayout: %s\n" (get_catalog_item "/PageLayout" pdf); if notjson then Printf.printf "Creator: %s\n" (getstring "/Creator");
Printf.printf "HideToolbar: %s\n" (get_viewer_pref_item "/HideToolbar" pdf); json =| ("Creator", `String (getstring "/Creator"));
Printf.printf "HideMenubar: %s\n" (get_viewer_pref_item "/HideMenubar" pdf); if notjson then Printf.printf "Producer: %s\n" (getstring "/Producer");
Printf.printf "HideWindowUI: %s\n" (get_viewer_pref_item "/HideWindowUI" pdf); json =| ("Producer", `String (getstring "/Producer"));
Printf.printf "FitWindow: %s\n" (get_viewer_pref_item "/FitWindow" pdf); if notjson then Printf.printf "Created: %s\n" (getstring "/CreationDate");
Printf.printf "CenterWindow: %s\n" (get_viewer_pref_item "/CenterWindow" pdf); json =| ("Created", `String (getstring "/CreationDate"));
Printf.printf "DisplayDocTitle: %s\n" (get_viewer_pref_item "/DisplayDocTitle" pdf); if notjson then Printf.printf "Modified: %s\n" (getstring "/ModDate");
Printf.printf "NonFullScreenPageMode: %s\n" (get_viewer_pref_item "/NonFullScreenPageMode" pdf); json =| ("Modified", `String (getstring "/ModDate"));
if notjson then Printf.printf "Trapped: %s\n" (getstring "/Trapped");
json =| ("Trapped", `Bool (bool_of_string (String.lowercase_ascii (getstring "/Trapped"))));
if notjson then Printf.printf "PageMode: %s\n" (get_catalog_item "/PageMode" pdf);
json =| ("PageMode", match (get_catalog_item "/PageMode" pdf) with "" -> `Null | x -> `String x);
if notjson then Printf.printf "PageLayout: %s\n" (get_catalog_item "/PageLayout" pdf);
json =| ("PageLayout", match (get_catalog_item "/PageLayout" pdf) with "" -> `Null | x -> `String x);
if notjson then Printf.printf "HideToolbar: %s\n" (get_viewer_pref_item "/HideToolbar" pdf);
json =| ("HideToolbar", match get_viewer_pref_item "/HideToolbar" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "HideMenubar: %s\n" (get_viewer_pref_item "/HideMenubar" pdf);
json =| ("HideMenubar", match get_viewer_pref_item "/HideMenubar" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "HideWindowUI: %s\n" (get_viewer_pref_item "/HideWindowUI" pdf);
json =| ("HideWindowUI", match get_viewer_pref_item "/HideWindowUI" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "FitWindow: %s\n" (get_viewer_pref_item "/FitWindow" pdf);
json =| ("FitWindow", match get_viewer_pref_item "/FitWindow" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "CenterWindow: %s\n" (get_viewer_pref_item "/CenterWindow" pdf);
json =| ("CenterWindow", match get_viewer_pref_item "/CenterWindow" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "DisplayDocTitle: %s\n" (get_viewer_pref_item "/DisplayDocTitle" pdf);
json =| ("DisplayDocTitle", match get_viewer_pref_item "/DisplayDocTitle" pdf with "" -> `Null | s -> `Bool (bool_of_string s));
if notjson then Printf.printf "NonFullScreenPageMode: %s\n" (get_viewer_pref_item "/NonFullScreenPageMode" pdf);
json =| ("NonFullPageScreenMode", match (get_viewer_pref_item "/NonFullPageScreenMode" pdf) with "" -> `Null | x -> `String x);
type xmltree = type xmltree =
E of Cpdfxmlm.tag * xmltree list E of Cpdfxmlm.tag * xmltree list
@ -298,13 +319,19 @@ let rec get_data_for namespace name = function
x :: _ -> Some x x :: _ -> Some x
| _ -> None | _ -> None
let output_xmp_info encoding pdf = let output_xmp_info ?(json=ref [("none", `Null)]) encoding pdf =
let notjson = !json = [("none", `Null)] in
let print_out tree title namespace name = let print_out tree title namespace name =
match get_data_for namespace name tree with match get_data_for namespace name tree with
None -> () None -> ()
| Some data -> | Some data ->
Printf.printf "%s: " title; if notjson then
print_endline data begin
Printf.printf "%s: " title;
print_endline data
end
else
json =| (title, `String data)
in in
match get_metadata pdf with match get_metadata pdf with
None -> () None -> ()

View File

@ -49,10 +49,10 @@ from the /Info dictionary, returning it as a UTF8 string *)
val get_info_utf8 : Pdf.t -> string -> string val get_info_utf8 : Pdf.t -> string -> string
(** Output to standard output general information about a PDF. *) (** Output to standard output general information about a PDF. *)
val output_info : encoding -> Pdf.t -> unit val output_info : ?json:(string * Cpdfyojson.Safe.t) list ref -> encoding -> Pdf.t -> unit
(** Output to standard output information from any XMP metadata stream in a PDF. *) (** Output to standard output information from any XMP metadata stream in a PDF. *)
val output_xmp_info : encoding -> Pdf.t -> unit val output_xmp_info : ?json:(string * Cpdfyojson.Safe.t) list ref -> encoding -> Pdf.t -> unit
(** Create XMP metadata from scratch *) (** Create XMP metadata from scratch *)
val create_metadata : Pdf.t -> Pdf.t val create_metadata : Pdf.t -> Pdf.t