cpdf-source/cpdfcommand.ml

4666 lines
163 KiB
OCaml
Raw Normal View History

2013-10-02 16:29:53 +02:00
(* cpdf command line tools *)
let demo = false
2020-01-30 11:42:24 +01:00
let noncomp = false
2014-11-17 19:48:17 +01:00
let major_version = 2
let minor_version = 4
let version_date = "(devel, 6th May 2020)"
2013-08-20 16:32:57 +02:00
open Pdfutil
open Pdfio
2019-06-30 16:00:51 +02:00
let tempfiles = ref []
let exit n =
2020-03-04 19:50:32 +01:00
begin try iter Sys.remove !tempfiles with _ -> exit n end;
2019-06-30 16:00:51 +02:00
exit n
2014-10-14 20:36:57 +02:00
let initial_file_size = ref 0
2016-07-21 18:02:11 +02:00
let empty = Pdf.empty ()
let emptypage = Pdfpage.blankpage Pdfpaper.a4
2013-08-20 16:32:57 +02:00
(* Wrap up the file reading functions to exit with code 1 when an encryption
problem occurs. This happens when object streams are in an encrypted document
and so it can't be read without the right password... The existing error
handling only dealt with the case where the document couldn't be decrypted once
it had been loaded. *)
let pdfread_pdf_of_input ?revision a b c =
try Pdfread.pdf_of_input ?revision a b c with
2013-08-20 16:32:57 +02:00
Pdf.PDFError s when String.length s >=10 && String.sub s 0 10 = "Encryption" ->
raise (Cpdf.SoftError "Bad owner or user password when reading document")
let pdfread_pdf_of_channel_lazy ?revision ?source b c d =
try Pdfread.pdf_of_channel_lazy ?revision ?source b c d with
2013-08-20 16:32:57 +02:00
Pdf.PDFError s when String.length s >=10 && String.sub s 0 10 = "Encryption" ->
raise (Cpdf.SoftError "Bad owner or user password when reading document")
let pdfread_pdf_of_file ?revision a b c =
try Pdfread.pdf_of_file ?revision a b c with
2013-08-20 16:32:57 +02:00
Pdf.PDFError s when String.length s >=10 && String.sub s 0 10 = "Encryption" ->
raise (Cpdf.SoftError "Bad owner or user password when reading document")
let optstring = function
| "" -> None
| x -> Some x
(* To prevent problems when piping on Windows *)
let _ =
set_binary_mode_in stdin true;
set_binary_mode_out stdout true
let stay_on_error = ref false
exception StayOnError
2013-08-20 16:32:57 +02:00
(* Fatal error reporting. *)
let error s =
prerr_string (s ^ "\nUse -help for help.\n");
flush stderr;
if not !stay_on_error then exit 2 else raise StayOnError
2013-08-20 16:32:57 +02:00
let soft_error s =
Printf.eprintf "%s\n" s;
flush stderr;
if not !stay_on_error then exit 1 else raise StayOnError
2013-08-20 16:32:57 +02:00
let parse_pagespec pdf spec =
try Cpdf.parse_pagespec pdf spec with
Failure x -> error x
(* Operations. *)
type op =
| CopyFont of string
| CSP1
| CSP2 of float
| CSP3
| CountPages
| Version
| Encrypt
| Decrypt
| StampOn of string
| StampUnder of string
| CombinePages of string
| TwoUp
| TwoUpStack
| RemoveBookmarks
| AddBookmarks of string
| AddText of string
| AddRectangle
| RemoveText
| Draft
| PadBefore
| PadAfter
| PadEvery of int
| PadMultiple of int
2019-07-01 16:35:17 +02:00
| PadMultipleBefore of int
2013-08-20 16:32:57 +02:00
| Shift
| Scale
| ScaleToFit
| ScaleContents of float
| AttachFile of string list
| RemoveAttachedFiles
| ListAttachedFiles
| DumpAttachedFiles
| RemoveAnnotations
| ListAnnotations
| ListAnnotationsMore
| CopyAnnotations of string
| Merge
| Split
| SplitOnBookmarks of int
| Clean
| Info
| PageInfo
| Metadata
| SetMetadata of string
| RemoveMetadata
| Fonts
| RemoveFonts
| Compress
| Decompress
| Crop
| Trim
| Bleed
| Art
2013-08-20 16:32:57 +02:00
| RemoveCrop
| RemoveArt
| RemoveTrim
| RemoveBleed
2013-08-20 16:32:57 +02:00
| CopyCropBoxToMediaBox
| CopyBox
| MediaBox
2017-05-19 20:10:49 +02:00
| HardBox of string
2013-08-20 16:32:57 +02:00
| Rotate of int
| Rotateby of int
| RotateContents of float
| Upright
| VFlip
| HFlip
| ThinLines of float
| SetAuthor of string
| SetTitle of string
| SetSubject of string
| SetKeywords of string
| SetCreate of string
| SetModify of string
| SetCreator of string
| SetProducer of string
| SetTrapped
| SetUntrapped
| SetVersion of int
| ListBookmarks
| SetPageLayout of string
| SetPageMode of string
| HideToolbar of bool
| HideMenubar of bool
| HideWindowUI of bool
| FitWindow of bool
| CenterWindow of bool
| DisplayDocTitle of bool
| Presentation
| ChangeId
| RemoveId
| CopyId of string
| BlackText
| BlackLines
| BlackFills
| ExtractImages
| ImageResolution of float
| MissingFonts
| RemoveUnusedResources
| ExtractFontFile
| ExtractText
| OpenAtPage of string
| OpenAtPageFit of string
2013-10-24 16:21:54 +02:00
| AddPageLabels
| RemovePageLabels
| PrintPageLabels
| Revisions
2015-01-07 19:29:11 +01:00
| RemoveDictEntry of string
2016-11-03 18:11:08 +01:00
| ListSpotColours
2016-11-09 16:42:47 +01:00
| RemoveClipping
| SetMetadataDate of string
2019-07-01 15:40:22 +02:00
| CreateMetadata
2019-07-02 19:20:05 +02:00
| EmbedMissingFonts
2019-07-06 18:55:26 +02:00
| BookmarksOpenToLevel of int
| CreatePDF
2019-07-11 18:19:40 +02:00
| RemoveAllText
2019-07-15 12:52:14 +02:00
| ShowBoxes
2019-07-15 14:42:32 +02:00
| TrimMarks
| Prepend of string
| Postpend of string
2020-01-30 11:20:33 +01:00
| OutputJSON
2020-02-26 17:24:27 +01:00
| OCGCoalesce
2020-02-27 14:32:45 +01:00
| OCGList
| OCGRename
2020-03-02 13:37:39 +01:00
| OCGOrderAll
| StampAsXObject of string
2013-08-20 16:32:57 +02:00
let string_of_op = function
| CopyFont _ -> "CopyFont"
| CSP1 -> "CSP1"
| CSP2 _ -> "CSP2"
| CSP3 -> "CSP3"
| CountPages -> "CountPages"
| Version -> "Version"
| Encrypt -> "Encrypt"
| Decrypt -> "Decrypt"
| StampOn _ -> "StampOn"
| StampUnder _ -> "StampUnder"
| CombinePages _ -> "CombinePages"
| TwoUp -> "TwoUp"
| TwoUpStack -> "TwoUpStack"
| RemoveBookmarks -> "RemoveBookmarks"
| AddBookmarks _ -> "AddBookmarks"
| AddText _ -> "AddText"
| AddRectangle -> "AddRectangle"
| RemoveText -> "RemoveText"
| Draft -> "Draft"
| PadBefore -> "PadBefore"
| PadAfter -> "PadAfter"
| PadEvery _ -> "PadEvery"
| PadMultiple _ -> "PadMultiple"
2019-07-01 16:35:17 +02:00
| PadMultipleBefore _ -> "PadMultipleBefore"
| Shift -> "Shift"
| Scale -> "Scale"
| ScaleToFit -> "ScaleToFit"
| ScaleContents _ -> "ScaleContents"
| AttachFile _ -> "AttachFile"
| RemoveAttachedFiles -> "RemoveAttachedFiles"
| ListAttachedFiles -> "ListAttachedFiles"
| DumpAttachedFiles -> "DumpAttachedFiles"
| RemoveAnnotations -> "RemoveAnnotations"
| ListAnnotations -> "ListAnnotations"
| ListAnnotationsMore -> "ListAnnotationsMore"
| CopyAnnotations _ -> "CopyAnnotations"
| Merge -> "Merge"
| Split -> "Split"
| SplitOnBookmarks _ -> "SplitOnBookmarks"
| Clean -> "Clean"
| Info -> "Info"
| PageInfo -> "PageInfo"
| Metadata -> "Metadata"
| SetMetadata _ -> "SetMetadata"
| RemoveMetadata -> "RemoveMetadata"
| Fonts -> "Fonts"
| RemoveFonts -> "RemoveFonts"
| Compress -> "Compress"
| Decompress -> "Decompress"
| Crop -> "Crop"
| RemoveCrop -> "RemoveCrop"
| CopyCropBoxToMediaBox -> "CopyCropBoxToMediaBox"
| CopyBox -> "CopyBox"
| MediaBox -> "MediaBox"
2017-05-19 20:10:49 +02:00
| HardBox _ -> "HardBox"
| Rotate _ -> "Rotate"
| Rotateby _ -> "Rotateby"
| RotateContents _ -> "RotateContents"
| Upright -> "Upright"
| VFlip -> "VFlip"
| HFlip -> "HFlip"
| ThinLines _ -> "ThinLines"
| SetAuthor _ -> "SetAuthor"
| SetTitle _ -> "SetTitle"
| SetSubject _ -> "SetSubject"
| SetKeywords _ -> "SetKeywords"
| SetCreate _ -> "SetCreate"
| SetModify _ -> "SetModify"
| SetCreator _ -> "SetCreator"
| SetProducer _ -> "SetProducer"
| SetTrapped -> "SetTrapped"
| SetUntrapped -> "SetUntrapped"
| SetVersion _ -> "SetVersion"
| ListBookmarks -> "ListBookmarks"
| SetPageLayout _ -> "SetPageLayout"
| SetPageMode _ -> "SetPageMode"
| HideToolbar _ -> "HideToolbar"
| HideMenubar _ -> "HideMenubar"
| HideWindowUI _ -> "HideWindowUI"
| FitWindow _ -> "FitWindow"
| CenterWindow _ -> "CenterWindow"
| DisplayDocTitle _ -> "DisplayDocTitle"
| Presentation -> "Presentation"
| ChangeId -> "ChangeId"
| RemoveId -> "RemoveId"
| CopyId _ -> "CopyId"
| BlackText -> "BlackText"
| BlackLines -> "BlackLines"
| BlackFills -> "BlackFills"
| ExtractImages -> "ExtractImages"
| ImageResolution _ -> "ImageResolution"
| MissingFonts -> "MissingFonts"
| RemoveUnusedResources -> "RemoveUnusedResources"
| ExtractFontFile -> "ExtractFontFile"
| ExtractText -> "ExtractText"
| OpenAtPage _ -> "OpenAtPage"
| OpenAtPageFit _ -> "OpenAtPageFit"
| AddPageLabels -> "AddPageLabels"
| RemovePageLabels -> "RemovePageLabels"
| PrintPageLabels -> "PrintPageLabels"
| Revisions -> "Revisions"
2015-01-07 19:29:11 +01:00
| RemoveDictEntry _ -> "RemoveDictEntry"
2016-11-03 18:11:08 +01:00
| ListSpotColours -> "ListSpotColours"
2016-11-09 16:42:47 +01:00
| RemoveClipping -> "RemoveClipping"
| Trim -> "Trim"
| Art -> "Art"
| Bleed -> "Bleed"
| RemoveArt -> "RemoveArt"
| RemoveTrim -> "RemoveTrim"
| RemoveBleed -> "RemoveBleed"
| SetMetadataDate _ -> "SetMetadataDate"
2019-07-01 15:40:22 +02:00
| CreateMetadata -> "CreateMetadata"
2019-07-02 19:20:05 +02:00
| EmbedMissingFonts -> "EmbedMissingFonts"
2019-07-06 18:55:26 +02:00
| BookmarksOpenToLevel _ -> "BookmarksOpenToLevel"
| CreatePDF -> "CreatePDF"
2019-07-11 18:19:40 +02:00
| RemoveAllText -> "RemoveAllText"
2019-07-15 12:52:14 +02:00
| ShowBoxes -> "ShowBoxes"
2019-07-15 14:42:32 +02:00
| TrimMarks -> "TrimMarks"
| Prepend _ -> "Prepend"
| Postpend _ -> "Postpend"
2020-01-30 11:20:33 +01:00
| OutputJSON -> "OutputJSON"
2020-02-26 17:24:27 +01:00
| OCGCoalesce -> "OCGCoalesce"
2020-02-27 14:32:45 +01:00
| OCGList -> "OCGList"
| OCGRename -> "OCGRename"
2020-03-02 13:37:39 +01:00
| OCGOrderAll -> "OCGOrderAll"
| StampAsXObject _ -> "StampAsXObject"
2013-08-20 16:32:57 +02:00
(* Inputs: filename, pagespec. *)
type input_kind =
| AlreadyInMemory of Pdf.t
| InFile of string
| StdIn
let string_of_input_kind = function
| AlreadyInMemory _ -> "AlreadyInMemory"
| InFile s -> s
| StdIn -> "Stdin"
type input =
input_kind * string * string * string * bool ref * int option
(* input kind, range, user_pw, owner_pw, was_decrypted_with_owner, revision *)
2013-08-20 16:32:57 +02:00
type output_method =
| NoOutputSpecified
| Stdout
| File of string
(* A list of PDFs to be output, if no output method was specified. *)
let output_pdfs : Pdf.t list ref = ref []
type font =
| StandardFont of Pdftext.standard_font
| OtherFont of string
type args =
{mutable op : op option;
mutable preserve_objstm : bool;
mutable create_objstm : bool;
mutable out : output_method;
mutable inputs : input list;
mutable chunksize : int;
mutable linearize : bool;
mutable keeplinearize : bool;
mutable rectangle : string;
mutable coord : string;
2013-08-20 16:32:57 +02:00
mutable duration : float option;
mutable transition : string option;
mutable horizontal : bool;
mutable inward : bool;
mutable direction : int;
mutable effect_duration : float;
mutable font : font;
mutable fontname : string;
mutable fontsize : float;
mutable color : float * float * float;
mutable opacity : float;
mutable position : Cpdf.position;
mutable underneath : bool;
mutable linespacing : float;
mutable midline : bool;
2015-01-20 16:50:36 +01:00
mutable topline : bool;
2013-08-20 16:32:57 +02:00
mutable justification : Cpdf.justification;
mutable bates : int;
2015-07-17 17:34:47 +02:00
mutable batespad : int option;
2013-08-20 16:32:57 +02:00
mutable prerotate : bool;
mutable orientation : Cpdf.orientation;
mutable relative_to_cropbox : bool;
mutable keepversion : bool;
mutable bycolumns : bool;
mutable pagerotation : int;
mutable crypt_method : string;
mutable owner : string;
mutable user : string;
mutable no_edit : bool;
mutable no_print : bool;
mutable no_copy : bool;
mutable no_annot : bool;
mutable no_forms : bool;
mutable no_extract : bool;
mutable no_assemble : bool;
mutable no_hq_print : bool;
mutable debug : bool;
2014-10-24 18:24:29 +02:00
mutable debugcrypt : bool;
2018-03-20 11:19:50 +01:00
mutable debugforce : bool;
2013-08-20 16:32:57 +02:00
mutable boxes : bool;
mutable encrypt_metadata : bool;
mutable retain_numbering : bool;
mutable remove_duplicate_fonts : bool;
mutable remove_duplicate_streams : bool;
mutable encoding : Cpdf.encoding;
mutable scale : float;
mutable copyfontpage : int;
mutable copyfontname : string option;
mutable fast : bool;
mutable dashrange : string;
mutable outline : bool;
mutable linewidth : float;
mutable path_to_ghostscript : string;
mutable frombox : string option;
mutable tobox : string option;
mutable mediabox_if_missing : bool;
mutable topage : string option;
mutable scale_stamp_to_fit : bool;
mutable keep_this_id : string option;
mutable makenewid : bool;
2014-09-18 15:27:07 +02:00
mutable labelstyle : Pdfpagelabels.labelstyle;
2013-10-24 16:21:54 +02:00
mutable labelprefix : string option;
2014-09-18 16:40:22 +02:00
mutable labelstartval : int;
mutable labelsprogress : bool;
mutable squeeze : bool;
mutable squeeze_recompress : bool;
mutable squeeze_pagedata: bool;
2014-10-02 20:57:06 +02:00
mutable original_filename : string;
mutable was_encrypted : bool;
2014-10-15 14:48:39 +02:00
mutable cpdflin : string option;
mutable recrypt : bool;
2015-01-22 20:16:56 +01:00
mutable was_decrypted_with_owner : bool;
mutable creator : string option;
2015-04-13 15:17:48 +02:00
mutable producer : string option;
2016-11-09 19:15:23 +01:00
mutable embedfonts : bool;
2017-12-18 20:44:02 +01:00
mutable extract_text_font_size : float option;
mutable padwith : string option;
mutable alsosetxml : bool;
mutable justsetxml : bool;
2019-07-03 15:40:32 +02:00
mutable gs_malformed : bool;
2019-07-22 15:00:37 +02:00
mutable gs_quiet : bool;
2019-07-03 15:40:32 +02:00
mutable merge_add_bookmarks : bool;
mutable merge_add_bookmarks_use_titles : bool;
mutable createpdf_pages : int;
2019-07-09 17:31:45 +02:00
mutable createpdf_pagesize : Pdfpaper.t;
2020-01-30 11:20:33 +01:00
mutable removeonly : string option;
2020-02-01 11:18:15 +01:00
mutable jsonparsecontentstreams : bool;
2020-02-27 14:32:45 +01:00
mutable jsonnostreamdata : bool;
mutable ocgrenamefrom : string;
mutable ocgrenameto : string}
2013-08-20 16:32:57 +02:00
let args =
{op = None;
preserve_objstm = true;
create_objstm = false;
out = NoOutputSpecified;
inputs = [];
chunksize = 1;
linearize = false;
keeplinearize = false;
rectangle = "0 0 0 0";
coord = "0 0";
2013-08-20 16:32:57 +02:00
duration = None;
transition = None;
horizontal = true;
inward = true;
direction = 0;
effect_duration = 1.;
font = StandardFont Pdftext.TimesRoman;
fontname = "Times-Roman";
fontsize = 12.;
color = 0., 0., 0.;
opacity = 1.;
position = Cpdf.TopLeft 100.;
underneath = false;
linespacing = 1.;
midline = false;
2015-01-20 16:50:36 +01:00
topline = false;
2013-08-20 16:32:57 +02:00
justification = Cpdf.LeftJustify;
bates = 0;
2015-07-17 17:34:47 +02:00
batespad = None;
2013-08-20 16:32:57 +02:00
prerotate = false;
orientation = Cpdf.Horizontal;
relative_to_cropbox = false;
keepversion = false;
bycolumns = false;
pagerotation = 0;
crypt_method = "";
owner = "";
user = "";
no_edit = false;
no_print = false;
no_copy = false;
no_annot = false;
no_forms = false;
no_extract = false;
no_assemble = false;
no_hq_print = false;
debug = false;
2014-10-24 18:24:29 +02:00
debugcrypt = false;
2018-03-20 11:19:50 +01:00
debugforce = false;
2013-08-20 16:32:57 +02:00
boxes = false;
encrypt_metadata = true;
retain_numbering = false;
remove_duplicate_fonts = false;
remove_duplicate_streams = false;
encoding = Cpdf.Stripped;
scale = 1.;
copyfontpage = 1;
copyfontname = None;
fast = false;
dashrange = "all";
outline = false;
linewidth = 1.0;
path_to_ghostscript = "";
frombox = None;
tobox = None;
mediabox_if_missing = false;
topage = None;
scale_stamp_to_fit = false;
keep_this_id = None;
makenewid = false;
2014-09-18 15:27:07 +02:00
labelstyle = Pdfpagelabels.DecimalArabic;
2013-10-24 16:21:54 +02:00
labelprefix = None;
2014-09-18 16:40:22 +02:00
labelstartval = 1;
labelsprogress = false;
squeeze = false;
squeeze_recompress = true;
squeeze_pagedata = true;
2014-10-02 20:57:06 +02:00
original_filename = "";
was_encrypted = false;
2014-10-15 14:48:39 +02:00
cpdflin = None;
recrypt = false;
2015-01-22 20:16:56 +01:00
was_decrypted_with_owner = false;
producer = None;
2015-04-13 15:17:48 +02:00
creator = None;
2016-11-09 19:15:23 +01:00
embedfonts = true;
2017-12-18 20:44:02 +01:00
extract_text_font_size = None;
padwith = None;
alsosetxml = false;
justsetxml = false;
2019-07-03 15:40:32 +02:00
gs_malformed = false;
2019-07-22 15:00:37 +02:00
gs_quiet = false;
2019-07-03 15:40:32 +02:00
merge_add_bookmarks = false;
merge_add_bookmarks_use_titles = false;
createpdf_pages = 1;
2019-07-09 17:31:45 +02:00
createpdf_pagesize = Pdfpaper.a4;
2020-01-30 11:20:33 +01:00
removeonly = None;
2020-02-01 11:18:15 +01:00
jsonparsecontentstreams = false;
2020-02-27 14:32:45 +01:00
jsonnostreamdata = false;
ocgrenamefrom = "";
ocgrenameto = ""}
2013-08-20 16:32:57 +02:00
let reset_arguments () =
args.op <- None;
args.preserve_objstm <- true;
args.create_objstm <- false;
args.out <- NoOutputSpecified;
args.inputs <- [];
args.chunksize <- 1;
args.linearize <- false;
args.keeplinearize <- false;
args.rectangle <- "0 0 0 0";
args.coord <- "0 0";
2013-08-20 16:32:57 +02:00
args.duration <- None;
args.transition <- None;
args.horizontal <- true;
args.inward <- true;
args.direction <- 0;
args.effect_duration <- 1.;
args.font <- StandardFont Pdftext.TimesRoman;
args.fontname <- "Times-Roman";
args.fontsize <- 12.;
args.color <- 0., 0., 0.;
args.opacity <- 1.;
args.position <- Cpdf.TopLeft 100.;
args.underneath <- false;
args.linespacing <- 1.;
args.midline <- false;
2015-01-20 16:50:36 +01:00
args.topline <- false;
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.LeftJustify;
args.bates <- 0;
2015-07-17 17:34:47 +02:00
args.batespad <- None;
2013-08-20 16:32:57 +02:00
args.prerotate <- false;
args.orientation <- Cpdf.Horizontal;
args.relative_to_cropbox <- false;
args.keepversion <- false;
args.bycolumns <- false;
args.pagerotation <- 0;
args.crypt_method <- "";
args.owner <- "";
args.user <- "";
args.no_edit <- false;
args.no_print <- false;
args.no_copy <- false;
args.no_annot <- false;
args.no_forms <- false;
args.no_extract <- false;
args.no_assemble <- false;
args.no_hq_print <- false;
args.debug <- false;
2014-10-24 18:24:29 +02:00
args.debugcrypt <- false;
2018-03-20 11:19:50 +01:00
args.debugforce <- false;
2013-08-20 16:32:57 +02:00
args.boxes <- false;
args.encrypt_metadata <- true;
args.retain_numbering <- false;
args.remove_duplicate_fonts <- false;
args.remove_duplicate_streams <- false;
args.encoding <- Cpdf.Stripped;
args.scale <- 1.;
args.copyfontpage <- 1;
args.copyfontname <- None;
args.fast <- false;
args.dashrange <- "all";
args.outline <- false;
args.linewidth <- 1.0;
args.frombox <- None;
args.tobox <- None;
args.mediabox_if_missing <- false;
args.topage <- None;
args.scale_stamp_to_fit <- false;
args.keep_this_id <- None;
args.makenewid <- false;
2014-09-18 15:27:07 +02:00
args.labelstyle <- Pdfpagelabels.DecimalArabic;
2013-10-24 16:21:54 +02:00
args.labelprefix <- None;
2014-09-18 16:40:22 +02:00
args.labelstartval <- 1;
args.labelsprogress <- false;
2016-11-13 19:01:01 +01:00
args.embedfonts <- true;
2017-12-18 20:44:02 +01:00
args.extract_text_font_size <- None;
args.padwith <- None;
args.alsosetxml <- false;
2019-07-03 15:40:32 +02:00
args.justsetxml <- false;
args.merge_add_bookmarks <- false;
args.merge_add_bookmarks_use_titles <- false;
args.createpdf_pages <- 1;
2019-07-09 17:31:45 +02:00
args.createpdf_pagesize <- Pdfpaper.a4;
2020-01-30 11:20:33 +01:00
args.removeonly <- None;
2020-02-01 11:18:15 +01:00
args.jsonparsecontentstreams <- false;
2020-02-27 14:32:45 +01:00
args.jsonnostreamdata <- false;
args.ocgrenamefrom <- "";
args.ocgrenameto <- ""
(* Do not reset original_filename or cpdflin or was_encrypted or
* was_decrypted_with_owner or recrypt or producer or creator or
2019-07-22 15:00:37 +02:00
* path_to_ghostscript or gs_malformed or gs_quiet, since we want these to work across
* ANDs. Or squeeze options: a little odd, but we want it to happen on eventual output. *)
2015-07-26 13:31:43 +02:00
let get_pagespec () =
match args.inputs with
| (_, ps, _, _, _, _)::_ -> ps
| _ -> error "No range specified for input, or specified too late."
let string_of_permission = function
| Pdfcrypt.NoEdit -> "No edit"
| Pdfcrypt.NoPrint -> "No print"
| Pdfcrypt.NoCopy -> "No copy"
| Pdfcrypt.NoAnnot -> "No annotate"
| Pdfcrypt.NoForms -> "No edit forms"
| Pdfcrypt.NoExtract -> "No extract"
| Pdfcrypt.NoAssemble -> "No assemble"
| Pdfcrypt.NoHqPrint -> "No high-quality print"
let getpermissions pdf =
fold_left
(fun x y -> if x = "" then x ^ y else x ^ ", " ^ y)
""
(map string_of_permission (Pdfread.permissions pdf))
2013-08-20 16:32:57 +02:00
let banlist_of_args () =
let l = ref [] in
if args.no_edit then l =| Pdfcrypt.NoEdit;
if args.no_print then l =| Pdfcrypt.NoPrint;
if args.no_copy then l =| Pdfcrypt.NoCopy;
if args.no_annot then l =| Pdfcrypt.NoAnnot;
if args.no_forms then l =| Pdfcrypt.NoForms;
if args.no_extract then l =| Pdfcrypt.NoExtract;
if args.no_assemble then l =| Pdfcrypt.NoAssemble;
if args.no_hq_print then l =| Pdfcrypt.NoHqPrint;
!l
(* If a file is encrypted, decrypt it using the owner password or, if not
present, the user password. If the user password is used, the operation to be
performed is checked to see if it's allowable under the permissions regime. *)
(* The bans. Each function has a list of bans. If any of these is present in the
bans list in the input file, the operation cannot proceed. Other operations
cannot proceed at all without owner password. *)
let banned banlist = function
| Fonts | Info | Metadata | PageInfo | Revisions | CountPages
| ListAttachedFiles | ListAnnotationsMore | ListAnnotations
| ListBookmarks | ImageResolution _ | MissingFonts
| PrintPageLabels | Clean | Compress | Decompress
2016-11-03 18:11:08 +01:00
| RemoveUnusedResources | ChangeId | CopyId _ | ListSpotColours | Version
2019-07-15 12:52:14 +02:00
| DumpAttachedFiles | RemoveMetadata | EmbedMissingFonts | BookmarksOpenToLevel _ | CreatePDF
2019-08-01 15:34:45 +02:00
| SetPageMode _ | HideToolbar _ | HideMenubar _ | HideWindowUI _
| FitWindow _ | CenterWindow _ | DisplayDocTitle _
| RemoveId | OpenAtPageFit _ | OpenAtPage _ | SetPageLayout _
2019-08-18 12:25:55 +02:00
| ShowBoxes | TrimMarks | CreateMetadata | SetMetadataDate _ | SetVersion _
| SetAuthor _|SetTitle _|SetSubject _|SetKeywords _|SetCreate _
| SetModify _|SetCreator _|SetProducer _|RemoveDictEntry _ | SetMetadata _
| ExtractText | ExtractImages | ExtractFontFile
2020-02-26 17:24:27 +01:00
| AddPageLabels | RemovePageLabels | OutputJSON | OCGCoalesce
2020-03-02 13:37:39 +01:00
| OCGRename | OCGList | OCGOrderAll
2019-08-18 12:25:55 +02:00
-> false (* Always allowed *)
(* Combine pages is not allowed because we would not know where to get the
-recrypt from -- the first or second file? *)
| Decrypt | Encrypt | CombinePages _ -> true (* Never allowed *)
2019-07-01 16:35:17 +02:00
| AddBookmarks _ | PadBefore | PadAfter | PadEvery _ | PadMultiple _ | PadMultipleBefore _
| Merge | Split | SplitOnBookmarks _ | RotateContents _ | Rotate _
2019-08-18 12:25:55 +02:00
| Rotateby _ | Upright | VFlip | HFlip ->
2019-08-01 15:34:45 +02:00
mem Pdfcrypt.NoAssemble banlist
2016-11-03 18:11:08 +01:00
| CSP1|CSP3|TwoUp|TwoUpStack|RemoveBookmarks|AddRectangle|RemoveText|
Draft|Shift|Scale|ScaleToFit|RemoveAttachedFiles|
RemoveAnnotations|RemoveFonts|Crop|RemoveCrop|Trim|RemoveTrim|Bleed|RemoveBleed|Art|RemoveArt|
2017-05-19 20:10:49 +02:00
CopyCropBoxToMediaBox|CopyBox|MediaBox|HardBox _|SetTrapped|SetUntrapped|Presentation|
BlackText|BlackLines|BlackFills|CopyFont _|CSP2 _|StampOn _|StampUnder _|StampAsXObject _|
AddText _|ScaleContents _|AttachFile _|CopyAnnotations _| ThinLines _ | RemoveClipping | RemoveAllText
| Prepend _ | Postpend _ ->
2016-11-03 18:11:08 +01:00
mem Pdfcrypt.NoEdit banlist
2013-08-20 16:32:57 +02:00
let operation_allowed pdf banlist op =
2018-03-20 11:19:50 +01:00
args.debugforce ||
2014-10-15 18:51:15 +02:00
match op with
| None ->
2014-10-28 19:40:56 +01:00
if args.debugcrypt then Printf.printf "operation is None, so allowed!\n";
true (* Merge *) (* changed to allow it *)
| Some op ->
if args.debugcrypt then Printf.printf "operation_allowed: op = %s\n" (string_of_op op);
if args.debugcrypt then Printf.printf "Permissions: %s\n" (getpermissions pdf);
not (banned banlist op)
2013-08-20 16:32:57 +02:00
let rec decrypt_if_necessary (_, _, user_pw, owner_pw, was_dec_with_owner, _) op pdf =
2014-10-24 18:24:29 +02:00
if args.debugcrypt then
begin match op with
None -> flprint "decrypt_if_necessary: op = None\n"
| Some x -> Printf.printf "decrypt_if_necessary: op = %s\n" (string_of_op x)
end;
2013-08-20 16:32:57 +02:00
if not (Pdfcrypt.is_encrypted pdf) then pdf else
2014-11-17 15:48:56 +01:00
match op with Some (CombinePages _) ->
(* This is a hack because we don't have support for recryption on combine
* pages. This is pervented by permissions above, but in the case that the
* owner password is blank (e.g christmas_tree_lights.pdf), we would end
* up here. *)
soft_error "Combine pages: both files must be unencrypted for this operation."
| _ ->
match Pdfcrypt.decrypt_pdf_owner owner_pw pdf with
| Some pdf ->
args.was_decrypted_with_owner <- true;
was_dec_with_owner := true;
if args.debugcrypt then Printf.printf "Managed to decrypt with owner password\n";
pdf
2013-08-20 16:32:57 +02:00
| _ ->
2014-11-17 15:48:56 +01:00
if args.debugcrypt then Printf.printf "Couldn't decrypt with owner password %s\n" owner_pw;
2019-10-26 15:10:03 +02:00
match
if args.debugcrypt then Printf.printf "call decrypt_pdf user\n";
let r = Pdfcrypt.decrypt_pdf user_pw pdf in
if args.debugcrypt then Printf.printf "returned from decrypt_pdf\n";
r
with
2014-11-17 15:48:56 +01:00
| Some pdf, permissions ->
if args.debugcrypt then Printf.printf "Managed to decrypt with user password\n";
if operation_allowed pdf permissions op
then pdf
else soft_error "User password cannot give permission for this operation"
| _ ->
2019-10-26 15:10:03 +02:00
if args.debugcrypt then Printf.printf "Failed to decrypt with user password: raising soft_error";
2014-11-17 15:48:56 +01:00
soft_error "Failed to decrypt file: wrong password?"
2013-08-20 16:32:57 +02:00
let nobble pdf =
if not demo then pdf else
Cpdf.process_pages (Cpdf.nobble_page pdf) pdf (ilist 1 (Pdfpage.endpage pdf))
(* Output Page Count *)
let output_page_count pdf =
Printf.printf "%i\n" (Pdfpage.endpage pdf)
2019-07-08 13:44:27 +02:00
let detect_duplicate_op op =
2019-09-26 14:30:22 +02:00
match args.op with
None | Some Shift -> ()
| _ ->
2019-07-08 13:44:27 +02:00
Printf.eprintf "Operation %s already specified, so cannot specify operation %s.\nUse AND from Chapter 1 of the manual to chain commands together.\n"
(string_of_op (unopt args.op)) (string_of_op op);
exit 1
2013-08-20 16:32:57 +02:00
let setop op () =
2019-07-08 13:44:27 +02:00
detect_duplicate_op op;
2013-08-20 16:32:57 +02:00
args.op <- Some op
let setout name =
args.out <- File name
let setchunk c =
if c > 0
then args.chunksize <- c
else error "invalid chunk size"
let setlinearize () =
args.linearize <- true
let setkeeplinearize () =
args.keeplinearize <- true
2013-08-20 16:32:57 +02:00
let fixdashes s =
let bufferdashes chars =
let buf = ref [] in
iter
(function '-' -> buf =@ [' '; '-'; ' '] | x -> buf =| x)
chars;
rev !buf
in
let chars = explode s in
implode (bufferdashes chars)
let encrypt_to_collect = ref 0
let setmethod s =
2019-07-08 13:44:27 +02:00
detect_duplicate_op Encrypt;
2013-08-20 16:32:57 +02:00
if args.op = None then args.op <- Some Encrypt; (* Could be additional to -split *)
match s with
| "40bit" | "128bit" | "AES" | "AES256" | "AES256ISO" -> args.crypt_method <- s
| _ -> error "Unsupported encryption method"
let setowner s =
2014-10-14 20:36:57 +02:00
args.owner <- s
2013-08-20 16:32:57 +02:00
let setuser s =
2014-10-14 20:36:57 +02:00
args.user <- s
2013-08-20 16:32:57 +02:00
let anon_fun s =
try
match !encrypt_to_collect with
| 3 -> setmethod s; decr encrypt_to_collect
| 2 -> setowner s; decr encrypt_to_collect
| 1 -> setuser s; decr encrypt_to_collect
| 0 ->
let before, after = cleavewhile (neq '=') (explode s) in
begin match implode before with
| "user" ->
begin match args.inputs with
| [] -> ()
| (a, b, _, e, f, g)::more ->
args.inputs <- (a, b, implode (tl after), e, f, g)::more
2013-08-20 16:32:57 +02:00
end
| "owner" ->
begin match args.inputs with
| [] -> ()
| (a, b, d, _, f, g)::more ->
args.inputs <- (a, b, d, implode (tl after), f, g)::more
2013-08-20 16:32:57 +02:00
end
| _ -> raise Not_found
end
| _ -> assert false
with
Not_found ->
try
ignore (String.index s '.');
args.inputs <- (InFile s, "all", "", "", ref false, None)::args.inputs;
args.original_filename <- s
2013-08-20 16:32:57 +02:00
with
Not_found ->
match args.inputs with
| [] -> ()
| (a, _, d, e, f, g)::t ->
args.inputs <- (a, fixdashes s, d, e, f, g)::t
2013-08-20 16:32:57 +02:00
2016-07-21 18:02:11 +02:00
(*let firstpage pdf =
List.hd (Pdfpage.pages_of_pagetree pdf)*)
2013-08-20 16:32:57 +02:00
(* Setting operations *)
let setcrop s =
setop Crop ();
args.rectangle <- s
2013-08-20 16:32:57 +02:00
let settrim s =
setop Trim ();
args.rectangle <- s
let setbleed s =
setop Bleed ();
args.rectangle <- s
let setart s =
setop Art ();
args.rectangle <- s
2013-08-20 16:32:57 +02:00
let setmediabox s =
setop MediaBox ();
args.rectangle <- s
2013-08-20 16:32:57 +02:00
let setrectangle s =
setop AddRectangle ();
args.coord <- s
2013-08-20 16:32:57 +02:00
let setrotate i =
if i = 0 || i = 90 || i = 180 || i = 270
then setop (Rotate i) ()
else error "bad rotation"
let setrotateby i =
if i = 0 || i = 90 || i = 180 || i = 270
then setop (Rotateby i) ()
else error "bad rotation"
let setrotatecontents f =
setop (RotateContents f) ()
let setauthor s = setop (SetAuthor s) ()
let settitle s = setop (SetTitle s) ()
let setsubject s = setop (SetSubject s) ()
let setkeywords s = setop (SetKeywords s) ()
let setcreate s = setop (SetCreate s) ()
let setmodify s = setop (SetModify s) ()
let setcreator s = setop (SetCreator s) ()
let setproducer s = setop (SetProducer s) ()
let setmetadata s = setop (SetMetadata s) ()
let setversion i = setop (SetVersion i) ()
let setpagelayout s = setop (SetPageLayout s) ()
let setpagemode s = setop (SetPageMode s) ()
let hidetoolbar b =
try setop (HideToolbar (bool_of_string b)) () with
_ -> failwith "HideToolBar: must use true or false"
let hidemenubar b =
try setop (HideMenubar (bool_of_string b)) () with
_ -> failwith "HideMenuBar: must use true or false"
let hidewindowui b =
try setop (HideWindowUI (bool_of_string b)) () with
_ -> failwith "HideWindowUI: must use true or false"
let fitwindow b =
try setop (FitWindow (bool_of_string b)) () with
_ -> failwith "FitWindow: must use true or false"
let centerwindow b =
try setop (CenterWindow (bool_of_string b)) () with
_ -> failwith "CenterWindow: must use true or false"
let displaydoctitle b =
try setop (DisplayDocTitle (bool_of_string b)) () with
_ -> failwith "DisplayDocTitle: must use true or false"
2016-07-21 18:02:11 +02:00
2013-08-20 16:32:57 +02:00
let setsplitbookmarks i = setop (SplitOnBookmarks i) ()
let setstdout () = args.out <- Stdout
let setstdin () = args.inputs <- [StdIn, "all", "", "", ref false, None]
2013-08-20 16:32:57 +02:00
let settrans s = args.transition <- Some s
let setduration f = args.duration <- Some f
let setvertical () = args.horizontal <- false
let setoutward () = args.inward <- false
let setdirection i =
args.direction <-
match i with
| 0 | 90 | 180 | 270 | 315 -> i
| _ -> error "Bad direction"
let seteffectduration f = args.effect_duration <- f
let setcopyid s = setop (CopyId s) ()
let setthinlines s = setop (ThinLines (Cpdfcoord.parse_single_number empty s)) ()
2013-08-20 16:32:57 +02:00
let setcopyannotations s = setop (CopyAnnotations s) ()
let setshift s =
setop Shift ();
args.coord <- s
2013-08-20 16:32:57 +02:00
let setscale s =
setop Scale ();
args.coord <- s
2013-08-20 16:32:57 +02:00
let setscaletofit s =
setop ScaleToFit ();
args.coord <- s
2013-08-20 16:32:57 +02:00
let setattachfile s =
match args.op with
| Some (AttachFile t) ->
args.op <- Some (AttachFile (s::t))
2019-07-08 13:44:27 +02:00
| None ->
2013-08-20 16:32:57 +02:00
setop (AttachFile [s]) ()
2019-07-08 13:44:27 +02:00
| Some _ -> detect_duplicate_op (AttachFile [s])
2013-08-20 16:32:57 +02:00
let setfont f =
args.font <-
begin match Pdftext.standard_font_of_name ("/" ^ f) with
| Some x -> StandardFont x
| None -> OtherFont f
end;
args.fontname <- f
2016-11-13 15:02:09 +01:00
let setextracttextfontsize f =
args.extract_text_font_size <- Some f
2013-08-20 16:32:57 +02:00
let setfontsize f =
2016-11-13 15:02:09 +01:00
if f > 0. then args.fontsize <- f else error "Negative font size specified"
2013-08-20 16:32:57 +02:00
let setaddtext s =
setop (AddText s) ()
2016-11-10 16:03:34 +01:00
let parse_color s =
match String.lowercase s with
| "white" -> 1., 1., 1.
| "black" -> 0., 0., 0.
| "red" -> 1., 0., 0.
| "green" -> 0., 1., 0.
| "blue" -> 0., 0., 1.
| _ ->
let getnum = function
| Pdfgenlex.LexInt i -> float i
| Pdfgenlex.LexReal f -> f
| _ -> error "Bad color"
in
match Pdfgenlex.lex_string s with
| [a;b;c] -> getnum a, getnum b, getnum c
| _ -> error "Bad color"
2013-08-20 16:32:57 +02:00
let setcolor s =
2016-11-10 16:03:34 +01:00
args.color <- parse_color s
2013-08-20 16:32:57 +02:00
let setopacity o =
args.opacity <- o
let setaddbookmarks s =
setop (AddBookmarks s) ()
let setstampon f =
setop (StampOn f) ();
(* Due to an earlier bad decision (default position), we have this nasty hack *)
if args.position = Cpdf.TopLeft 100. then args.position <- Cpdf.BottomLeft 0.
2013-08-20 16:32:57 +02:00
let setstampunder f =
setop (StampUnder f) ();
if args.position = Cpdf.TopLeft 100. then args.position <- Cpdf.BottomLeft 0.
2013-08-20 16:32:57 +02:00
let setstampasxobject f =
setop (StampAsXObject f) ()
2013-08-20 16:32:57 +02:00
let setcombinepages f =
setop (CombinePages f) ()
let setposcenter s =
let x, y = Cpdfcoord.parse_coordinate empty s in
2013-08-20 16:32:57 +02:00
args.position <- Cpdf.PosCentre (x, y)
let setposleft s =
let x, y = Cpdfcoord.parse_coordinate empty s in
2013-08-20 16:32:57 +02:00
args.position <- Cpdf.PosLeft (x, y)
let setposright s =
let x, y = Cpdfcoord.parse_coordinate empty s in
2013-08-20 16:32:57 +02:00
args.position <- Cpdf.PosRight (x, y)
let settop n =
args.position <- Cpdf.Top (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.CentreJustify
let settopleft n =
args.position <- Cpdf.TopLeft (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.LeftJustify
let settopright n =
args.position <- Cpdf.TopRight (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.RightJustify
let setleft n =
args.position <- Cpdf.Left (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.LeftJustify
let setbottomleft n =
args.position <- Cpdf.BottomLeft (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.LeftJustify
let setbottom n =
args.position <- Cpdf.Bottom (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.CentreJustify
let setbottomright n =
args.position <- Cpdf.BottomRight (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.RightJustify
let setright n =
args.position <- Cpdf.Right (Cpdfcoord.parse_single_number empty n);
2013-08-20 16:32:57 +02:00
args.justification <- Cpdf.RightJustify
let setdiagonal n =
args.position <- Cpdf.Diagonal;
args.justification <- Cpdf.CentreJustify
let setreversediagonal n =
args.position <- Cpdf.ReverseDiagonal;
args.justification <- Cpdf.CentreJustify
let setcenter n =
args.position <- Cpdf.Centre;
args.justification <- Cpdf.CentreJustify
2015-07-17 17:34:47 +02:00
let setbatespad n =
args.batespad <- Some n
2013-08-20 16:32:57 +02:00
let setbates n =
args.bates <- n
2015-07-26 13:31:43 +02:00
(* Calculate -bates automatically so that n is applied to the first page in the range *)
let setbatesrange n =
let first_page =
let range = Cpdf.parse_pagespec_without_pdf (get_pagespec ()) in
fold_left min max_int range
in
args.bates <- n + 1 - first_page
2013-08-20 16:32:57 +02:00
let setkeepversion () =
args.keepversion <- true
let setbycolumns () =
args.bycolumns <- true
let setpagerotation r =
match r with
| 90 | 270 -> args.pagerotation <- r
| _ -> error "Bad Page rotation. Try 90 or 270."
let set_no_edit () =
args.no_edit <- true
let set_no_print () =
args.no_print <- true
let set_no_copy () =
args.no_copy <- true
let set_no_annot () =
args.no_annot <- true
let set_no_forms () =
args.no_forms <- true
let set_no_extract () =
args.no_extract <- true
let set_no_assemble () =
args.no_assemble <- true
let set_no_hq_print () =
args.no_hq_print <- true
let set_input s =
args.original_filename <- s;
args.inputs <- (InFile s, "all", "", "", ref false, None)::args.inputs
2013-08-20 16:32:57 +02:00
let set_input_dir s =
let names = sort compare (leafnames_of_dir s) in
args.inputs <-
(rev
(map
(fun n -> (InFile (s ^ Filename.dir_sep ^ n), "all", "", "", ref false, None)) names))
@ args.inputs
2013-08-20 16:32:57 +02:00
let setdebug () =
set Pdfread.read_debug;
set Pdfwrite.write_debug;
set Pdfcrypt.crypt_debug;
2017-07-04 15:37:28 +02:00
set Pdfops.debug;
2014-11-24 15:31:38 +01:00
set Cpdf.debug;
2013-08-20 16:32:57 +02:00
args.debug <- true
2014-10-24 18:24:29 +02:00
let setdebugcrypt () =
args.debugcrypt <- true
2018-03-20 11:19:50 +01:00
let setdebugforce () =
args.debugforce <- true
2013-08-20 16:32:57 +02:00
let setboxes () =
args.boxes <- true
let set_no_encrypt_metadata () =
args.encrypt_metadata <- false
let set_retain_numbering () =
args.retain_numbering <- true
let set_remove_duplicate_fonts () =
args.remove_duplicate_fonts <- true
let setencoding enc () =
args.encoding <- enc
let setlinespacing f =
args.linespacing <- f
let setmidline () =
args.midline <- true
2015-01-20 16:50:36 +01:00
let settopline () =
args.topline <- true
2013-08-20 16:32:57 +02:00
let setscaletofitscale f =
args.scale <- f
let setscalecontents f =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (ScaleContents f);
2013-08-20 16:32:57 +02:00
args.op <- Some (ScaleContents f);
args.position <- Cpdf.Diagonal (* Will be center *)
2014-09-11 15:05:13 +02:00
let setsqueeze () =
2014-09-20 21:16:08 +02:00
args.squeeze <- true;
args.create_objstm <- true
2014-09-11 15:05:13 +02:00
2015-01-22 20:16:56 +01:00
let setcreator s =
args.creator <- Some s
let setproducer s =
args.producer <- Some s
let setprepend s =
args.op <- Some (Prepend s)
let setpostpend s =
args.op <- Some (Postpend s)
2013-08-20 16:32:57 +02:00
(* Parsing the control file *)
let rec getuntilendquote prev = function
| [] -> implode (rev prev), []
| '"'::t -> implode (rev prev), t
| '\\'::'"'::t -> getuntilendquote ('"'::prev) t
| h::t -> getuntilendquote (h::prev) t
let rec getarg prev = function
| [] -> implode (rev prev), []
| h::t ->
if Pdf.is_whitespace h
then implode (rev prev), t
else getarg (h::prev) t
let rec parse_chars args = function
| [] -> rev args
| h::more when Pdf.is_whitespace h ->
parse_chars args more
| '"'::more ->
let this, rest = getuntilendquote [] more in
parse_chars (this::args) rest
| h::t ->
let this, rest = getarg [] (h::t) in
parse_chars (this::args) rest
let control_args = ref []
let parse_control_file name =
(parse_chars []
(charlist_of_bytes (Pdfio.bytes_of_input_channel (open_in_bin name))))
let setencryptcollect () =
encrypt_to_collect := 3
let setcopyfont s =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (CopyFont s);
2013-08-20 16:32:57 +02:00
args.op <- Some (CopyFont s)
let setfontpage i =
args.copyfontpage <- i
let setcopyfontname s =
args.copyfontname <- Some s
let setpadevery i =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (PadEvery i);
if i > 0 then
args.op <- Some (PadEvery i)
else
error "PadEvery: must be > 0"
2013-08-20 16:32:57 +02:00
2017-12-18 20:44:02 +01:00
let setpadwith filename =
args.padwith <- Some filename
2013-08-20 16:32:57 +02:00
let setpadmultiple i =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (PadMultiple i);
2013-08-20 16:32:57 +02:00
args.op <- Some (PadMultiple i)
2019-07-01 16:35:17 +02:00
let setpadmultiplebefore i =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (PadMultipleBefore i);
2019-07-01 16:35:17 +02:00
args.op <- Some (PadMultipleBefore i)
2013-08-20 16:32:57 +02:00
let setfast () =
args.fast <- true
let setcsp2 f =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (CSP2 f);
2013-08-20 16:32:57 +02:00
args.op <- Some (CSP2 f)
(* Explicitly add a range. Parse it and replace the top input file with the range. *)
let setrange spec =
2014-10-03 15:18:28 +02:00
args.dashrange <- spec;
match args.inputs with
(x, _, c, d, e, f)::more ->
args.inputs <- (x, spec, c, d, e, f) :: more
2014-10-03 15:18:28 +02:00
| x -> ()
2013-08-20 16:32:57 +02:00
let setrevision n =
match args.inputs with
(a, b, c, d, e, _)::more ->
args.inputs <- (a, b, c, d, e, Some n) :: more
2014-12-22 14:20:20 +01:00
| [] ->
Printf.eprintf "Warning. -revision ignored. Put it after the filename.\n"
2013-08-20 16:32:57 +02:00
let setoutline () =
args.outline <- true
let setlinewidth l =
args.linewidth <- l
let setunderneath () =
args.underneath <- true
let setimageresolution f =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (ImageResolution f);
2013-08-20 16:32:57 +02:00
args.op <- Some (ImageResolution f)
let setgspath p =
args.path_to_ghostscript <- p
2019-07-14 14:50:48 +02:00
let settextvertical () =
2013-08-20 16:32:57 +02:00
args.orientation <- Cpdf.Vertical
2019-07-14 14:50:48 +02:00
let settextverticaldown () =
2013-08-20 16:32:57 +02:00
args.orientation <- Cpdf.VerticalDown
let setfrombox s =
2019-07-08 13:44:27 +02:00
detect_duplicate_op CopyBox;
2013-08-20 16:32:57 +02:00
args.op <- Some CopyBox;
args.frombox <- Some s
let settobox s =
args.tobox <- Some s
let setmediaboxifmissing () =
args.mediabox_if_missing <- true
let setrelativetocropbox () =
args.relative_to_cropbox <- true
let setprerotate () =
args.prerotate <- true
let setflatkids () =
Pdfpage.flat_pagetrees := true
let settopage s =
args.topage <- Some s
let setscalestamptofit () =
args.scale_stamp_to_fit <- true
let setkeepthisid () =
match args.inputs with
| (InFile s, _, _, _, _, _)::_ -> args.keep_this_id <- Some s
2013-08-20 16:32:57 +02:00
| _ -> ()
let setmakenewid () =
args.makenewid <- true
let setjustifyleft () =
args.justification <- Cpdf.LeftJustify
let setjustifyright () =
args.justification <- Cpdf.RightJustify
let setjustifycenter () =
args.justification <- Cpdf.CentreJustify
let setremoveduplicatestreams () =
args.remove_duplicate_streams <- true
let setnopreserveobjstm () =
args.preserve_objstm <- false
let setcreateobjstm () =
args.create_objstm <- true
let setstdinuser u =
match args.inputs with
| (StdIn, x, _, o, f, g)::t -> args.inputs <- (StdIn, x, u, o, f, g)::t
2013-08-20 16:32:57 +02:00
| _ -> error "-stdin-user: must follow -stdin"
let setstdinowner o =
match args.inputs with
| (StdIn, x, u, _, f, g)::t -> args.inputs <- (StdIn, x, u, o, f, g)::t
2014-11-17 19:48:17 +01:00
| _ -> error "-stdin-owner: must follow -stdin"
2013-08-20 16:32:57 +02:00
2013-10-02 16:29:53 +02:00
let setopenatpage n =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (OpenAtPage n);
2013-10-02 16:29:53 +02:00
args.op <- Some (OpenAtPage n)
let setopenatpagefit n =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (OpenAtPageFit n);
2013-10-02 16:29:53 +02:00
args.op <- Some (OpenAtPageFit n)
2013-10-24 16:21:54 +02:00
let setlabelstyle s =
let style =
match s with
| "DecimalArabic" -> Pdfpagelabels.DecimalArabic
| "UppercaseRoman" -> Pdfpagelabels.UppercaseRoman
| "LowercaseRoman" -> Pdfpagelabels.LowercaseRoman
| "UppercaseLetters" -> Pdfpagelabels.UppercaseLetters
| "LowercaseLetters" -> Pdfpagelabels.LowercaseLetters
2014-09-18 15:27:07 +02:00
| "NoLabelPrefixOnly" -> Pdfpagelabels.NoLabelPrefixOnly
2013-10-24 16:21:54 +02:00
| _ -> error "Unknown label style"
in
2014-09-18 15:27:07 +02:00
args.labelstyle <- style
2013-10-24 16:21:54 +02:00
let setlabelprefix s =
args.labelprefix <- Some s
2014-09-18 16:40:22 +02:00
let setlabelstartval i =
args.labelstartval <- i
let setlabelsprogress () =
args.labelsprogress <- true
2014-10-02 20:57:06 +02:00
let setcpdflin s =
args.cpdflin <- Some s
2014-10-15 18:51:15 +02:00
let setrecrypt () =
args.recrypt <- true
2014-10-15 14:48:39 +02:00
2015-01-07 19:29:11 +01:00
let setremovedictentry s =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (RemoveDictEntry s);
2015-01-07 19:29:11 +01:00
args.op <- Some (RemoveDictEntry s)
2015-01-07 21:29:39 +01:00
let logto = ref None
let setsqueezelogto s =
logto := Some s
let setstayonerror () =
set stay_on_error
2015-04-13 15:17:48 +02:00
let setnoembedfont () =
args.embedfonts <- false
2017-05-19 20:10:49 +02:00
let sethardbox box =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (HardBox box);
2017-05-19 20:10:49 +02:00
args.op <- Some (HardBox box)
let setalsosetxml () =
args.alsosetxml <- true
let setjustsetxml () =
args.justsetxml <- true
2019-06-29 16:03:22 +02:00
let setsetmetadatadate d =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (SetMetadataDate d);
2019-06-29 16:03:22 +02:00
args.op <- Some (SetMetadataDate d)
let setgsmalformed () =
args.gs_malformed <- true
2019-07-03 15:40:32 +02:00
let setmergeaddbookmarks () =
args.merge_add_bookmarks <- true
let setmergeaddbookmarksusetitles () =
args.merge_add_bookmarks_use_titles <- true
2019-07-06 18:55:26 +02:00
let setbookmarksopentolevel l =
2019-07-08 13:44:27 +02:00
detect_duplicate_op (BookmarksOpenToLevel l);
2019-07-06 18:55:26 +02:00
args.op <- Some (BookmarksOpenToLevel l)
let setcreatepdfpages i =
args.createpdf_pages <- i
let setcreatepdfpapersize s =
args.createpdf_pagesize <-
let w, h = Cpdfcoord.parse_coordinate (Pdf.empty ()) s in
Pdfpaper.make Pdfunits.PdfPoint w h
2019-07-09 17:31:45 +02:00
let setdraftremoveonly s =
args.removeonly <- Some s
2019-07-22 15:00:37 +02:00
let setgsquiet () =
args.gs_quiet <- true
2020-01-30 11:20:33 +01:00
let setjsonparsecontentstreams () =
args.jsonparsecontentstreams <- true
2020-02-01 11:18:15 +01:00
let setjsonnostreamdata () =
args.jsonnostreamdata <- true
2020-02-27 14:32:45 +01:00
let setocgrenamefrom s =
args.ocgrenamefrom <- s
let setocgrenameto s =
args.ocgrenameto <- s
let setsqueezepagedata () =
args.squeeze_pagedata <- false
let setsqueezerecompress () =
args.squeeze_recompress <- false
let whingemalformed () =
prerr_string "Command line must be of exactly the form\ncpdf <infile> -gs <path> -gs-malformed-force -o <outfile>\n";
exit 1
2019-10-21 13:06:04 +02:00
let nothing () = ()
2013-08-20 16:32:57 +02:00
(* Parse a control file, make an argv, and then make Arg parse it. *)
let rec make_control_argv_and_parse filename =
control_args := !control_args @ parse_control_file filename
and specs =
[("-version",
Arg.Unit (setop Version),
" Print the cpdf version number");
("-o",
Arg.String setout,
" Set the output file, if appropriate");
("-i",
Arg.String set_input,
" Add an input file");
("-idir",
Arg.String set_input_dir,
" Add a directory of files");
("-stdin",
Arg.Unit setstdin,
" Read input from standard input");
("-stdin-owner",
Arg.String setstdinowner,
" Owner password for -stdin");
("-stdin-user",
Arg.String setstdinuser,
" User password for -stdin");
("-stdout",
Arg.Unit setstdout,
" Send result to standard output");
("-error-on-malformed",
Arg.Set Pdfread.error_on_malformed,
" Do not try to read malformed files");
2013-08-20 16:32:57 +02:00
("-range",
Arg.String setrange,
" Explicitly add a range");
("-revision",
Arg.Int setrevision,
2016-11-04 17:46:08 +01:00
"");
2013-08-20 16:32:57 +02:00
("-change-id",
Arg.Unit (setop ChangeId),
" Change the file's /ID tag");
("-no-preserve-objstm",
Arg.Unit setnopreserveobjstm,
" Don't preserve object streams");
("-create-objstm",
Arg.Unit setcreateobjstm,
" Create object streams anew");
("-keep-version",
Arg.Unit setkeepversion,
" Don't change the version number");
("-l",
Arg.Unit setlinearize,
2014-12-01 19:12:02 +01:00
" Linearize output file");
("-keep-l",
Arg.Unit setkeeplinearize,
2014-12-01 19:12:02 +01:00
" Linearize if the input file was linearized");
2014-10-02 20:57:06 +02:00
("-cpdflin",
Arg.String setcpdflin,
" Set location of 'cpdflin'");
2014-10-15 14:48:39 +02:00
("-recrypt",
2014-10-15 18:51:15 +02:00
Arg.Unit setrecrypt,
2014-10-15 14:48:39 +02:00
" Keep this file's encryption when writing");
2013-08-20 16:32:57 +02:00
("-raw",
Arg.Unit (setencoding Cpdf.Raw),
" Do not process text");
("-stripped",
Arg.Unit (setencoding Cpdf.Stripped),
" Process text by simple stripping to ASCII");
("-utf8",
Arg.Unit (setencoding Cpdf.UTF8),
" Process text by conversion to UTF8 Unicode");
("-fast",
Arg.Unit setfast,
" Speed over correctness with malformed documents");
("-control",
Arg.String make_control_argv_and_parse,
2013-10-09 15:34:50 +02:00
" Use a control file. Deprecated. Use -args.");
2019-10-21 13:06:04 +02:00
("-args",
Arg.Unit nothing,
" Get arguments from a file.");
2013-08-20 16:32:57 +02:00
("-merge",
Arg.Unit (setop Merge),
" Merge a number of files into one");
("-retain-numbering",
Arg.Unit set_retain_numbering,
" Don't renumber pages when merging");
2019-07-03 15:40:32 +02:00
("-merge-add-bookmarks",
Arg.Unit setmergeaddbookmarks,
" Add bookmarks for each file to merged file");
("-merge-add-bookmarks-use-titles",
Arg.Unit setmergeaddbookmarksusetitles,
" Use title of document rather than filename");
2013-08-20 16:32:57 +02:00
("-remove-duplicate-fonts",
Arg.Unit set_remove_duplicate_fonts,
" Remove duplicate fonts when merging");
("-split",
Arg.Unit (setop Split),
" Split a file into individual pages");
("-chunk",
Arg.Int setchunk,
" Set chunk size for -split (default 1)");
("-split-bookmarks",
Arg.Int setsplitbookmarks,
" Split a file at bookmarks at a given level");
("-scale-page",
Arg.String setscale,
" -scale-page \"sx sy\" scales by (sx, sy)");
("-scale-to-fit",
Arg.String setscaletofit,
" -scale-to-fit \"x y\" scales to page size (x, y)");
("-scale-contents",
Arg.Float setscalecontents,
" Scale Contents by the given factor");
("-scale-to-fit-scale",
Arg.Float setscaletofitscale,
" -scale-to-fit-scale (1.0 = 100%)");
("-shift",
Arg.String setshift,
" -shift \"dx dy\" shifts the chosen pages");
("-rotate",
Arg.Int setrotate,
" Set rotation of pages to 0, 90, 180, 270");
("-rotateby",
Arg.Int setrotateby,
" Rotate pages by 90, 180 or 270 degrees");
("-rotate-contents",
Arg.Float setrotatecontents,
" Rotate contents of pages");
("-upright",
Arg.Unit (setop Upright),
" Make pages upright");
("-hflip",
Arg.Unit (setop HFlip),
" Flip pages horizontally");
("-vflip",
Arg.Unit (setop VFlip),
" Flip pages vertically");
("-crop",
Arg.String setcrop,
" Crop specified pages");
("-cropbox",
Arg.String setcrop,
" Crop specified pages (synonym for -crop)");
("-artbox",
Arg.String setart,
" Set art box for specified pages");
("-bleedbox",
Arg.String setbleed,
" Set bleed box for specified pages");
("-trimbox",
Arg.String settrim,
" Set trim box for specified pages");
2017-05-19 20:10:49 +02:00
("-hard-box",
Arg.String sethardbox,
" Hard crop specified pages to the given box");
2019-07-15 12:52:14 +02:00
("-show-boxes",
Arg.Unit (setop ShowBoxes),
" Show boxes by adding rectangles to pages");
2019-07-15 14:42:32 +02:00
("-trim-marks",
Arg.Unit (setop TrimMarks),
" Add trim marks");
2013-08-20 16:32:57 +02:00
("-remove-crop",
Arg.Unit (setop RemoveCrop),
" Remove cropping on specified pages");
("-remove-cropbox",
Arg.Unit (setop RemoveCrop),
" Synonym for -remove-crop");
("-remove-trimbox",
Arg.Unit (setop RemoveTrim),
" Remove trim box on specified pages");
("-remove-bleedbox",
Arg.Unit (setop RemoveBleed),
" Remove bleed box on specified pages");
("-remove-artbox",
Arg.Unit (setop RemoveArt),
" Remove art box on specified pages");
2013-08-20 16:32:57 +02:00
("-copy-cropbox-to-mediabox",
Arg.Unit (setop CopyCropBoxToMediaBox),
""); (* Undocumented now, since /frombox, /tobox now used *)
("-frombox", Arg.String setfrombox, " Set box to copy from");
("-tobox", Arg.String settobox, " Set box to copy to");
2015-01-21 13:20:49 +01:00
("-mediabox-if-missing",
Arg.Unit setmediaboxifmissing,
" If copy from box missing, substitute media box");
2013-08-20 16:32:57 +02:00
("-mediabox",
Arg.String setmediabox,
" Set media box on specified pages");
("-encrypt",
Arg.Unit setencryptcollect,
" Encrypt a document");
("-decrypt",
Arg.Unit (setop Decrypt),
" Decrypt a file");
("-no-edit", Arg.Unit set_no_edit, " No edits");
("-no-print", Arg.Unit set_no_print, " No printing");
("-no-copy", Arg.Unit set_no_copy, " No copying");
("-no-annot", Arg.Unit set_no_annot, " No annotations");
("-no-forms", Arg.Unit set_no_forms, " No forms");
("-no-extract", Arg.Unit set_no_extract, " No extracting");
("-no-assemble", Arg.Unit set_no_assemble, " No assembling");
("-no-hq-print", Arg.Unit set_no_hq_print, " No high quality printing");
("-no-encrypt-metadata",
Arg.Unit set_no_encrypt_metadata,
" Don't encrypt metadata (AES only)");
("-decompress",
Arg.Unit (setop Decompress),
" Decompress");
("-compress",
Arg.Unit (setop Compress),
" Compress streams, leaving metadata alone");
("-remove-duplicate-streams",
Arg.Unit setremoveduplicatestreams,
"");
("-list-bookmarks",
Arg.Unit (setop ListBookmarks),
" List Bookmarks");
("-remove-bookmarks",
Arg.Unit (setop RemoveBookmarks),
" Remove bookmarks from a file");
("-add-bookmarks",
Arg.String setaddbookmarks,
" Add bookmarks from the given file");
2019-07-06 18:55:26 +02:00
("-bookmarks-open-to-level",
Arg.Int setbookmarksopentolevel,
" Open bookmarks to this level (0 = all closed)");
2013-08-20 16:32:57 +02:00
("-presentation",
Arg.Unit (setop Presentation),
" Make a presentation");
("-trans",
Arg.String settrans,
" Set the transition method for -presentation");
("-duration",
Arg.Float setduration,
" Set the display duration for -presentation");
("-vertical",
Arg.Unit setvertical,
" Set dimension for Split and Blinds styles");
("-outward",
Arg.Unit setoutward,
" Set direction for Split and Box styles");
("-direction",
Arg.Int setdirection,
" Set direction for Wipe and Glitter styles");
("-effect-duration",
Arg.Float seteffectduration,
" Set the effect duration in seconds");
("-stamp-on",
Arg.String setstampon,
" Stamp a file on some pages of another");
("-stamp-under",
Arg.String setstampunder,
" Stamp a file under some pages of another");
2014-10-08 15:52:55 +02:00
("-scale-stamp-to-fit",
Arg.Unit setscalestamptofit,
" Scale the stamp to fit the page");
2013-08-20 16:32:57 +02:00
("-combine-pages",
Arg.String setcombinepages,
" Combine two files by merging individual pages");
("-add-text",
Arg.String setaddtext,
" Superimpose text on the given range of pages");
("-remove-text",
Arg.Unit (setop RemoveText),
" Remove text previously added by cpdf");
("-add-rectangle",
Arg.String setrectangle,
2016-11-08 19:15:04 +01:00
" Add a rectangle to the page");
2013-08-20 16:32:57 +02:00
("-bates",
Arg.Int setbates,
" Set the base bates number");
2015-07-26 13:31:43 +02:00
("-bates-at-range",
Arg.Int setbatesrange,
" Set the base bates number at first page in range");
2015-07-17 17:34:47 +02:00
("-bates-pad-to",
Arg.Int setbatespad,
" Pad the bates number with leading zeroes to width");
2013-08-20 16:32:57 +02:00
("-font",
Arg.String setfont,
" Set the font");
("-font-size",
Arg.Float setfontsize,
" Set the font size");
2015-04-13 15:17:48 +02:00
("-no-embed-font",
Arg.Unit setnoembedfont,
2016-11-04 17:46:08 +01:00
" Do not embed fonts");
2013-08-20 16:32:57 +02:00
("-color",
Arg.String setcolor,
" Set the color");
("-opacity",
Arg.Float setopacity,
" Set the text opacity");
("-outline",
Arg.Unit setoutline,
" Use outline mode for text");
("-linewidth",
Arg.Float setlinewidth,
" Set line width for outline text");
("-pos-center",
Arg.String setposcenter,
" Set position relative to center of baseline");
("-pos-left",
Arg.String setposleft,
" Set position relative to left of baseline");
("-pos-right",
Arg.String setposright,
" Set position relative to right of baseline");
("-top",
Arg.String settop,
" Set position relative to center top of page");
("-topleft",
Arg.String settopleft,
" Set position relative to top left of page");
("-topright",
Arg.String settopright,
" Set position relative to top right of page");
("-left",
Arg.String setleft,
" Set position relative to center left of page");
("-bottomleft",
Arg.String setbottomleft,
" Set position relative to bottom left of page");
("-bottom",
Arg.String setbottom,
" Set position relative to center bottom of page");
("-bottomright",
Arg.String setbottomright,
" Set position relative to bottom right of page");
("-right",
Arg.String setright,
" Set position relative to center right of page");
("-diagonal",
Arg.Unit setdiagonal,
" Place text diagonally across page");
("-reverse-diagonal",
Arg.Unit setreversediagonal,
" Place text diagonally across page from top left");
("-center",
Arg.Unit setcenter,
" Place text in the center of the page");
2013-08-20 16:32:57 +02:00
("-justify-left",
Arg.Unit setjustifyleft,
" Justify multiline text left");
("-justify-right",
Arg.Unit setjustifyright,
" Justify multiline text right");
("-justify-center",
Arg.Unit setjustifycenter,
2019-10-21 12:54:34 +02:00
" Justify multiline text center");
2013-08-20 16:32:57 +02:00
("-underneath",
Arg.Unit setunderneath,
" Text stamp is underneath content");
("-line-spacing",
Arg.Float setlinespacing,
" Line spacing (1 is normal)");
("-midline",
Arg.Unit setmidline,
" Adjust text to midline rather than baseline");
2015-01-20 16:50:36 +01:00
("-topline",
Arg.Unit settopline,
" Adjust text to topline rather than baseline");
2013-08-20 16:32:57 +02:00
("-relative-to-cropbox",
Arg.Unit setrelativetocropbox,
" Add text relative to Crop Box not Media Box");
2019-07-02 19:20:05 +02:00
("-embed-missing-fonts",
Arg.Unit (setop EmbedMissingFonts),
" Embed missing fonts by calling gs");
2013-08-20 16:32:57 +02:00
("-prerotate",
Arg.Unit setprerotate,
" Calls -upright on pages before adding text");
("-twoup",
Arg.Unit (setop TwoUp),
" Put 2 pages onto one");
("-twoup-stack",
Arg.Unit (setop TwoUpStack),
" Stack 2 pages onto one twice the size");
("-pad-before",
Arg.Unit (setop PadBefore),
" Add a blank page before the given pages");
("-pad-after",
Arg.Unit (setop PadAfter),
" Add a blank page after the given pages");
("-pad-every",
Arg.Int setpadevery,
" Add a blank page after every n pages");
2017-12-18 20:44:02 +01:00
("-pad-with",
Arg.String setpadwith,
" Use a given PDF instead of a blank page");
2013-08-20 16:32:57 +02:00
("-pad-multiple",
Arg.Int setpadmultiple,
" Pad the document to a multiple of n pages");
2019-07-01 16:35:17 +02:00
("-pad-multiple-before",
Arg.Int setpadmultiplebefore,
" Pad the document at beginning to a multiple of n pages");
2013-08-20 16:32:57 +02:00
("-list-annotations",
Arg.Unit (setop ListAnnotations),
" List annotations");
("-copy-annotations",
Arg.String setcopyannotations,
" Copy annotations from given file");
("-remove-annotations",
Arg.Unit (setop RemoveAnnotations),
" Remove annotations");
("-list-fonts",
Arg.Unit (setop Fonts),
" Output font list");
("-info",
Arg.Unit (setop Info),
" Output file information");
("-page-info",
Arg.Unit (setop PageInfo),
2019-10-21 12:54:34 +02:00
" Output page information");
2013-08-20 16:32:57 +02:00
("-set-author",
Arg.String setauthor,
" Set Author");
("-set-title",
Arg.String settitle,
" Set Title");
("-set-subject",
Arg.String setsubject,
" Set Subject");
("-set-keywords",
Arg.String setkeywords,
" Set Keywords");
("-set-create",
Arg.String setcreate,
" Set Creation date");
("-set-modify",
Arg.String setmodify,
" Set Modification date");
("-set-creator",
Arg.String setcreator,
" Set Creator");
("-set-producer",
Arg.String setproducer,
" Set Producer");
("-set-trapped",
Arg.Unit (setop SetTrapped),
" Mark as trapped");
("-set-untrapped",
Arg.Unit (setop SetUntrapped),
" Mark as not trapped");
2019-07-28 13:35:51 +02:00
("-also-set-xmp",
Arg.Unit setalsosetxml,
2019-07-28 13:35:51 +02:00
" Also set XMP metadata");
("-just-set-xmp",
Arg.Unit setjustsetxml,
2019-07-28 13:35:51 +02:00
" Just set XMP metadata, not old-fashioned metadata");
2019-07-01 15:40:22 +02:00
("-create-metadata",
Arg.Unit (setop CreateMetadata),
2019-10-21 12:54:34 +02:00
" Create XMP metadata from scratch.");
2013-08-20 16:32:57 +02:00
("-set-page-layout",
Arg.String setpagelayout,
" Set page layout upon document opening");
("-set-page-mode",
Arg.String setpagemode,
" Set page mode upon document opening");
2013-10-02 16:29:53 +02:00
("-open-at-page",
Arg.String setopenatpage,
2013-10-02 16:29:53 +02:00
" Set initial page");
("-open-at-page-fit",
Arg.String setopenatpagefit,
2013-10-02 16:29:53 +02:00
" Set inital page, scaling to fit");
2013-08-20 16:32:57 +02:00
("-set-metadata",
Arg.String setmetadata,
" Set metadata to the contents of a file");
("-print-metadata",
Arg.Unit (setop Metadata),
" Output metadata information");
("-remove-metadata",
Arg.Unit (setop RemoveMetadata),
" Remove document metadata");
2019-06-29 16:03:22 +02:00
("-set-metadata-date",
Arg.String setsetmetadatadate,
" Set the XMP metadata date property");
2013-08-20 16:32:57 +02:00
("-hide-toolbar",
Arg.String hidetoolbar,
" Hide the viewer's toolbar");
("-hide-menubar",
Arg.String hidemenubar,
" Hide the viewer's menubar");
("-hide-window-ui",
Arg.String hidewindowui,
" Hide the viewer's scroll bars etc.");
("-fit-window",
Arg.String fitwindow,
" Resize document's window to fit size of page");
("-center-window",
Arg.String centerwindow,
" Position window in the center of screen");
("-display-doc-title",
Arg.String displaydoctitle,
" Display document's title in the title bar");
("-pages",
Arg.Unit (setop CountPages),
" Count pages");
("-revisions",
Arg.Unit (setop Revisions),
2016-11-04 17:46:08 +01:00
"");
2013-08-20 16:32:57 +02:00
("-list-attached-files",
Arg.Unit (setop ListAttachedFiles),
" List attached files");
("-dump-attachments",
Arg.Unit (setop DumpAttachedFiles),
2019-07-14 14:50:48 +02:00
" Dump attachments to disk");
2013-08-20 16:32:57 +02:00
("-attach-file",
Arg.String setattachfile,
" Attach a file");
("-to-page",
Arg.String settopage,
" Attach file to given page instead of document");
("-remove-files",
Arg.Unit (setop RemoveAttachedFiles),
" Remove embedded attached document-level files");
("-image-resolution",
Arg.Float setimageresolution,
" List images under a given dpi");
("-copy-font",
Arg.String setcopyfont,
" Copy a named font");
("-copy-font-page",
Arg.Int setfontpage,
" Set the page a copied font is drawn from");
("-copy-font-name",
Arg.String setcopyfontname,
" Set the name of the font to copy");
2019-07-31 18:32:18 +02:00
("-remove-fonts",
Arg.Unit (setop RemoveFonts),
" Remove embedded fonts");
2013-08-20 16:32:57 +02:00
("-missing-fonts",
Arg.Unit (setop MissingFonts),
" List missing fonts");
("-remove-id",
Arg.Unit (setop RemoveId),
" Remove the file's /ID tag");
("-draft",
Arg.Unit (setop Draft),
" Remove images from the file");
2019-07-09 17:31:45 +02:00
("-draft-remove-only",
Arg.String setdraftremoveonly,
" Only remove named image");
2013-08-20 16:32:57 +02:00
("-boxes",
Arg.Unit setboxes,
" Add crossed boxes to -draft option");
2019-07-11 18:19:40 +02:00
("-remove-all-text",
Arg.Unit (setop RemoveAllText),
" Remove all text");
2013-08-20 16:32:57 +02:00
("-blacktext",
Arg.Unit (setop BlackText),
" Blacken document text");
("-blacklines",
Arg.Unit (setop BlackLines),
" Blacken lines in document");
("-blackfills",
Arg.Unit (setop BlackFills),
" Blacken fills in document");
("-thinlines",
Arg.String setthinlines,
" Set minimum line thickness to the given width");
2016-11-09 16:42:47 +01:00
("-remove-clipping",
Arg.Unit (setop RemoveClipping),
" Remove clipping paths");
2013-08-20 16:32:57 +02:00
("-clean",
Arg.Unit (setop Clean),
" Garbage-collect a file");
("-set-version",
Arg.Int setversion,
" Set PDF version number");
("-copy-id-from",
Arg.String setcopyid,
" Copy one file's ID tag to another");
("-print-page-labels",
Arg.Unit (setop PrintPageLabels),
" Print page labels");
("-remove-page-labels",
Arg.Unit (setop RemovePageLabels),
" Remove page labels");
("-add-page-labels",
2013-10-24 16:21:54 +02:00
Arg.Unit (setop AddPageLabels),
" Add or replace page labels");
2013-10-24 16:21:54 +02:00
("-label-style",
Arg.String setlabelstyle,
" Set label style (default DecimalArabic)");
("-label-prefix",
Arg.String setlabelprefix,
" Set label prefix (default none)");
2014-09-18 16:40:22 +02:00
("-label-startval",
Arg.Int setlabelstartval,
" Set label start value (default 1)");
("-labels-progress",
Arg.Unit setlabelsprogress,
" Label start value progresses with multiple ranges");
2015-01-07 19:29:11 +01:00
("-remove-dict-entry",
Arg.String setremovedictentry,
" Remove an entry from all dictionaries");
2015-01-22 20:16:56 +01:00
("-producer",
Arg.String setproducer,
" Change the /Producer entry in the /Info dictionary");
("-creator",
Arg.String setcreator,
" Change the /Creator entry in the /Info dictionary");
2016-11-08 19:15:04 +01:00
("-list-spot-colors",
2016-11-03 18:11:08 +01:00
Arg.Unit (setop ListSpotColours),
2016-11-08 19:15:04 +01:00
" List spot colors");
("-create-pdf",
Arg.Unit (setop CreatePDF),
" Create a new PDF");
("-create-pdf-pages",
Arg.Int setcreatepdfpages,
" Number of pages for new PDF");
("-create-pdf-papersize",
Arg.String setcreatepdfpapersize,
" Paper size for new PDF");
("-prepend-content",
Arg.String setprepend,
" Prepend content to page");
("-postpend-content",
Arg.String setpostpend,
" Postpend content to page");
("-gs", Arg.String setgspath, " Path to gs executable");
2019-07-22 15:00:37 +02:00
("-gs-malformed", Arg.Unit setgsmalformed, " Also try to reconstruct malformed files with gs");
("-gs-quiet", Arg.Unit setgsquiet, " Make gs go into quiet mode");
2016-11-04 17:46:08 +01:00
("-squeeze", Arg.Unit setsqueeze, " Squeeze");
("-squeeze-log-to", Arg.String setsqueezelogto, " Squeeze log location");
("-squeeze-no-pagedata", Arg.Unit setsqueezepagedata, " Don't recompress pages");
("-squeeze-no-recompress", Arg.Unit setsqueezerecompress, " Don't recompress streams");
(* Just for error reporting *)
("-gs-malformed-force", Arg.Unit whingemalformed, "");
2020-02-26 17:24:27 +01:00
(* These items are not documented yet, but will be soon *)
2020-01-30 11:20:33 +01:00
("-output-json", Arg.Unit (setop OutputJSON), "");
("-output-json-parse-content-streams", Arg.Unit setjsonparsecontentstreams, "");
2020-02-01 11:18:15 +01:00
("-output-json-no-stream-data", Arg.Unit setjsonnostreamdata, "");
2020-02-26 17:24:27 +01:00
("-ocg-coalesce-on-name", Arg.Unit (setop OCGCoalesce), "");
2020-02-27 14:32:45 +01:00
("-ocg-list", Arg.Unit (setop OCGList), "");
("-ocg-rename", Arg.Unit (setop OCGRename), "");
("-ocg-rename-from", Arg.String setocgrenamefrom, "");
("-ocg-rename-to", Arg.String setocgrenameto, "");
2020-03-02 13:37:39 +01:00
("-ocg-order-all", Arg.Unit (setop OCGOrderAll), "");
("-stamp-as-xobject", Arg.String setstampasxobject, "");
2020-02-26 17:24:27 +01:00
(* These items are undocumented *)
("-remove-unused-resources", Arg.Unit (setop RemoveUnusedResources), "");
("-stay-on-error", Arg.Unit setstayonerror, "");
2013-08-20 16:32:57 +02:00
("-extract-fontfile", Arg.Unit (setop ExtractFontFile), "");
2019-07-08 13:44:27 +02:00
("-extract-images", Arg.Unit (setop ExtractImages), "");
2013-08-20 16:32:57 +02:00
("-csp1", Arg.Unit (setop CSP1), "");
("-csp2", Arg.Float setcsp2, "");
("-csp3", Arg.Unit (setop CSP3), "");
2019-07-14 14:50:48 +02:00
("-text-vertical", Arg.Unit settextvertical, "");
("-text-vertical-down", Arg.Unit settextverticaldown, "");
2013-08-20 16:32:57 +02:00
("-flat-kids", Arg.Unit setflatkids, "");
("-debug", Arg.Unit setdebug, "");
2014-10-24 18:24:29 +02:00
("-debug-crypt", Arg.Unit setdebugcrypt, "");
2018-03-20 11:19:50 +01:00
("-debug-force", Arg.Unit setdebugforce, "");
("-debug-malformed", Arg.Set Pdfread.debug_always_treat_malformed, "");
2013-08-20 16:32:57 +02:00
("-fix-prince", Arg.Unit (setop RemoveUnusedResources), "");
2016-11-09 19:15:23 +01:00
("-extract-text", Arg.Unit (setop ExtractText), "");
2016-11-13 15:02:09 +01:00
("-extract-text-font-size", Arg.Float setextracttextfontsize, "");
2016-11-10 16:03:34 +01:00
]
2013-08-20 16:32:57 +02:00
and usage_msg =
"Syntax: cpdf <op> <op-specific arguments> [-o <output file>] <input files>\n\n\
This is a copyrighted, commercial program, and may NOT be freely copied.\n\n\
Version " ^ string_of_int major_version ^ "." ^ string_of_int minor_version ^ " " ^ version_date ^ "\n\n\
To buy, visit http://www.coherentpdf.com/\n\n\
Input names are distinguished by containing a '.' and may be\n\
followed by a page range specification, for instance \"1,2,3\"\n\
or \"1-6,9-end\" or \"even\" or \"odd\" or \"reverse\".\n\nOperations (See \
manual for full details):\n"
(* Reading and writing *)
2014-10-14 20:36:57 +02:00
let filesize name =
try
let x = open_in_bin name in
let r = in_channel_length x in
close_in x;
r
with
_ -> 0
2013-08-20 16:32:57 +02:00
2019-07-02 19:20:05 +02:00
(* Embed missing fonts with Ghostscript. *)
let embed_missing_fonts fi fo =
if args.path_to_ghostscript = "" then begin
Printf.eprintf "Please supply path to gs with -gs\n";
2019-07-08 15:44:34 +02:00
exit 2
2019-07-02 19:20:05 +02:00
end;
let gscall =
args.path_to_ghostscript ^
2019-08-18 16:22:13 +02:00
" -dNOPAUSE " ^ (if args.gs_quiet then "-dQUIET" else "") ^ " -sDEVICE=pdfwrite -sOUTPUTFILE=" ^ Filename.quote fo ^
" -dBATCH " ^ Filename.quote fi
2019-07-02 19:20:05 +02:00
in
match Sys.command gscall with
| 0 -> exit 0
| _ -> Printf.eprintf "Font embedding failed.\n"; exit 2
(* Mend PDF file with Ghostscript. We use this if a file is malformed and CPDF
* cannot mend it. It is copied to a temporary file, fixed, then we return None or Some (pdf). *)
let mend_pdf_file_with_ghostscript filename =
if args.path_to_ghostscript = "" then begin
Printf.eprintf "Please supply path to gs with -gs\n";
end;
Printf.eprintf "CPDF could not mend. Attempting to mend file with gs\n";
flush stderr;
let tmpout = Filename.temp_file "cpdf" ".pdf" in
2019-06-30 16:00:51 +02:00
tempfiles := tmpout::!tempfiles;
let gscall =
args.path_to_ghostscript ^
2019-08-18 16:22:13 +02:00
" -dNOPAUSE " ^ (if args.gs_quiet then "-dQUIET" else "") ^ " -sDEVICE=pdfwrite -sOUTPUTFILE=" ^ Filename.quote tmpout ^
" -dBATCH " ^ Filename.quote filename
in
match Sys.command gscall with
| 0 -> Printf.eprintf "Succeeded!\n"; flush stderr; tmpout
| _ -> Printf.eprintf "Could not fix malformed PDF file, even with gs\n"; flush stderr; exit 2
2019-07-02 15:10:42 +02:00
exception StdInBytes of bytes
let pdf_of_stdin ?revision user_pw owner_pw =
2019-07-02 15:10:42 +02:00
let rbytes = ref (mkbytes 0) in
try
let user_pw = Some user_pw
and owner_pw = if owner_pw = "" then None else Some owner_pw in
let o, bytes = Pdfio.input_output_of_bytes 16384 in
try
while true do o.Pdfio.output_char (input_char stdin) done;
Pdf.empty ()
with
End_of_file ->
let thebytes = Pdfio.extract_bytes_from_input_output o bytes in
rbytes := thebytes;
let i = Pdfio.input_of_bytes thebytes in
pdfread_pdf_of_input ?revision user_pw owner_pw i
with
_ -> raise (StdInBytes !rbytes)
2014-10-15 18:51:15 +02:00
2019-07-02 16:50:36 +02:00
let rec get_single_pdf ?(decrypt=true) ?(fail=false) op read_lazy =
2019-07-02 15:10:42 +02:00
let failout () =
if fail then begin
(* Reconstructed with ghostscript, but then we couldn't read it even then. Do not loop. *)
Printf.eprintf "Failed to read gs-reconstructed PDF even though gs succeeded\n";
exit 2
end
in
let warn_gs () =
2019-07-12 15:53:55 +02:00
begin match args.inputs with
(InFile inname, _, _, _, _, _)::_ ->
begin try ignore (close_in (open_in inname)) with _ ->
2019-07-17 14:05:00 +02:00
Printf.eprintf "File %s does not exist\n" inname;
2019-07-12 15:53:55 +02:00
exit 2
end
| _ -> ()
end;
Printf.eprintf "get_single_pdf: failed to read malformed PDF file. Consider using -gs-malformed\n";
2019-07-02 15:10:42 +02:00
exit 2
in
2014-10-15 18:51:15 +02:00
match args.inputs with
| (InFile inname, x, u, o, y, revision) as input::more ->
2014-10-15 18:51:15 +02:00
if args.squeeze then
Printf.printf "Initial file size is %i bytes\n" (filesize inname);
let pdf =
try
if read_lazy then
pdfread_pdf_of_channel_lazy ?revision (optstring u) (optstring o) (open_in_bin inname)
else
pdfread_pdf_of_file ?revision (optstring u) (optstring o) inname
with
| Cpdf.SoftError _ as e -> raise e (* Bad owner or user password *)
| _ ->
if args.gs_malformed then
begin
2019-07-02 15:10:42 +02:00
failout ();
let newname = mend_pdf_file_with_ghostscript inname in
args.inputs <- (InFile newname, x, u, o, y, revision)::more;
get_single_pdf ~fail:true op read_lazy
end
else
2019-07-02 15:10:42 +02:00
warn_gs ()
in
args.was_encrypted <- Pdfcrypt.is_encrypted pdf;
2019-07-02 16:50:36 +02:00
if decrypt then decrypt_if_necessary input op pdf else pdf
2019-07-02 15:10:42 +02:00
| (StdIn, x, u, o, y, revision) as input::more ->
let pdf =
try pdf_of_stdin ?revision u o with
StdInBytes b ->
if args.gs_malformed then
begin
2019-07-02 15:10:42 +02:00
failout ();
let inname = Filename.temp_file "cpdf" ".pdf" in
tempfiles := inname::!tempfiles;
let fh = open_out_bin inname in
Pdfio.bytes_to_output_channel fh b;
close_out fh;
let newname = mend_pdf_file_with_ghostscript inname in
args.inputs <- (InFile newname, x, u, o, y, revision)::more;
get_single_pdf ~fail:true op read_lazy
end
2019-07-02 15:10:42 +02:00
else
warn_gs ()
2014-10-15 18:51:15 +02:00
in
args.was_encrypted <- Pdfcrypt.is_encrypted pdf;
2019-07-02 16:50:36 +02:00
if decrypt then decrypt_if_necessary input op pdf else pdf
| (AlreadyInMemory pdf, _, _, _, _, _)::_ -> pdf
2014-10-15 18:51:15 +02:00
| _ ->
raise (Arg.Bad "cpdf: No input specified.\n")
let get_single_pdf_nodecrypt read_lazy =
2019-07-02 16:50:36 +02:00
get_single_pdf ~decrypt:false None read_lazy
let filenames = null_hash ()
2019-08-16 16:16:21 +02:00
let squeeze_logto filename x =
let fh = open_out_gen [Open_wronly; Open_creat] 0o666 filename in
seek_out fh (out_channel_length fh);
output_string fh x;
close_out fh
2019-07-02 16:50:36 +02:00
(* This now memoizes on the name of the file to make sure we only load each
file once *)
2019-07-02 17:50:39 +02:00
let rec get_pdf_from_input_kind ?(read_lazy=false) ?(decrypt=true) ?(fail=false) ((_, x, u, o, y, revision) as input) op ik =
2019-07-02 16:50:36 +02:00
let failout () =
if fail then begin
(* Reconstructed with ghostscript, but then we couldn't read it even then. Do not loop. *)
Printf.eprintf "Failed to read gs-reconstructed PDF even though gs succeeded\n";
exit 2
end
in
let warn_gs () =
2019-07-12 15:53:55 +02:00
begin match input with
(InFile inname, _, _, _, _, _) ->
begin try ignore (close_in (open_in inname)) with _ ->
Printf.eprintf "File %s does not exist\n" inname;
exit 2
end
| _ -> ()
end;
Printf.eprintf "get_pdf_from_input_kind: failed to read malformed PDF file. Consider using -gs-malformed\n";
2019-07-02 16:50:36 +02:00
exit 2
in
match ik with
| AlreadyInMemory pdf -> pdf
| InFile s ->
2014-10-15 18:51:15 +02:00
if args.squeeze then
2019-07-02 16:50:36 +02:00
begin
let size = filesize s in
initial_file_size := size;
2019-08-16 16:16:21 +02:00
let str = Printf.sprintf "Initial file size is %i bytes\n" size in
begin match !logto with
| None -> print_string str
| Some filename -> squeeze_logto filename str
end
2019-07-02 16:50:36 +02:00
end;
begin try Hashtbl.find filenames s with
Not_found ->
let pdf =
2019-07-02 17:50:39 +02:00
try
if read_lazy then
pdfread_pdf_of_channel_lazy ?revision (optstring u) (optstring o) (open_in_bin s)
else
pdfread_pdf_of_file ?revision (optstring u) (optstring o) s
with
| Cpdf.SoftError _ as e -> raise e (* Bad owner or user password *)
| e ->
Printf.printf "%s\n" (Printexc.to_string e);
2019-07-02 16:50:36 +02:00
if args.gs_malformed then
begin
failout ();
let newname = mend_pdf_file_with_ghostscript s in
get_pdf_from_input_kind ~fail:true (InFile newname, x, u, o, y, revision) op (InFile newname);
end
else
warn_gs ()
in
args.was_encrypted <- Pdfcrypt.is_encrypted pdf;
let pdf = if decrypt then decrypt_if_necessary input op pdf else pdf in
Hashtbl.add filenames s pdf; pdf
end
| StdIn ->
let pdf =
try pdf_of_stdin ?revision u o with
StdInBytes b ->
if args.gs_malformed then
begin
failout ();
let inname = Filename.temp_file "cpdf" ".pdf" in
tempfiles := inname::!tempfiles;
let fh = open_out_bin inname in
Pdfio.bytes_to_output_channel fh b;
close_out fh;
let newname = mend_pdf_file_with_ghostscript inname in
get_pdf_from_input_kind ~fail:true (InFile newname, x, u, o, y, revision) op (InFile newname);
end
else
warn_gs ()
in
args.was_encrypted <- Pdfcrypt.is_encrypted pdf;
if decrypt then decrypt_if_necessary input op pdf else pdf
2014-10-15 18:51:15 +02:00
2015-01-22 20:16:56 +01:00
let rec unescape_octals prev = function
| [] -> rev prev
| '\\'::('0'..'9' as a)::('0'..'9' as b)::('0'..'9' as c)::t ->
let chr = char_of_int (int_of_string ("0o" ^ implode [a;b;c])) in
unescape_octals (chr::prev) t
| '\\'::'\\'::t -> unescape_octals ('\\'::prev) t
| h::t -> unescape_octals (h::prev) t
let unescape_octals s =
implode (unescape_octals [] (explode s))
let process s =
if args.encoding <> Cpdf.Raw
then Pdftext.pdfdocstring_of_utf8 s
else unescape_octals s
let set_producer s pdf =
ignore (Cpdf.set_pdf_info ("/Producer", Pdf.String (process s), 0) pdf)
let set_creator s pdf =
ignore (Cpdf.set_pdf_info ("/Creator", Pdf.String (process s), 0) pdf)
let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outname =
2015-01-22 20:16:56 +01:00
if args.producer <> None then set_producer (unopt args.producer) pdf;
2016-07-22 19:42:55 +02:00
if noncomp then
set_producer "cpdf non-commercial use only. To buy: http://coherentpdf.com/"
pdf;
2015-01-22 20:16:56 +01:00
if args.creator <> None then set_creator (unopt args.creator) pdf;
2014-10-28 19:40:56 +01:00
if args.debugcrypt then Printf.printf "really_write_pdf\n%!";
2014-12-02 16:12:28 +01:00
let will_linearize =
args.linearize || args.keeplinearize && pdf.Pdf.was_linearized
in
2014-10-02 14:32:30 +02:00
let outname' =
2014-12-02 16:12:28 +01:00
if will_linearize then Filename.temp_file "cpdflin" ".pdf" else outname
2014-10-02 14:32:30 +02:00
in
if args.debugcrypt then
2014-11-19 18:36:02 +01:00
Printf.printf "args.recrypt = %b, args.was_encrypted = %b\n"
args.recrypt args.was_encrypted;
2014-10-24 18:24:29 +02:00
begin
if args.recrypt && args.was_encrypted then
begin
2014-11-19 18:36:02 +01:00
if args.debugcrypt then
Printf.printf "Recrypting in really_write_pdf\n";
2014-10-30 18:00:00 +01:00
match args.inputs with
[] -> raise (Pdf.PDFError "no input in recryption")
| (_, _, user_pw, owner_pw, _, _)::_ ->
2014-11-19 18:36:02 +01:00
let best_password =
if owner_pw <> "" then owner_pw else user_pw
in
2014-11-02 15:59:37 +01:00
Pdfwrite.pdf_to_file_options
~preserve_objstm:args.preserve_objstm
~generate_objstm:args.create_objstm
~compress_objstm:(not is_decompress)
~recrypt:(Some best_password)
false None mk_id pdf outname'
2014-10-24 18:24:29 +02:00
end
else
begin
2018-03-20 16:53:25 +01:00
if args.debugforce || not args.was_encrypted || args.was_decrypted_with_owner then
begin
2014-11-19 18:36:02 +01:00
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
2014-11-19 18:36:02 +01:00
soft_error
"You must supply -recrypt here, or provide the owner password."
2014-10-24 18:24:29 +02:00
end
2014-10-15 18:51:15 +02:00
end;
2014-10-14 20:36:57 +02:00
begin
2014-12-02 16:12:28 +01:00
if will_linearize then
2014-11-24 13:02:36 +01:00
let cpdflin = Cpdf.find_cpdflin args.cpdflin in
2014-11-19 18:36:02 +01:00
match args.inputs with
[] -> raise (Pdf.PDFError "no input in recryption")
| (_, _, user_pw, owner_pw, _, _)::_ ->
2014-11-19 18:36:02 +01:00
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;
2014-10-14 20:36:57 +02:00
end;
if args.squeeze then
let s = filesize outname in
2019-08-16 16:16:21 +02:00
begin
let str =
Printf.sprintf
"Final file size is %i bytes, %.2f%% of original.\n"
s
((float s /. float !initial_file_size) *. 100.)
in
match !logto with
| None -> print_string str
| Some filename -> squeeze_logto filename str
end
2014-10-02 14:32:30 +02:00
2014-10-02 15:19:05 +02:00
let write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf =
if args.debugcrypt then Printf.printf "write_pdf\n";
2013-08-20 16:32:57 +02:00
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 ->
2014-10-14 20:36:57 +02:00
begin match encryption with
None ->
2014-10-22 12:48:20 +02:00
let pdf = nobble pdf in
if not is_decompress then
begin
ignore (Cpdf.recompress_pdf pdf);
if args.squeeze then Cpdf.squeeze ~pagedata:args.squeeze_pagedata ~recompress:args.squeeze_recompress ?logto:!logto pdf;
2014-10-22 12:48:20 +02:00
end;
2014-11-13 16:32:14 +01:00
Pdf.remove_unreferenced pdf;
really_write_pdf ~is_decompress mk_id pdf outname
2014-10-14 20:36:57 +02:00
| Some _ ->
really_write_pdf ~encryption ~is_decompress mk_id pdf outname
2014-10-14 20:36:57 +02:00
end
2013-08-20 16:32:57 +02:00
| Stdout ->
2018-02-13 19:05:48 +01:00
(* FIXME Do not use a temp file if not calling cpdflin *)
let temp = Filename.temp_file "cpdfstdout" ".pdf" in
2014-10-02 14:32:30 +02:00
begin match encryption with
None ->
2014-10-22 12:48:20 +02:00
let pdf = nobble pdf in
if not is_decompress then
begin
ignore (Cpdf.recompress_pdf pdf);
if args.squeeze then Cpdf.squeeze ~pagedata:args.squeeze_pagedata ~recompress:args.squeeze_recompress ?logto:!logto pdf;
2014-10-22 12:48:20 +02:00
Pdf.remove_unreferenced pdf
end;
really_write_pdf ~encryption ~is_decompress mk_id pdf temp;
2014-10-02 14:32:30 +02:00
| Some _ ->
really_write_pdf ~encryption ~is_decompress mk_id pdf temp
2014-10-02 14:32:30 +02:00
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 ->
2018-02-13 19:14:48 +01:00
begin try close_in temp_file; Sys.remove temp with
e -> Printf.eprintf "Failed to remove temp file %s (%s)\n" temp (Printexc.to_string e)
end;
flush stdout (*r For Windows *)
2013-08-20 16:32:57 +02:00
2019-07-14 14:50:48 +02:00
(* Remove characters which might not make good filenames. *)
let remove_unsafe_characters s =
if args.encoding = Cpdf.Raw then s else
let chars =
lose
(function x ->
match x with
'/' | '?' | '<' | '>' | '\\' | ':' | '*' | '|' | '\"' | '^' | '+' | '=' -> true
| x when int_of_char x < 32 || (int_of_char x > 126 && args.encoding <> Cpdf.Stripped) -> true
| _ -> false)
(explode s)
in
match chars with
| '.'::more -> implode more
| chars -> implode chars
let get_bookmark_name pdf marks splitlevel n _ =
2017-05-28 20:19:17 +02:00
let refnums = Pdf.page_reference_numbers pdf in
2017-05-29 15:39:01 +02:00
let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in
match keep (function m -> n = Pdfpage.pagenumber_of_target ~fastrefnums pdf m.Pdfmarks.target && m.Pdfmarks.level <= splitlevel) marks with
| {Pdfmarks.text = title}::_ -> remove_unsafe_characters title
| _ -> ""
(* @F means filename without extension *)
(* @N means sequence number with no padding *)
(* @S means start page of this section *)
(* @E means end page of this section *)
(* @B means bookmark name at start page *)
let process_others marks pdf splitlevel filename sequence startpage endpage s =
2019-07-01 17:52:31 +02:00
let rec find_ats p = function
'@'::r -> find_ats (p + 1) r
| r -> (p, r)
in
2019-07-01 17:52:31 +02:00
let string_of_int_width w i =
if w < 0 then raise (Pdf.PDFError "width of field too narrow")
else if w > 8 then raise (Pdf.PDFError "width of field too broad") else
let formats =
[|format_of_string "%i";
format_of_string "%i";
format_of_string "%02i";
format_of_string "%03i";
format_of_string "%04i";
format_of_string "%05i";
format_of_string "%06i";
format_of_string "%07i";
format_of_string "%08i"|]
in
Printf.sprintf formats.(w) i
in
let rec procss prev = function
| [] -> rev prev
| '@'::'F'::t -> procss (rev (explode filename) @ prev) t
| '@'::'N'::t ->
let width, rest = find_ats 0 t in
procss (rev (explode (string_of_int_width width sequence)) @ prev) rest
| '@'::'S'::t ->
let width, rest = find_ats 0 t in
procss (rev (explode (string_of_int_width width startpage)) @ prev) rest
| '@'::'E'::t ->
let width, rest = find_ats 0 t in
procss (rev (explode (string_of_int_width width endpage)) @ prev) rest
| '@'::'B'::t -> procss (rev (explode (get_bookmark_name pdf marks splitlevel startpage pdf)) @ prev) t
| h::t -> procss (h::prev) t
in
implode (procss [] (explode s))
let name_of_spec marks (pdf : Pdf.t) splitlevel spec n filename startpage endpage =
let fill l n =
let chars = explode (string_of_int n) in
if length chars > l
then implode (drop chars (length chars - l))
else implode ((many '0' (l - length chars)) @ chars)
in
let chars = explode spec in
let before, including = cleavewhile (neq '%') chars in
let percents, after = cleavewhile (eq '%') including in
if percents = []
then
process_others marks pdf splitlevel filename n startpage endpage spec
else
process_others marks pdf splitlevel filename n startpage endpage
(implode before ^ fill (length percents) n ^ implode after)
(* Find the stem of a filename *)
let stem s =
2014-10-28 19:40:56 +01:00
implode
(rev (tail_no_fail
(dropwhile
(neq '.') (rev (explode (Filename.basename s))))))
let fast_write_split_pdfs
2014-11-03 20:19:12 +01:00
enc splitlevel original_filename 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
2014-10-28 19:40:56 +01:00
let name =
name_of_spec
marks main_pdf splitlevel spec number
(stem original_filename) startpage endpage
in
Pdf.remove_unreferenced pdf;
if sq then Cpdf.squeeze ~pagedata:args.squeeze_pagedata ~recompress:args.squeeze_recompress ?logto:!logto pdf;
really_write_pdf ~encryption:enc (not (enc = None)) pdf name)
(indx pagenums)
pagenums
(* Return list, in order, a *set* of page numbers of bookmarks at a given level *)
let bookmark_pages level pdf =
2017-05-28 20:19:17 +02:00
let refnums = Pdf.page_reference_numbers pdf in
2017-05-29 15:39:01 +02:00
let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in
2017-05-28 20:19:17 +02:00
setify_preserving_order
(option_map
(function
l when l.Pdfmarks.level = level ->
2017-05-29 15:39:01 +02:00
Some (Pdfpage.pagenumber_of_target ~fastrefnums pdf l.Pdfmarks.target)
2017-05-28 20:19:17 +02:00
| _ -> None)
(Pdfmarks.read_bookmarks pdf))
let split_at_bookmarks
2014-11-03 20:19:12 +01:00
enc original_filename ~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
2014-11-03 20:19:12 +01:00
enc level original_filename squeeze nobble spec pdf pts pdf_pages
let split_pdf
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
2014-11-03 20:19:12 +01:00
enc 0 original_filename squeeze nobble spec pdf
(splitinto chunksize (indx pdf_pages)) pdf_pages
2013-08-20 16:32:57 +02:00
(* Copy a font from [frompdf] with name [fontname] on page [fontpage] to [pdf] on all pages in [range] *)
let copy_font frompdf fontname fontpage range pdf =
match Pdf.renumber_pdfs [frompdf; pdf] with
| [] | [_] | _::_::_::_ -> assert false
| [frompdf; pdf] ->
(* 1. Get fontpage *)
let frompdf_pages = Pdfpage.pages_of_pagetree frompdf in
let frompdf_page =
try select fontpage frompdf_pages with
Not_found -> failwith "copy_font: Page not found in input pdf"
in
(* 2. Extract font *)
let fonts =
match Pdf.lookup_direct frompdf "/Font" frompdf_page.Pdfpage.resources with
| Some f -> f
| None -> failwith "copy_font: font not found"
in
let fromfont =
match Pdf.lookup_direct frompdf fontname fonts with
| Some f -> f
| None -> failwith "copy_font: font not found"
in
let basefontname =
match Pdf.lookup_direct frompdf "/BaseFont" fromfont with
| Some (Pdf.Name n) -> n
| _ -> "/CopyFontAddedNoName"
in
(* 3. Get all objects forming font (except main /Font one) *)
let objnumbers = Pdf.objects_referenced [] [] frompdf fromfont in
(* 4. Copy them to from frompdf to pdf. *)
iter (function objnum -> Pdf.addobj_given_num pdf (objnum, Pdf.lookup_obj frompdf objnum)) objnumbers;
(* 5. Get pages from pdf *)
let pdf_pages = Pdfpage.pages_of_pagetree pdf in
(* 6. Add the font to pages in range *)
let pages' =
map
(function (page, pagenum) ->
if mem pagenum range then
let font =
match Pdf.lookup_direct pdf "/Font" page.Pdfpage.resources with
| Some f -> f
| None -> Pdf.Dictionary []
in
let font' =
match font with
| (Pdf.Dictionary _) as d ->
Pdf.add_dict_entry d basefontname fromfont
| _ -> failwith "copy_font: error"
in
let resources' =
Pdf.add_dict_entry page.Pdfpage.resources "/Font" font'
in
{page with
Pdfpage.resources = resources'}
else page)
(combine pdf_pages (indx pdf_pages));
in
(* 7. Put the pages back into the pdf, and return *)
let pdf, root = Pdfpage.add_pagetree pages' pdf in
Pdfpage.add_root root [] pdf
(* Extract Images. *)
let pnm_to_channel_24 channel w h s =
let white () = output_char channel ' '
and newline () = output_char channel '\n'
and output_string = Pervasives.output_string channel in
output_string "P6";
white ();
output_string (string_of_int w);
white ();
output_string (string_of_int h);
white ();
output_string "255";
newline ();
let pos = ref 0 in
for y = 1 to h do
for x = 1 to w * 3 do
output_byte channel (bget s !pos);
incr pos
done
done
let null_device =
match Sys.os_type with
| "Win32" -> "nul"
| _ -> "/dev/null"
(* cpdf -extract-images in.pdf 2-5 -o img%%% (FIXME: Add output spec. Document png stuff.) *)
let write_stream name stream =
let fh = open_out_bin name in
for x = 0 to bytes_size stream - 1 do
output_byte fh (bget stream x)
done;
close_out fh
let write_image pdf resources name image =
match Pdfimage.get_image_24bpp pdf resources image with
| Pdfimage.JPEG (stream, _) -> write_stream (name ^ ".jpg") stream
| Pdfimage.JPEG2000 (stream, _) -> write_stream (name ^ ".jpx") stream
| Pdfimage.JBIG2 (stream, _) -> write_stream (name ^ ".jbig2") stream
| Pdfimage.Raw (w, h, Pdfimage.BPP24, stream) ->
let fh = open_out_bin (name ^ ".pnm") in
pnm_to_channel_24 fh w h stream;
close_out fh;
(* If pnmtopng is present, convert the pnm to a PNG. *)
begin match
Sys.command ("pnmtopng -gamma 0.45 -quiet " ^ "\"" ^ name ^ ".pnm\"" ^ "> \"" ^ name ^ ".png\" 2>" ^ null_device)
with
| 0 -> Sys.remove (name ^ ".pnm")
| _ -> ()
end
| _ -> ()
(* FIXME: Doesn't cope with images within form xobjects *)
let extract_images pdf range stem =
let pdf_pages = Pdfpage.pages_of_pagetree pdf in
let pages =
option_map
(function (i, pdf_pages) -> if mem i range then Some pdf_pages else None)
(combine (indx pdf_pages) pdf_pages)
in
iter2
(fun page pnum ->
2013-08-20 16:32:57 +02:00
let xobjects =
match Pdf.lookup_direct pdf "/XObject" page.Pdfpage.resources with
| Some (Pdf.Dictionary elts) -> map snd elts
| _ -> []
in
let images =
keep (fun o -> Pdf.lookup_direct pdf "/Subtype" o = Some (Pdf.Name "/Image")) xobjects
in
if images <> [] then
(let names =
map
(function n ->
2019-07-01 17:52:31 +02:00
let r = name_of_spec [] pdf 0 ("p" ^ string_of_int pnum ^ "_" ^ stem) n "" 0 0 in r)
(indx images)
2013-08-20 16:32:57 +02:00
in
iter2 (write_image pdf page.Pdfpage.resources) names images))
pages
(indx pages)
2013-08-20 16:32:57 +02:00
let getencryption pdf =
match Pdfread.what_encryption pdf with
| None | Some Pdfwrite.AlreadyEncrypted -> "Not encrypted"
| Some Pdfwrite.PDF40bit -> "40bit"
| Some Pdfwrite.PDF128bit -> "128bit"
| Some (Pdfwrite.AES128bit true) -> "128bit AES, Metadata encrypted"
| Some (Pdfwrite.AES128bit false) -> "128bit AES, Metadata not encrypted"
| Some (Pdfwrite.AES256bit true) -> "256bit AES, Metadata encrypted"
| Some (Pdfwrite.AES256bit false) -> "256bit AES, Metadata not encrypted"
| Some (Pdfwrite.AES256bitISO true) -> "256bit AES ISO, Metadata encrypted"
| Some (Pdfwrite.AES256bitISO false) -> "256bit AES ISO, Metadata not encrypted"
(* If a cropbox exists, make it the mediabox. If not, change nothing. *)
let copy_cropbox_to_mediabox pdf range =
Cpdf.process_pages
(fun _ page ->
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some pdfobject -> {page with Pdfpage.mediabox = Pdf.direct pdf pdfobject}
| None -> page)
pdf
range
(* Image resolution *)
type xobj =
| Image of int * int (* width, height *)
| Form of Pdftransform.transform_matrix * Pdf.pdfobject * Pdf.pdfobject (* Will add actual data later. *)
(* Given a page and a list of (pagenum, name, thing) *)
let rec image_resolution_page pdf page pagenum dpi (images : (int * string * xobj) list) =
try
let pageops = Pdfops.parse_operators pdf page.Pdfpage.resources page.Pdfpage.content
and transform = ref [ref Pdftransform.i_matrix] in
iter
(function
| Pdfops.Op_cm matrix ->
begin match !transform with
| [] -> raise (Failure "no transform")
| _ -> (hd !transform) := Pdftransform.matrix_compose !(hd !transform) matrix
end
| Pdfops.Op_Do xobject ->
let trans (x, y) =
match !transform with
| [] -> raise (Failure "no transform")
| _ -> Pdftransform.transform_matrix !(hd !transform) (x, y)
in
let o = trans (0., 0.)
and x = trans (1., 0.)
and y = trans (0., 1.)
in
(*i Printf.printf "o = %f, %f, x = %f, %f, y = %f, %f\n" (fst o) (snd o) (fst x) (snd x) (fst y) (snd y); i*)
let rec lookup_image k = function
| [] -> assert false
| (_, a, _) as h::_ when a = k -> h
| _::t -> lookup_image k t
in
begin match lookup_image xobject images with
| (pagenum, name, Form (xobj_matrix, content, resources)) ->
let content =
(* Add in matrix etc. *)
let total_matrix = Pdftransform.matrix_compose xobj_matrix !(hd !transform) in
let ops =
Pdfops.Op_cm total_matrix::
Pdfops.parse_operators pdf resources [content]
in
Pdfops.stream_of_ops ops
in
let page =
{Pdfpage.content = [content];
Pdfpage.mediabox = Pdf.Null;
Pdfpage.resources = resources;
Pdfpage.rotate = Pdfpage.Rotate0;
Pdfpage.rest = Pdf.Dictionary []}
in
2016-11-24 20:10:53 +01:00
let newpdf = Pdfpage.change_pages false pdf [page] in
2013-08-20 16:32:57 +02:00
image_resolution newpdf [pagenum] dpi
| (pagenum, name, Image (w, h)) ->
let lx = Pdfunits.convert 0. Pdfunits.PdfPoint Pdfunits.Inch (distance_between o x)
and ly = Pdfunits.convert 0. Pdfunits.PdfPoint Pdfunits.Inch (distance_between o y) in
let wdpi = float w /. lx
and hdpi = float h /. ly in
if wdpi < dpi || hdpi < dpi then
Printf.printf "%i, %s, %i, %i, %f, %f\n" pagenum xobject w h wdpi hdpi
(*i else
Printf.printf "S %i, %s, %i, %i, %f, %f\n" pagenum xobject (int_of_float w) (int_of_float h) wdpi hdpi i*)
end
| Pdfops.Op_q ->
begin match !transform with
| [] -> raise (Failure "Unbalanced q/Q ops")
| h::t ->
let h' = ref Pdftransform.i_matrix in
h' := !h;
transform := h'::h::t
end
| Pdfops.Op_Q ->
begin match !transform with
| [] -> raise (Failure "Unbalanced q/Q ops")
| _ -> transform := tl !transform
end
| _ -> ())
pageops
with
e -> Printf.printf "Error %s\n" (Printexc.to_string e); flprint "\n"
and image_resolution pdf range dpi =
let images = ref [] in
Cpdf.iter_pages
(fun pagenum page ->
(* 1. Get all image names and their native resolutions from resources as string * int * int *)
match Pdf.lookup_direct pdf "/XObject" page.Pdfpage.resources with
| Some (Pdf.Dictionary xobjects) ->
iter
(function (name, xobject) ->
match Pdf.lookup_direct pdf "/Subtype" xobject with
| Some (Pdf.Name "/Image") ->
let width =
match Pdf.lookup_direct pdf "/Width" xobject with
| Some x -> Pdf.getnum x
| None -> 1.
and height =
match Pdf.lookup_direct pdf "/Height" xobject with
| Some x -> Pdf.getnum x
| None -> 1.
in
images := (pagenum, name, Image (int_of_float width, int_of_float height))::!images
| Some (Pdf.Name "/Form") ->
let resources =
match Pdf.lookup_direct pdf "/Resources" xobject with
| None -> page.Pdfpage.resources (* Inherit from page or form above. *)
| Some r -> r
and contents =
xobject
and matrix =
match Pdf.lookup_direct pdf "/Matrix" xobject with
| Some (Pdf.Array [a; b; c; d; e; f]) ->
{Pdftransform.a = Pdf.getnum a; Pdftransform.b = Pdf.getnum b; Pdftransform.c = Pdf.getnum c;
Pdftransform.d = Pdf.getnum d; Pdftransform.e = Pdf.getnum e; Pdftransform.f = Pdf.getnum f}
| _ -> Pdftransform.i_matrix
in
images := (pagenum, name, Form (matrix, contents, resources))::!images
| _ -> ()
)
xobjects
| _ -> ())
pdf
range;
(* Now, split into differing pages, and call [image_resolution_page] on each one *)
let pagesplits =
map
(function (a, _, _)::_ as ls -> (a, ls) | _ -> assert false)
(collate (fun (a, _, _) (b, _, _) -> compare a b) (rev !images))
and pages =
Pdfpage.pages_of_pagetree pdf
in
iter
(function (pagenum, images) ->
let page = select pagenum pages in
image_resolution_page pdf page pagenum dpi images)
pagesplits
(* Missing Fonts *)
let is_missing pdf dict =
match Pdf.lookup_direct pdf "/FontDescriptor" dict with
| None -> true
| Some d ->
match Pdf.lookup_direct pdf "/FontFile" d with
| Some _ -> false
| None ->
match Pdf.lookup_direct pdf "/FontFile2" d with
| Some _ -> false
| None ->
match Pdf.lookup_direct pdf "/FontFile3" d with
| Some _ -> false
| None -> true
let missing_font pdf page (name, dict) =
if is_missing pdf dict then
let subtype =
match Pdf.lookup_direct pdf "/Subtype" dict with
| Some (Pdf.Name n) -> n
| _ -> ""
and basefont =
match Pdf.lookup_direct pdf "/BaseFont" dict with
| Some (Pdf.Name n) -> n
| _ -> ""
and encoding =
match Pdf.lookup_direct pdf "/Encoding" dict with
| Some (Pdf.Name n) -> n
| _ -> ""
in
if Pdftext.standard_font_of_name basefont <> None then () else
Printf.printf "%i, %s, %s, %s, %s\n" page name subtype basefont encoding
let missing_fonts pdf range =
Cpdf.iter_pages
(fun num page ->
match Pdf.lookup_direct pdf "/Font" page.Pdfpage.resources with
| Some (Pdf.Dictionary fontdict) ->
(* Extract descendant fonts *)
let name_dict_pairs =
flatten
(map
(fun (name, dict) ->
match Pdf.lookup_direct pdf "/DescendantFonts" dict with
| Some (Pdf.Array desc_fonts) -> map (fun d -> name, d) desc_fonts
| _ -> [(name, dict)])
fontdict)
in
iter (missing_font pdf num) name_dict_pairs
| _ -> ())
pdf
range
let lines_of_channel c =
let ls = ref [] in
try
while true do ls =| input_line c done; []
with
End_of_file -> rev !ls
(* parse the results of the standard error part of gs -sDEVICE=bbox *)
let parse_whiteboxes filename =
try
let fh = open_in filename in
let lines = lines_of_channel fh in
let lines =
keep
(function l ->
match explode l with
| '%'::'%'::_ -> true | _ -> false)
lines
in
let hires_lines = drop_odds lines in
let result =
map
(fun line ->
let line = implode (drop (explode line) 20) in
match Pdfgenlex.lex_string line with
| [a; b; c; d] ->
let getfloat = function
| Pdfgenlex.LexInt i -> float i
| Pdfgenlex.LexReal f -> f
| _ -> raise (Failure "parse_whiteboxes")
in
getfloat a, getfloat b, getfloat c, getfloat d
| x -> raise (Failure ("bad lex on line " ^ line ^ "made tokens " ^ Pdfgenlex.string_of_tokens x)))
hires_lines
in
close_in fh;
result
with
e ->
Printf.eprintf "%s\n" ("parse_whiteboxes " ^ Printexc.to_string e);
raise (Failure "")
(* Make start, end pairs from a sortedrange *)
let rec startends_of_range_inner pairs ls =
match pairs, ls with
| _, [] -> rev pairs
| [], (h::t) ->
startends_of_range_inner [(h, h)] t
| ((s, e)::ps), (h::t) when h = e + 1 ->
startends_of_range_inner ((s, h)::ps) t
| ps, (h::t) ->
startends_of_range_inner ((h, h)::ps) t
let startends_of_range x =
startends_of_range_inner [] x
2013-08-20 16:32:57 +02:00
(* Calculating margins *)
let calculate_margins filename pdf (s, e) =
(* Call ghostscript *)
let gscall =
args.path_to_ghostscript ^
" -dSAFER -dNOPAUSE -dBATCH -sDEVICE=bbox -r1200"
^ " -dFirstPage="
^ string_of_int s
^ " -dLastPage=" ^ string_of_int e
^ " \"" ^ filename ^ "\"" ^ " > waste.txt 2> margins.txt"
in
match Sys.command gscall with
| 0 ->
(* Parse white boxes *)
let whiteboxes =
parse_whiteboxes "margins.txt"
(* Get media boxes *)
and mediaboxes =
take' (e - s + 1)
(drop' (s - 1)
(map
(function page ->
(* Prefer the crop box *)
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some pdfobject -> Pdf.parse_rectangle (Pdf.direct pdf pdfobject)
| None -> Pdf.parse_rectangle page.Pdfpage.mediabox)
(Pdfpage.pages_of_pagetree pdf)))
in
iter2
(fun (m_minx, m_miny, m_maxx, m_maxy) (w_minx, w_miny, w_maxx, w_maxy) ->
if w_minx = 0. && w_miny = 0. && w_maxx = 0. && w_maxy = 0. then
Printf.printf "100, 100, 100, 100\n"
else
let topmargin = ((m_maxy -. w_maxy) /. (m_maxy -. m_miny)) *. 100.
and bottommargin = ((w_miny -. m_miny) /. (m_maxy -. m_miny)) *. 100.
and leftmargin = ((w_minx -. m_minx) /. (m_maxx -. m_minx)) *. 100.
and rightmargin = ((m_maxx -. w_maxx) /. (m_maxx -. m_minx)) *. 100. in
Printf.printf "%f, %f, %f, %f\n" leftmargin rightmargin topmargin bottommargin)
mediaboxes
whiteboxes;
(* Clean up temp files *)
Sys.remove "margins.txt";
Sys.remove "waste.txt"
| _ -> Printf.eprintf "Call to ghostscript failed."
let calculate_margins filename pdf range =
iter (calculate_margins filename pdf) (startends_of_range (sort compare range))
(* copy the contents of the box f to the box t. If mediabox_if_missing is set,
the contents of the mediabox will be used if the from fox is not available. If
mediabox_is_missing is false, the page is unaltered. *)
let copy_box f t mediabox_if_missing pdf range =
Cpdf.process_pages
(fun _ page ->
if f = "/MediaBox" then
{page with Pdfpage.rest =
(Pdf.add_dict_entry page.Pdfpage.rest t (page.Pdfpage.mediabox))}
else
match Pdf.lookup_direct pdf f page.Pdfpage.rest with
| Some pdfobject ->
if t = "/MediaBox"
then {page with
Pdfpage.mediabox = Pdf.direct pdf pdfobject}
else {page with Pdfpage.rest =
(Pdf.add_dict_entry page.Pdfpage.rest t (Pdf.direct pdf pdfobject))}
| None ->
if mediabox_if_missing
then {page with Pdfpage.rest = Pdf.add_dict_entry page.Pdfpage.rest t page.Pdfpage.mediabox}
else page)
pdf
range
(* Remove Embedded fonts. This is done by removing the Font Descriptor. *)
let remove_fontdescriptor pdf = function
| Pdf.Dictionary d as font ->
begin match lookup "/Type" d with
| Some (Pdf.Name "/Font") ->
(match Pdf.lookup_direct pdf "/FontDescriptor" font with
| Some fontdes ->
let fontdescriptor' =
Pdf.remove_dict_entry
(Pdf.remove_dict_entry
(Pdf.remove_dict_entry fontdes "/FontFile")
"/FontFile2")
"/FontFile3"
in
Pdf.add_dict_entry font "/FontDescriptor" (Pdf.Indirect (Pdf.addobj pdf fontdescriptor'))
| _ -> font)
| _ -> font
end
| x -> x
let remove_fonts pdf =
Pdf.objiter (fun k v -> ignore (Pdf.addobj_given_num pdf (k, remove_fontdescriptor pdf v))) pdf;
pdf
let dump_attachment out pdf (_, embeddedfile) =
match Pdf.lookup_direct pdf "/F" embeddedfile with
| Some (Pdf.String s) ->
let efdata =
begin match Pdf.lookup_direct pdf "/EF" embeddedfile with
| Some d ->
let stream =
match Pdf.lookup_direct pdf "/F" d with
| Some s -> s
| None -> error "Bad embedded file stream"
in
Pdfcodec.decode_pdfstream_until_unknown pdf stream;
begin match stream with Pdf.Stream {contents = (_, Pdf.Got b)} -> b | _ -> error "Bad embedded file stream" end
| _ -> error "Bad embedded file stream"
end
in
2019-07-14 14:50:48 +02:00
let s = remove_unsafe_characters s in
let filename = if out = "" then s else out ^ Filename.dir_sep ^ s in
begin try
2013-08-20 16:32:57 +02:00
let fh = open_out_bin filename in
for x = 0 to bytes_size efdata - 1 do output_byte fh (bget efdata x) done;
close_out fh
2019-07-14 14:50:48 +02:00
with
e -> Printf.eprintf "Failed to write attachment to %s\n" filename;
end
2013-08-20 16:32:57 +02:00
| _ -> ()
let dump_attached_document pdf out =
let root = Pdf.lookup_obj pdf pdf.Pdf.root in
let names =
match Pdf.lookup_direct pdf "/Names" root with Some n -> n | _ -> Pdf.Dictionary []
in
match Pdf.lookup_direct pdf "/EmbeddedFiles" names with
| Some x ->
iter (dump_attachment out pdf) (Pdf.contents_of_nametree pdf x)
| None -> ()
let dump_attached_page pdf out page =
let annots =
match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with
| Some (Pdf.Array l) -> l
| _ -> []
in
let efannots =
keep
(fun annot ->
match Pdf.lookup_direct pdf "/Subtype" annot with
| Some (Pdf.Name "/FileAttachment") -> true
| _ -> false)
annots
in
let fsannots = option_map (Pdf.lookup_direct pdf "/FS") efannots in
iter (dump_attachment out pdf) (map (fun x -> 0, x) fsannots)
(* Dump both document-level and page-level attached files to file, using their file names *)
let dump_attached_files pdf out =
try
dump_attached_document pdf out;
iter (dump_attached_page pdf out) (Pdfpage.pages_of_pagetree pdf)
with
2019-07-14 14:50:48 +02:00
e -> error (Printf.sprintf "Couldn't dump attached files: %s\n" (Printexc.to_string e))
2013-08-20 16:32:57 +02:00
(* If pages in stamp < pages in main, extend stamp by repeating its last page. If pages in stamp more, chop stamp *)
let equalize_pages_extend main stamp =
let length_stamp = Pdfpage.endpage stamp
in let length_main = Pdfpage.endpage main
in let extend_lastpage lastpage page len =
Pdfpage.change_pages true page (Pdfpage.pages_of_pagetree page @ (many lastpage len))
in let chop pdf n =
Pdfpage.change_pages true pdf (take (Pdfpage.pages_of_pagetree pdf) n)
in
if length_stamp > length_main
then chop stamp length_main
else extend_lastpage (last (Pdfpage.pages_of_pagetree stamp)) stamp (length_main - length_stamp)
let remove_unused_resources_page pdf n page =
let xobjects, all_names =
match Pdf.lookup_direct pdf "/XObject" page.Pdfpage.resources with
| Some (Pdf.Dictionary d) -> Pdf.Dictionary d, map fst d
| _ -> Pdf.Dictionary [], []
in
let names_to_keep =
option_map
(function Pdfops.Op_Do n -> Some n | _ -> None)
(Pdfops.parse_operators pdf page.Pdfpage.resources page.Pdfpage.content)
in
let names_to_remove = lose (mem' names_to_keep) all_names in
let xobjdict = fold_left (Pdf.remove_dict_entry) xobjects names_to_remove in
{page with Pdfpage.resources = Pdf.add_dict_entry page.Pdfpage.resources "/XObject" xobjdict}
let remove_unused_resources pdf =
Cpdf.process_pages (remove_unused_resources_page pdf) pdf (ilist 1 (Pdfpage.endpage pdf))
(* Extracts font to font.dat in CWD. *)
let extract_fontfile pagenumber fontname pdf =
let resources = (select pagenumber (Pdfpage.pages_of_pagetree pdf)).Pdfpage.resources in
match Pdf.lookup_direct pdf "/Font" resources with
| None -> failwith "extract_fontfile: font not found"
| Some fonts ->
let fontobj = Pdf.lookup_fail "no /Fonts" pdf fontname fonts in
let font = Pdftext.read_font pdf fontobj in
match font with
| Pdftext.CIDKeyedFont (_, {Pdftext.cid_fontdescriptor = {Pdftext.fontfile = Some fontfile}}, _)
| Pdftext.SimpleFont {Pdftext.fontdescriptor = Some {Pdftext.fontfile = Some fontfile}} ->
begin let objnum =
match fontfile with
| Pdftext.FontFile i -> i
| Pdftext.FontFile2 i -> i
| Pdftext.FontFile3 i -> i
in
match Pdf.lookup_obj pdf objnum with
| Pdf.Stream s as obj ->
Pdfcodec.decode_pdfstream pdf obj;
begin match s with
| {contents = (_, Pdf.Got bytes)} ->
let fh = open_out_bin "font.dat" in
for x = 0 to bytes_size bytes - 1 do output_byte fh (bget bytes x) done;
close_out fh;
(* Now try to read using Pdfcff module *)
(*let font = Pdftruetype.to_type3 pdf font in*)
(*let extractor = Pdftext.text_extractor_of_font pdf fontobj in*)
(*flprint "glyph names for incodes 0,1,2,3...";
iter print_string (Pdftext.glyphnames_of_text extractor "\000\001\002\003\004\005\006\007");
flprint "\n";*)
()
| _ -> failwith "extract_fontfile"
end
| _ -> failwith "extract_fontfile"
end
| _ -> failwith "unsupported or unfound font"
2016-11-08 19:15:04 +01:00
let addrectangle
fast (w, h) colour outline linewidth opacity position relative_to_cropbox
underneath range pdf
=
2013-08-20 16:32:57 +02:00
let addrectangle_page _ page =
2016-11-08 19:15:04 +01:00
let resources', unique_extgstatename =
if opacity < 1.0 then
let dict =
match Pdf.lookup_direct pdf "/ExtGState" page.Pdfpage.resources with
| Some d -> d
| None -> Pdf.Dictionary []
in
let unique_extgstatename = Pdf.unique_key "gs" dict in
let dict' =
Pdf.add_dict_entry dict unique_extgstatename
(Pdf.Dictionary [("/ca", Pdf.Real opacity); ("/CA", Pdf.Real opacity)])
in
Pdf.add_dict_entry page.Pdfpage.resources "/ExtGState" dict', Some unique_extgstatename
else
page.Pdfpage.resources, None
in
let mediabox =
if relative_to_cropbox then
match Pdf.lookup_direct pdf "/CropBox" page.Pdfpage.rest with
| Some pdfobject -> Pdf.parse_rectangle (Pdf.direct pdf pdfobject)
| None -> Pdf.parse_rectangle page.Pdfpage.mediabox
else
Pdf.parse_rectangle page.Pdfpage.mediabox
in
let x, y, _ =
Cpdf.calculate_position false w mediabox Cpdf.Horizontal position
in
2016-11-13 17:22:21 +01:00
let x, y =
match position with
Cpdf.Top _ | Cpdf.TopLeft _ | Cpdf.TopRight _ -> (x, y -. h)
2016-11-13 17:36:37 +01:00
| Cpdf.Centre | Cpdf.PosCentre _ -> (x, y -. (h /. 2.))
2016-11-13 17:22:21 +01:00
| _ -> (x, y)
in
2016-11-08 19:15:04 +01:00
let ops =
[
Pdfops.Op_q;
Pdfops.Op_BMC "/CPDFSTAMP";
(match colour with (r, g, b) -> Pdfops.Op_rg (r, g, b));
(match colour with (r, g, b) -> Pdfops.Op_RG (r, g, b))
]
@
(if outline then [Pdfops.Op_w linewidth] else [])
@
(match unique_extgstatename with None -> [] | Some n -> [Pdfops.Op_gs n])
@
[
Pdfops.Op_re (x, y, w, h);
(if outline then Pdfops.Op_s else Pdfops.Op_f);
Pdfops.Op_EMC;
Pdfops.Op_Q
]
in
let page = {page with Pdfpage.resources = resources'} in
if underneath
then Pdfpage.prepend_operators pdf ops ~fast:fast page
else Pdfpage.postpend_operators pdf ops ~fast:fast page
2013-08-20 16:32:57 +02:00
in
Cpdf.process_pages addrectangle_page pdf range
2016-11-03 18:11:08 +01:00
let print_spot_colour n s =
Printf.printf "%i %s\n" n s
2016-11-04 13:43:58 +01:00
let list_spot_colours pdf =
Pdf.objiter
(fun _ obj ->
match obj with
Pdf.Array (Pdf.Name "/Separation"::x::_) ->
begin match Pdf.direct pdf x with
Pdf.Name col -> Printf.printf "%s\n" col
| _ -> ()
end
| _ -> ())
pdf
2016-11-03 18:11:08 +01:00
2016-11-09 19:15:23 +01:00
let remove_clipping_ops pdf resources content =
let ops = Pdfops.parse_operators pdf resources content in
let rec process a = function
Pdfops.Op_W::Pdfops.Op_n::t -> process (Pdfops.Op_n::a) t
| h::t -> process (h::a) t
| [] -> rev a
in
[Pdfops.stream_of_ops (process [] ops)]
2016-11-09 16:42:47 +01:00
let remove_clipping pdf range =
let remove_clipping_page _ page =
let content' =
remove_clipping_ops pdf page.Pdfpage.resources page.Pdfpage.content
in
Cpdf.process_xobjects pdf page remove_clipping_ops;
{page with Pdfpage.content = content'}
in
Cpdf.process_pages remove_clipping_page pdf range
2019-07-03 15:40:32 +02:00
(* Indent bookmarks in each file by one and add a title bookmark pointing to the first page. *)
let add_bookmark_title filename use_title pdf =
let title =
if use_title then
match Cpdf.get_info_utf8 pdf "/Title", Cpdf.get_xmp_info pdf "/Title" with
"", x | x, "" | _, x -> x
else
Filename.basename filename
in
let marks = Pdfmarks.read_bookmarks pdf in
let page1objnum =
match Pdfpage.page_object_number pdf 1 with
None -> error "add_bookmark_title: page not found"
| Some x -> x
in
let newmarks =
{Pdfmarks.level = 0;
Pdfmarks.text = title;
Pdfmarks.target = Pdfdest.XYZ (Pdfdest.PageObject page1objnum, None, None, None);
Pdfmarks.isopen = false}
::map (function m -> {m with Pdfmarks.level = m.Pdfmarks.level + 1}) marks
in
Pdfmarks.add_bookmarks newmarks pdf
2019-07-06 18:55:26 +02:00
let bookmarks_open_to_level n pdf =
let marks = Pdfmarks.read_bookmarks pdf in
let newmarks =
2020-03-04 19:50:32 +01:00
map
(fun m -> {m with Pdfmarks.isopen = m.Pdfmarks.level < n})
2019-07-06 18:55:26 +02:00
marks
in
Pdfmarks.add_bookmarks newmarks pdf
let create_pdf pages pagesize =
let page =
{(Pdfpage.blankpage args.createpdf_pagesize) with
Pdfpage.content = [Pdfops.stream_of_ops []];
Pdfpage.resources = Pdf.Dictionary []}
in
let pdf, pageroot = Pdfpage.add_pagetree (many page args.createpdf_pages) (Pdf.empty ()) in
Pdfpage.add_root pageroot [] pdf
2019-07-11 18:19:40 +02:00
let rec remove_all_text_ops pdf resources content =
let is_textop = function
Pdfops.Op_Tj _ | Pdfops.Op_' _ | Pdfops.Op_'' _ | Pdfops.Op_TJ _ -> true
| _ -> false
in
let content' =
let ops = Pdfops.parse_operators pdf resources content in
Pdfops.stream_of_ops
(option_map (function x -> if is_textop x then None else Some x) ops)
in
[content']
let remove_all_text_page pdf p =
let resources = p.Pdfpage.resources in
let content = p.Pdfpage.content in
Cpdf.process_xobjects pdf p remove_all_text_ops;
2019-07-19 18:04:30 +02:00
{p with Pdfpage.content = remove_all_text_ops pdf resources content}, pdf
2019-07-11 18:19:40 +02:00
let remove_all_text range pdf =
let pages = Pdfpage.pages_of_pagetree pdf in
let pagenums = indx pages in
let pdf = ref pdf in
let pages' = ref [] in
iter2
(fun p pagenum ->
let p', pdf' =
if mem pagenum range
then remove_all_text_page !pdf p
else p, !pdf
in
pdf := pdf';
pages' =| p')
pages
pagenums;
Pdfpage.change_pages true !pdf (rev !pages')
2019-07-15 12:52:14 +02:00
(* Add rectangles on top of pages to show Media, Crop, Art, Trim, Bleed boxes.
*
* We use different dash lengths and colours to help distinguish coincident
* boxes The sequence of operators is postpended to the page content,
* appropriately protected to prevent pollution of matrices.
*
* /MediaBox: Solid red line
* /CropBox: Dashed 7 on 7 off green line
* /ArtBox: Dashed 5 on 5 off blue line
* /TrimBox: Dashed 3 on 3 off orange line
* /BleedBox: Dashed 2 on 2 off pink line *)
let get_rectangle pdf page box =
if box = "/MediaBox" then
match page.Pdfpage.mediabox with
Pdf.Array [a; b; c; d] as r -> Some (Pdf.parse_rectangle r)
| _ -> None
else
match Pdf.lookup_direct pdf box page.Pdfpage.rest with
Some (Pdf.Array [a; b; c; d] as r) -> Some (Pdf.parse_rectangle r)
| _ -> None
let show_boxes_page pdf _ page =
let make_ops (r, g, b) on off boxname =
match get_rectangle pdf page boxname with
Some (r1, r2, r3, r4) ->
[Pdfops.Op_q;
Pdfops.Op_RG (r /. 255., g /. 255., b /. 255.);
Pdfops.Op_w 1.;
Pdfops.Op_d ((if on = 0. && off = 0. then [] else [on; off]), 0.);
Pdfops.Op_re (r1, r2, r3 -. r1, r4 -. r2);
Pdfops.Op_S;
Pdfops.Op_Q]
| None -> []
in
let ops =
make_ops (255., 0., 0.) 0. 0. "/MediaBox"
@ make_ops (0., 255., 0.) 7. 7. "/CropBox"
@ make_ops (0., 0., 255.) 5. 5. "/ArtBox"
@ make_ops (255.,150.,0.) 3. 3. "/TrimBox"
@ make_ops (255.,9.,147.) 2. 2. "/BleedBox"
in
Pdfpage.postpend_operators pdf ops ~fast:args.fast page
let show_boxes range pdf =
Cpdf.process_pages (show_boxes_page pdf) pdf range
2019-07-15 14:42:32 +02:00
let allowance = 9.
let line (x0, y0, x1, y1) =
[Pdfops.Op_m (x0, y0);
Pdfops.Op_l (x1, y1);
Pdfops.Op_s]
let trim_marks_page pdf n page =
match get_rectangle pdf page "/TrimBox", get_rectangle pdf page "/MediaBox" with
| Some (tminx, tminy, tmaxx, tmaxy), Some (minx, miny, maxx, maxy) ->
let ops =
[Pdfops.Op_q;
Pdfops.Op_K (1., 1., 1., 1.);
Pdfops.Op_w 1.]
@ line (minx, tmaxy, tminy -. allowance, tmaxy) (* top left *)
@ line (tminx, tmaxy +. allowance, tminx, maxy)
@ line (tmaxx +. allowance, tmaxy, maxx, tmaxy) (* top right *)
@ line (tmaxx, tmaxy +. allowance, tmaxx, maxy)
@ line (tmaxx +. allowance, tminy, maxx, tminy) (* bottom right *)
@ line (tmaxx, tminy -. allowance, tmaxx, miny)
@ line (tminx -. allowance, tminy, minx, tminy) (* bottom left *)
@ line (tminx, tminy -. allowance, tminx, miny)
@ [Pdfops.Op_Q]
in
Pdfpage.postpend_operators pdf ops ~fast:args.fast page
2019-08-01 15:10:02 +02:00
| _, _ -> Printf.eprintf "warning: no /TrimBox found on page %i\n" n; page
2019-07-15 14:42:32 +02:00
let trim_marks range pdf =
Cpdf.process_pages (trim_marks_page pdf) pdf range
2020-01-30 11:20:33 +01:00
let write_json output pdf =
match output with
| NoOutputSpecified ->
error "-output-json: no output name specified"
| Stdout ->
2020-02-01 11:18:15 +01:00
CpdfwriteJSON.write stdout args.jsonparsecontentstreams args.jsonnostreamdata pdf
2020-01-30 11:20:33 +01:00
| File filename ->
let f = open_out_bin filename in
2020-02-01 11:18:15 +01:00
CpdfwriteJSON.write f args.jsonparsecontentstreams args.jsonnostreamdata pdf;
2020-01-30 11:20:33 +01:00
close_out f
2014-10-14 20:45:10 +02:00
(* Main function *)
2013-08-20 16:32:57 +02:00
let go () =
match args.op with
| Some Version ->
flprint
("cpdf Version " ^ string_of_int major_version ^ "." ^ string_of_int minor_version ^ " " ^ version_date ^ "\n")
| None | Some Merge ->
begin match args.out, args.inputs with
| _, (_::_ as inputs) ->
2014-10-28 19:40:56 +01:00
let op = match inputs with [_] -> None | _ -> Some Merge in
2013-08-20 16:32:57 +02:00
let write_pdf x pdf =
match args.keep_this_id with
| None -> write_pdf x pdf
| Some s ->
(* get the ID from the file with name 's', and copy to pdf *)
let namewiths =
keep (function (InFile s', _, _, _, _, _) when s' = s -> true | _ -> false) inputs
2013-08-20 16:32:57 +02:00
in
match namewiths with
| (namewiths, _, _, _, _, _) as input::t ->
2014-10-28 19:40:56 +01:00
let spdf = get_pdf_from_input_kind input op namewiths in
2013-08-20 16:32:57 +02:00
write_pdf x (Cpdf.copy_id true spdf pdf)
| _ -> write_pdf x pdf
in
let names, ranges, rotations, _, _, _ = split6 inputs in
2014-10-28 19:40:56 +01:00
let pdfs = map2 (fun i -> get_pdf_from_input_kind i op) inputs names in
2013-08-20 16:32:57 +02:00
(* If at least one file had object streams and args.preserve_objstm is true, set -objstm-create *)
if args.preserve_objstm then
iter
(fun pdf ->
if Hashtbl.length pdf.Pdf.objects.Pdf.object_stream_ids > 0
then args.create_objstm <- true)
pdfs;
begin match pdfs with
| [pdf] ->
(* FIXME Here, if the output file is different from the
input file, and we're just extracting pages, might we use a
lazy read? *)
2014-11-23 21:05:03 +01:00
if hd ranges <> "all" || !Pdfpage.flat_pagetrees then
2014-10-14 20:36:57 +02:00
let range = parse_pagespec pdf (hd ranges) in
let newpdf = Pdfpage.pdf_of_pages ~retain_numbering:args.retain_numbering pdf range in
write_pdf false newpdf
2013-08-20 16:32:57 +02:00
else
write_pdf false pdf
| _ ->
(* We check permissions. A merge is allowed if each file
2014-11-05 15:05:20 +01:00
included was (a) not encrypted (detected by the absence of
saved encryption information in the PDF, or (b) decrypted using
the owner password (stored in the input) *)
if
2018-03-20 16:43:15 +01:00
(not args.debugforce) &&
(not
2014-11-05 15:05:20 +01:00
(fold_left ( && ) true
(map2
(fun (_, _, _, _, was_dec_with_owner, _) pdf ->
2014-11-05 15:05:20 +01:00
!was_dec_with_owner || pdf.Pdf.saved_encryption = None)
inputs
2018-03-20 16:43:15 +01:00
pdfs)))
2014-11-05 15:05:20 +01:00
then
soft_error "Merge requires the owner password for all encrypted files."
else
2019-07-03 15:40:32 +02:00
let pdfs =
if args.merge_add_bookmarks then
2020-03-04 19:50:32 +01:00
map2
2019-07-03 15:40:32 +02:00
(fun filename pdf -> add_bookmark_title filename args.merge_add_bookmarks_use_titles pdf)
2020-03-04 19:50:32 +01:00
(map (function InFile s -> s | StdIn -> "" | AlreadyInMemory _ -> "") names)
2019-07-03 15:40:32 +02:00
pdfs
else
pdfs
in
2014-11-05 15:05:20 +01:00
(* If args.keep_this_id is set, change the ID to the one from the kept one *)
let rangenums = map2 parse_pagespec pdfs ranges in
let outpdf =
2019-07-03 15:40:32 +02:00
Pdfmerge.merge_pdfs
args.retain_numbering args.remove_duplicate_fonts
2014-11-05 15:05:20 +01:00
(map string_of_input_kind names) pdfs rangenums
in
write_pdf false outpdf
2013-08-20 16:32:57 +02:00
end
| _ ->
match args.op with
| Some Merge ->
error "Merge: Must specify one output and at least one input"
| None ->
error "Must specify one output and at least one input"
| _ -> assert false
end
| Some RemoveUnusedResources ->
begin match args.inputs, args.out with
| _::_, _ ->
let pdf = get_single_pdf (Some RemoveUnusedResources) false in
let outpdf = remove_unused_resources pdf in
write_pdf true outpdf
| _ -> error "RemoveUnusedResources: bad command line"
end
| Some (CopyFont fromfile) ->
begin match args.inputs, args.out with
| (_, pagespec, u, o, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some (CopyFont fromfile)) false
and frompdf = pdfread_pdf_of_file (optstring u) (optstring o) fromfile in
let range = parse_pagespec pdf pagespec in
let copyfontname =
match args.copyfontname with
| Some x -> x
| None -> failwith "copy_font: no font name given"
in
let outpdf = copy_font frompdf copyfontname args.copyfontpage range pdf in
write_pdf true outpdf
| _ -> error "copyfont: bad command line"
end
| Some RemoveFonts ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some RemoveFonts) false in
write_pdf true (remove_fonts pdf)
| _ -> error "remove fonts: bad command line"
end
| Some ExtractFontFile ->
(*Graphics.open_graph " 1600x1050";*)
begin match args.inputs, args.out with
| (_, pagespec, u, o, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some ExtractFontFile) false in
let page = args.copyfontpage
and name =
match args.copyfontname with
| Some x -> x
| None -> failwith "extract fontfile: no font name given"
in
extract_fontfile page name pdf
| _ -> error "extract fontfile: bad command line"
end
| Some CountPages ->
2019-07-02 16:50:36 +02:00
begin match args.inputs with
[(ik, _, _, _, _, _) as input] ->
2019-07-02 17:50:39 +02:00
let pdf = get_pdf_from_input_kind ~read_lazy:true ~decrypt:false input (Some CountPages) ik in
2019-07-02 16:50:36 +02:00
output_page_count pdf
| _ -> raise (Arg.Bad "CountPages: must have a single input file only")
end
| Some Revisions ->
let input =
match args.inputs with
(InFile inname, _, _, _, _, _)::_ -> Pdfio.input_of_channel (open_in_bin inname)
| (StdIn, _, _, _, _, _)::_ -> Pdfio.input_of_channel stdin
| _ -> raise (Arg.Bad "Revisions: must be a filename or stdin")
in
begin try
Printf.printf "%i\n%!" (Pdfread.revisions input)
with
_ -> soft_error "Malformed XRef table. Cannot determine number of revisions."
end
2013-08-20 16:32:57 +02:00
| Some Clean ->
2019-10-21 12:28:40 +02:00
let pdf' = get_single_pdf (Some Clean) false in
write_pdf false pdf'
2013-08-20 16:32:57 +02:00
| Some Info ->
2019-10-26 15:10:03 +02:00
let pdf, inname, input =
match args.inputs with
| (InFile inname, _, u, o, _, _) as input::_ ->
pdfread_pdf_of_channel_lazy (optstring u) (optstring o) (open_in_bin inname), inname, input
| (StdIn, _, u, o, _, _) as input::_ -> pdf_of_stdin u o, "", input
| (AlreadyInMemory pdf, _, _, _, _, _) as input::_ -> pdf, "", input
| _ -> raise (Arg.Bad "cpdf: No input specified.\n")
in
Printf.printf "Encryption: %s\n" (getencryption pdf);
Printf.printf "Permissions: %s\n" (getpermissions pdf);
if inname <> "" then
Printf.printf "Linearized: %b\n" (Pdfread.is_linearized (Pdfio.input_of_channel (open_in_bin inname)));
let pdf = decrypt_if_necessary input (Some Info) pdf in
Cpdf.output_info args.encoding pdf;
Cpdf.output_xmp_info args.encoding pdf
2013-08-20 16:32:57 +02:00
| Some PageInfo ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf pagespec in
Cpdf.output_page_info (get_single_pdf (Some PageInfo) true) range
| _ -> error "list-bookmarks: bad command line"
end
2013-08-20 16:32:57 +02:00
| Some Metadata ->
Cpdf.print_metadata (get_single_pdf (Some Metadata) true)
| Some Fonts ->
Cpdf.print_fonts (get_single_pdf (Some Fonts) true)
| Some ListBookmarks ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf pagespec in
Cpdf.list_bookmarks args.encoding range pdf (Pdfio.output_of_channel stdout);
flush stdout
| _ -> error "list-bookmarks: bad command line"
end
| Some Crop ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Crop) false in
let xywhlist = Cpdfcoord.parse_rectangles pdf args.rectangle in
2013-08-20 16:32:57 +02:00
let range = parse_pagespec pdf pagespec in
2016-07-21 18:02:11 +02:00
let pdf = Cpdf.crop_pdf xywhlist pdf range in
2013-08-20 16:32:57 +02:00
write_pdf false pdf
| _ -> error "crop: bad command line"
end
| Some Art ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Art) false in
let xywhlist = Cpdfcoord.parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/ArtBox" xywhlist pdf range in
write_pdf false pdf
2019-08-11 20:39:12 +02:00
| _ -> error "art: bad command line"
end
| Some Bleed ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Bleed) false in
let xywhlist = Cpdfcoord.parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/BleedBox" xywhlist pdf range in
write_pdf false pdf
2019-08-11 20:39:12 +02:00
| _ -> error "bleed: bad command line"
end
| Some Trim ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some Trim) false in
let xywhlist = Cpdfcoord.parse_rectangles pdf args.rectangle in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.crop_pdf ~box:"/TrimBox" xywhlist pdf range in
write_pdf false pdf
2019-08-11 20:39:12 +02:00
| _ -> error "trim: bad command line"
end
2013-08-20 16:32:57 +02:00
| Some MediaBox ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some MediaBox) false in
let xywhlist = Cpdfcoord.parse_rectangles pdf args.rectangle in
2013-08-20 16:32:57 +02:00
let range = parse_pagespec pdf pagespec in
2016-07-21 18:02:11 +02:00
let pdf = Cpdf.set_mediabox xywhlist pdf range in
2013-08-20 16:32:57 +02:00
write_pdf false pdf
| _ -> error "set media box: bad command line"
end
2017-05-19 20:10:49 +02:00
| Some (HardBox box) ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some (HardBox box)) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.hard_box pdf range box args.mediabox_if_missing args.fast in
write_pdf false pdf
| _ -> error "hard box: bad command line"
end
2013-08-20 16:32:57 +02:00
| Some CopyBox ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some CopyBox) false in
let range = parse_pagespec pdf pagespec in
let f, t =
begin match args.frombox, args.tobox with
| Some f, Some t -> f, t
| _ -> error "Copy box: no tobox or no frombox specified"
end
in
let pdf = copy_box f t args.mediabox_if_missing pdf range in
write_pdf false pdf
| _ -> error "Copy Box: bad command line"
end
| Some Decompress ->
let pdf = get_single_pdf (Some Decompress) false in
2013-08-20 16:32:57 +02:00
Pdf.iter_stream
(function stream ->
try Pdfcodec.decode_pdfstream_until_unknown pdf stream with
e -> Printf.eprintf "Decode failure: %s. Carrying on...\n" (Printexc.to_string e); ())
pdf;
2014-10-02 15:19:05 +02:00
write_pdf ~is_decompress:true args.makenewid pdf
2013-08-20 16:32:57 +02:00
| Some Compress ->
let pdf = get_single_pdf (Some Compress) false in
if args.remove_duplicate_streams then
Pdfmerge.remove_duplicate_fonts pdf;
2013-08-20 16:32:57 +02:00
write_pdf false (Cpdf.recompress_pdf pdf)
| Some RemoveCrop ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some RemoveCrop) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_cropping_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some RemoveArt ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveArt) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_art_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some RemoveTrim ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveTrim) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_trim_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some RemoveBleed ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf (Some RemoveBleed) false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.remove_bleed_pdf pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
2013-08-20 16:32:57 +02:00
| Some CopyCropBoxToMediaBox ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf (Some CopyCropBoxToMediaBox) false in
let range = parse_pagespec pdf pagespec in
let pdf = copy_cropbox_to_mediabox pdf range in
write_pdf false pdf
| _ -> error "remove-crop: bad command line"
end
| Some (Rotate _) | Some (Rotateby _) ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf pagespec in
let rotate =
match args.op with
| Some (Rotate i) -> Cpdf.rotate_pdf i
| Some (Rotateby i) -> Cpdf.rotate_pdf_by i
| _ -> assert false
in
let pdf = rotate pdf range in
write_pdf false pdf
| _ -> error "rotate: bad command line"
end
| Some (RotateContents a) ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.rotate_contents ~fast:args.fast a pdf range in
write_pdf false pdf
| _ -> error "rotate-contents: bad command line"
end
| Some Upright ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf pagespec in
let pdf = Cpdf.upright ~fast:args.fast range pdf in
write_pdf false pdf
| _ -> error "rotate-contents: bad command line"
end
| Some ((VFlip | HFlip) as flip) ->
begin match args.inputs, args.out with
| (_, pagespec, _, _, _, _)::_, _ ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf pagespec in
let pdf =
if flip = VFlip
then Cpdf.vflip_pdf ~fast:args.fast pdf range
else Cpdf.hflip_pdf ~fast:args.fast pdf range
in
write_pdf false pdf
| _ -> error "flip: bad command line"
end
| Some ((SetAuthor _ | SetTitle _ | SetSubject _ | SetKeywords _
| SetCreate _ | SetModify _ | SetCreator _ | SetProducer _
| SetTrapped | SetUntrapped) as op) ->
let key, value, version =
let f s = if args.encoding <> Cpdf.Raw then Pdftext.pdfdocstring_of_utf8 s else unescape_octals s in
2013-08-20 16:32:57 +02:00
match op with
| SetAuthor s -> "/Author", Pdf.String (f s), 0
| SetTitle s -> "/Title", Pdf.String (f s), 1
| SetSubject s -> "/Subject", Pdf.String (f s), 1
| SetKeywords s -> "/Keywords", Pdf.String (f s), 1
| SetCreate s -> "/CreationDate", Pdf.String (Cpdf.expand_date s), 0
| SetModify s -> "/ModDate", Pdf.String (Cpdf.expand_date s), 0
| SetCreator s -> "/Creator", Pdf.String (f s), 0
| SetProducer s -> "/Producer", Pdf.String (f s), 0
| SetTrapped -> "/Trapped", Pdf.Boolean true, 3
| SetUntrapped -> "/Trapped", Pdf.Boolean false, 3
| _ -> assert false
in
let pdf = get_single_pdf args.op false in
let version = if args.keepversion then pdf.Pdf.minor else version in
2019-06-28 16:01:28 +02:00
write_pdf false
(Cpdf.set_pdf_info
~xmp_also:args.alsosetxml
~xmp_just_set:args.justsetxml
(key, value, version) pdf)
| Some (SetMetadataDate date) ->
2019-07-01 15:40:22 +02:00
write_pdf false (Cpdf.set_metadata_date (get_single_pdf args.op false) date)
2013-08-20 16:32:57 +02:00
| Some ((HideToolbar _ | HideMenubar _ | HideWindowUI _
| FitWindow _ | CenterWindow _ | DisplayDocTitle _) as op) ->
begin match args.out with
| _ ->
let key, value, version =
match op with
| HideToolbar s -> "/HideToolbar", Pdf.Boolean s, 0
| HideMenubar s -> "/HideMenubar", Pdf.Boolean s, 0
| HideWindowUI s -> "/HideWindowUI", Pdf.Boolean s, 0
| FitWindow s -> "/FitWindow", Pdf.Boolean s, 0
| CenterWindow s -> "/CenterWindow", Pdf.Boolean s, 0
| DisplayDocTitle s -> "/DisplayDocTitle", Pdf.Boolean s, 4
| _ -> assert false
in
let pdf = get_single_pdf args.op false in
let version = if args.keepversion then pdf.Pdf.minor else version in
write_pdf false (Cpdf.set_viewer_preference (key, value, version) pdf)
end
| Some (OpenAtPage str) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf str in
let n = match range with [x] -> x | _ -> error "open_at_page: range does not specify single page" in
write_pdf false (Cpdf.set_open_action pdf false n)
| Some (OpenAtPageFit str) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf str in
let n = match range with [x] -> x | _ -> error "open_at_page: range does not specify single page" in
write_pdf false (Cpdf.set_open_action pdf true n)
2013-08-20 16:32:57 +02:00
| Some (SetMetadata metadata_file) ->
write_pdf false (Cpdf.set_metadata args.keepversion metadata_file (get_single_pdf args.op false))
| Some (SetVersion v) ->
let pdf = get_single_pdf args.op false in
2019-08-01 15:34:45 +02:00
let pdf =
if v >= 10
then {pdf with Pdf.major = 2; Pdf.minor = v - 10}
else {pdf with Pdf.major = 1; Pdf.minor = v}
in
write_pdf false pdf
2013-08-20 16:32:57 +02:00
| Some (SetPageLayout s) ->
write_pdf false (Cpdf.set_page_layout (get_single_pdf args.op false) s)
2013-08-20 16:32:57 +02:00
| Some (SetPageMode s) ->
write_pdf false (Cpdf.set_page_mode (get_single_pdf args.op false) s)
2013-08-20 16:32:57 +02:00
| Some Split ->
begin match args.inputs, args.out with
| [(f, ranges, _, _, _, _)], File output_spec ->
let pdf = get_single_pdf args.op true in
2013-08-20 16:32:57 +02:00
let enc =
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
2014-11-03 20:19:12 +01:00
args.create_objstm <- args.preserve_objstm;
split_pdf
2014-10-14 20:36:57 +02:00
enc args.original_filename args.chunksize args.linearize args.cpdflin
args.preserve_objstm args.preserve_objstm (*yes--always create if preserving *)
args.squeeze nobble output_spec pdf
2013-08-20 16:32:57 +02:00
| _, Stdout -> error "Can't split to standard output"
| _, NoOutputSpecified -> error "Split: No output format specified"
| _ -> error "Split: bad parameters"
end
| Some (SplitOnBookmarks level) ->
begin match args.out with
| File output_spec ->
let pdf = get_single_pdf args.op false in
let enc =
match args.crypt_method with
| "" -> None
| _ ->
Some
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
2014-11-03 20:19:12 +01:00
args.create_objstm <- args.preserve_objstm;
split_at_bookmarks
2014-11-03 20:19:12 +01:00
enc args.original_filename args.squeeze nobble level output_spec pdf
| Stdout -> error "Can't split to standard output"
| NoOutputSpecified -> error "Split: No output format specified"
end
2013-08-20 16:32:57 +02:00
| Some Presentation ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let pdf' =
Cpdf.presentation
range
args.transition args.duration args.horizontal
args.inward args.direction args.effect_duration pdf
in
pdf.Pdf.minor <- if args.keepversion then pdf.Pdf.minor else max pdf.Pdf.minor 1;
write_pdf false pdf'
| Some ChangeId ->
2014-11-18 15:57:18 +01:00
if args.recrypt then
soft_error "Cannot recrypt with change id: an id is part of encryption information";
2013-08-20 16:32:57 +02:00
begin match args.inputs, args.out with
| [(k, _, _, _, _, _) as input], File s ->
2013-08-20 16:32:57 +02:00
let pdf = get_pdf_from_input_kind input args.op k in
2014-10-02 15:19:05 +02:00
write_pdf true pdf
| [(k, _, _, _, _, _) as input], Stdout ->
2013-08-20 16:32:57 +02:00
let pdf = get_pdf_from_input_kind input args.op k in
2014-10-02 15:19:05 +02:00
write_pdf true pdf
2013-08-20 16:32:57 +02:00
| _ -> error "ChangeId: exactly one input file and output file required."
end
| Some RemoveId ->
2014-11-18 15:57:18 +01:00
if args.recrypt then
soft_error "Cannot recrypt with remove id: an id is part of encryption information";
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op false in
pdf.Pdf.trailerdict <- Pdf.remove_dict_entry pdf.Pdf.trailerdict "/ID";
write_pdf false pdf
| Some (CopyId getfrom) ->
2014-11-18 15:57:18 +01:00
if args.recrypt then
2014-11-18 16:16:11 +01:00
soft_error "Cannot recrypt with copy id: an id is part of encryption information";
2013-08-20 16:32:57 +02:00
begin match args.inputs with
| [(k, _, u, o, _, _) as input] ->
2013-08-20 16:32:57 +02:00
let pdf =
Cpdf.copy_id
args.keepversion
(pdfread_pdf_of_file (optstring u) (optstring o) getfrom)
(get_pdf_from_input_kind input args.op k)
in
write_pdf false pdf
| _ -> error "copy-id: No input file specified"
end
| Some (ThinLines w) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (Cpdf.thinlines range w pdf)
| Some BlackText ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2016-11-09 19:15:23 +01:00
write_pdf false (Cpdf.blacktext args.color range pdf)
2013-08-20 16:32:57 +02:00
| Some BlackLines ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2016-11-09 19:15:23 +01:00
write_pdf false (Cpdf.blacklines args.color range pdf)
2013-08-20 16:32:57 +02:00
| Some BlackFills ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2016-11-09 19:15:23 +01:00
write_pdf false (Cpdf.blackfills args.color range pdf)
2013-08-20 16:32:57 +02:00
| Some RemoveAnnotations ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (Cpdf.remove_annotations range pdf)
| Some (CopyAnnotations getfrom) ->
begin match args.inputs with
| [(k, _, u, o, _, _) as input] ->
2013-08-20 16:32:57 +02:00
let input_pdf = get_pdf_from_input_kind input args.op k in
let range = parse_pagespec input_pdf (get_pagespec ()) in
let pdf =
Cpdf.copy_annotations
range
(pdfread_pdf_of_file (optstring u) (optstring o) getfrom)
input_pdf
in
write_pdf false pdf
| _ -> error "copy-annotations: No input file specified"
end
| Some ListAnnotations ->
Cpdf.list_annotations args.encoding (get_single_pdf args.op true)
| Some ListAnnotationsMore ->
Cpdf.list_annotations_more (get_single_pdf args.op true)
| Some Shift ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let dxdylist = Cpdfcoord.parse_coordinates pdf args.coord in
2016-07-21 18:02:11 +02:00
write_pdf false (Cpdf.shift_pdf ~fast:args.fast dxdylist pdf range)
2013-08-20 16:32:57 +02:00
| Some Scale ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let sxsylist = Cpdfcoord.parse_coordinates pdf args.coord in
2016-07-21 18:02:11 +02:00
write_pdf false (Cpdf.scale_pdf ~fast:args.fast sxsylist pdf range)
2013-08-20 16:32:57 +02:00
| Some ScaleToFit ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let xylist = Cpdfcoord.parse_coordinates pdf args.coord
2013-08-20 16:32:57 +02:00
and scale = args.scale in
2019-03-19 15:03:28 +01:00
write_pdf false (Cpdf.scale_to_fit_pdf ~fast:args.fast args.position scale xylist args.op pdf range)
2013-08-20 16:32:57 +02:00
| Some (ScaleContents scale) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (Cpdf.scale_contents ~fast:args.fast args.position scale pdf range)
| Some ListAttachedFiles ->
let pdf = get_single_pdf args.op false in
2015-09-26 21:30:57 +02:00
let attachments = Cpdf.list_attached_files pdf in
iter
(fun a -> Printf.printf "%i %s\n" a.Cpdf.pagenumber a.Cpdf.name)
attachments;
flprint ""
2013-08-20 16:32:57 +02:00
| Some DumpAttachedFiles ->
let pdf = get_single_pdf args.op false in
begin match args.out with
| NoOutputSpecified -> dump_attached_files pdf ""
| File n -> dump_attached_files pdf n
| Stdout -> error "Can't dump attachments to stdout"
end
| Some RemoveAttachedFiles ->
write_pdf false (Cpdf.remove_attached_files (get_single_pdf args.op false))
| Some (AttachFile files) ->
begin match args.inputs with
| [(k, _, _, _, _, _) as input] ->
2013-08-20 16:32:57 +02:00
let pdf = get_pdf_from_input_kind input args.op k in
let topage =
try
match args.topage with
| None -> None
| Some "end" -> Some (Pdfpage.endpage pdf)
| Some s -> Some (int_of_string s)
with _ -> error "Bad -to-page"
in
let pdf = fold_left (Cpdf.attach_file args.keepversion topage) pdf (rev files) in
write_pdf false pdf
| _ -> error "attach file: No input file specified"
end
| Some PadBefore ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2017-12-18 20:44:02 +01:00
let padwith =
match args.padwith with
None -> None
| Some filename -> Some (pdfread_pdf_of_file None None filename)
in
write_pdf false (Cpdf.padbefore ?padwith range pdf)
2013-08-20 16:32:57 +02:00
| Some PadAfter ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2017-12-18 20:44:02 +01:00
let padwith =
match args.padwith with
None -> None
| Some filename -> Some (pdfread_pdf_of_file None None filename)
in
write_pdf false (Cpdf.padafter ?padwith range pdf)
2013-08-20 16:32:57 +02:00
| Some (PadEvery n) ->
let pdf = get_single_pdf args.op false in
let range =
match keep (function m -> m mod n = 0) (ilist 1 (Pdfpage.endpage pdf)) with
| [] -> []
| l -> if last l = Pdfpage.endpage pdf then all_but_last l else l
in
2017-12-21 15:54:01 +01:00
let padwith =
match args.padwith with
None -> None
| Some filename -> Some (pdfread_pdf_of_file None None filename)
in
write_pdf false (Cpdf.padafter ?padwith range pdf)
2013-08-20 16:32:57 +02:00
| Some (PadMultiple n) ->
let pdf = get_single_pdf args.op false in
write_pdf false (Cpdf.padmultiple n pdf)
2019-07-01 16:35:17 +02:00
| Some (PadMultipleBefore n) ->
let pdf = get_single_pdf args.op false in
write_pdf false (Cpdf.padmultiple (-n) pdf)
2013-08-20 16:32:57 +02:00
| Some Draft ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2019-07-09 17:31:45 +02:00
write_pdf false (Cpdf.draft args.removeonly args.boxes range pdf)
2013-08-20 16:32:57 +02:00
| Some (AddText text) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let font =
match args.font with
| StandardFont f -> Some f
| OtherFont f -> None (* it's in fontname *)
in
let pdf =
if args.prerotate then Cpdf.upright ~fast:args.fast range pdf else pdf
and filename =
match args.inputs with
| (InFile inname, _, _, _, _, _)::_ -> inname
2013-08-20 16:32:57 +02:00
| _ -> ""
in
write_pdf false
(Cpdf.addtexts
2015-04-13 15:42:18 +02:00
false args.linewidth args.outline args.fast args.fontname
2015-07-17 17:34:47 +02:00
font args.embedfonts args.bates args.batespad args.color args.position
2015-04-13 15:42:18 +02:00
args.linespacing args.fontsize args.underneath text range
args.orientation args.relative_to_cropbox args.opacity
2016-11-13 15:02:09 +01:00
args.justification args.midline args.topline filename
args.extract_text_font_size args.coord pdf)
2013-08-20 16:32:57 +02:00
| Some RemoveText ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (Cpdf.removetext range pdf)
| Some AddRectangle ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false
(addrectangle
args.fast (Cpdfcoord.parse_coordinate pdf args.coord)
2016-11-08 19:15:04 +01:00
args.color args.outline args.linewidth args.opacity args.position
args.relative_to_cropbox args.underneath range pdf)
2013-08-20 16:32:57 +02:00
| Some (AddBookmarks file) ->
write_pdf false
(Cpdf.add_bookmarks true (Pdfio.input_of_channel (open_in_bin file))
(get_single_pdf args.op false))
| Some RemoveBookmarks ->
write_pdf false (Pdfmarks.remove_bookmarks (get_single_pdf args.op false))
| Some TwoUp ->
write_pdf false (Cpdf.twoup args.fast (get_single_pdf args.op false))
2013-08-20 16:32:57 +02:00
| Some TwoUpStack ->
write_pdf false (Cpdf.twoup_stack args.fast (get_single_pdf args.op false))
2013-08-20 16:32:57 +02:00
| Some (StampOn over) ->
let overpdf =
match over with
| "stamp_use_stdin" -> pdf_of_stdin "" ""
| x -> pdfread_pdf_of_file None None x
in
2014-10-14 20:45:10 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let pdf =
2015-02-17 16:48:00 +01:00
Cpdf.stamp
args.relative_to_cropbox args.position args.topline args.midline args.fast
2015-02-17 16:48:00 +01:00
args.scale_stamp_to_fit true range overpdf pdf
2014-10-14 20:45:10 +02:00
in
write_pdf false pdf
2013-08-20 16:32:57 +02:00
| Some (StampUnder under) ->
let underpdf =
match under with
| "stamp_use_stdin" -> pdf_of_stdin "" ""
| x -> pdfread_pdf_of_file None None x
in
2014-10-14 20:45:10 +02:00
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let pdf =
2015-02-17 16:48:00 +01:00
Cpdf.stamp
args.relative_to_cropbox args.position args.topline args.midline args.fast
2015-02-17 16:48:00 +01:00
args.scale_stamp_to_fit false range underpdf pdf
2014-10-14 20:45:10 +02:00
in
write_pdf false pdf
2013-08-20 16:32:57 +02:00
| Some (CombinePages over) ->
write_pdf false
(Cpdf.combine_pages args.fast (get_single_pdf args.op false) (pdfread_pdf_of_file None None over) false false true)
| Some Encrypt ->
let pdf = get_single_pdf args.op false in
let pdf = Cpdf.recompress_pdf <| nobble pdf
and encryption =
{Pdfwrite.encryption_method =
(match args.crypt_method with
| "40bit" -> Pdfwrite.PDF40bit
| "128bit" -> Pdfwrite.PDF128bit
| "AES" -> Pdfwrite.AES128bit args.encrypt_metadata
| "AES256" -> Pdfwrite.AES256bit args.encrypt_metadata
| "AES256ISO" -> Pdfwrite.AES256bitISO args.encrypt_metadata
| _ -> assert false (* Pre-checked *));
Pdfwrite.owner_password = args.owner;
Pdfwrite.user_password = args.user;
Pdfwrite.permissions = banlist_of_args ()}
in
Pdf.remove_unreferenced pdf;
if not args.keepversion then
begin
let newversion =
match args.crypt_method with
"40bit" -> 1 | "128bit" -> 4 | "AES" -> 6 | "AES256" | "AES256ISO" -> 7 | _ -> 0
in
let newversion = if args.create_objstm then 5 else newversion in
pdf.Pdf.minor <- max pdf.Pdf.minor newversion
end;
2014-10-02 14:32:30 +02:00
write_pdf ~encryption:(Some encryption) args.makenewid pdf
2013-08-20 16:32:57 +02:00
| Some Decrypt ->
args.recrypt <- false;
2013-08-20 16:32:57 +02:00
write_pdf false (get_single_pdf args.op false)
| Some RemoveMetadata ->
write_pdf false (Cpdf.remove_metadata (get_single_pdf args.op false))
| Some ExtractImages ->
let output_spec =
begin match args.out with
| File output_spec -> output_spec
| _ -> ""
end
in
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf (get_pagespec ()) in
extract_images pdf range output_spec
| Some (ImageResolution f) ->
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf (get_pagespec ()) in
image_resolution pdf range f
| Some MissingFonts ->
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf (get_pagespec ()) in
missing_fonts pdf range
| Some CSP1 ->
write_pdf false (Cpdf.custom_csp1 (get_single_pdf (Some CSP1) false))
| Some (CSP2 f) ->
write_pdf false (Cpdf.custom_csp2 f (get_single_pdf (Some (CSP2 f)) false))
| Some CSP3 ->
begin match args.inputs with
| [(InFile s, _, _, _, _, _)] ->
2013-08-20 16:32:57 +02:00
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf (get_pagespec ()) in
calculate_margins s pdf range
| _ ->
Printf.eprintf "CSP3: Too many input files or input not a file"
end
| Some ExtractText ->
let pdf = get_single_pdf args.op true in
let range = parse_pagespec pdf (get_pagespec ()) in
2016-11-13 15:02:09 +01:00
let text = Cpdf.extract_text args.extract_text_font_size pdf range in
2013-08-20 16:32:57 +02:00
begin match args.out with
| File filename ->
let fh = open_out_bin filename in
output_string fh text;
close_out fh
| NoOutputSpecified | Stdout ->
print_string text;
print_newline ()
end
2013-10-24 16:21:54 +02:00
| Some AddPageLabels ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
2014-09-18 16:40:22 +02:00
Cpdf.add_page_labels
pdf args.labelsprogress args.labelstyle args.labelprefix args.labelstartval range;
2014-09-18 16:40:22 +02:00
write_pdf false pdf
| Some RemovePageLabels ->
let pdf = get_single_pdf args.op false in
Pdfpagelabels.remove pdf;
write_pdf false pdf
| Some PrintPageLabels ->
let pdf = get_single_pdf args.op true in
iter
print_string
(map Pdfpagelabels.string_of_pagelabel (Pdfpagelabels.read pdf))
2015-01-07 19:29:11 +01:00
| Some (RemoveDictEntry key) ->
let pdf = get_single_pdf args.op true in
(* 1. Process all objects *)
Pdf.objselfmap
(function
(Pdf.Dictionary _ as d) | (Pdf.Stream _ as d) ->
Pdf.remove_dict_entry d key
| x -> x)
pdf;
(* FIXME: We might like to do the trailer dictionary too *)
write_pdf false pdf
2016-11-03 18:11:08 +01:00
| Some ListSpotColours ->
2016-11-04 13:43:58 +01:00
let pdf = get_single_pdf args.op false in
list_spot_colours pdf
2016-11-09 16:42:47 +01:00
| Some RemoveClipping ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (remove_clipping pdf range)
2019-07-01 15:40:22 +02:00
| Some CreateMetadata ->
let pdf = get_single_pdf args.op false in
write_pdf false (Cpdf.create_metadata pdf)
2019-07-02 19:20:05 +02:00
| Some EmbedMissingFonts ->
let fi =
match args.inputs with
[(InFile fi, _, _, _, _, _)] -> fi
| _ -> error "Input method not supported for -embed-missing-fonts"
in
let fo =
match args.out with
File fo -> fo
| _ -> error "Output method not supported for -embed-missing-fonts"
in
embed_missing_fonts fi fo
2019-07-06 18:55:26 +02:00
| Some (BookmarksOpenToLevel n) ->
let pdf = get_single_pdf args.op false in
write_pdf false (bookmarks_open_to_level n pdf)
| Some CreatePDF ->
let pdf = create_pdf args.createpdf_pages args.createpdf_pagesize in
write_pdf false pdf
2019-07-11 18:19:40 +02:00
| Some RemoveAllText ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (remove_all_text range pdf)
2019-07-15 12:52:14 +02:00
| Some ShowBoxes ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (show_boxes range pdf)
2019-07-15 14:42:32 +02:00
| Some TrimMarks ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
write_pdf false (trim_marks range pdf)
| Some (Postpend s | Prepend s as x) ->
let pdf = get_single_pdf args.op false in
let range = parse_pagespec pdf (get_pagespec ()) in
let before = match x with Prepend _ -> true | _ -> false in
2019-10-02 14:41:56 +02:00
write_pdf false (Cpdf.append_page_content s before args.fast range pdf)
2020-01-30 11:20:33 +01:00
| Some OutputJSON ->
let pdf = get_single_pdf args.op false in
write_json args.out pdf
2020-02-26 17:24:27 +01:00
| Some OCGCoalesce ->
let pdf = get_single_pdf args.op false in
2020-02-27 15:14:51 +01:00
Cpdf.ocg_coalesce pdf;
write_pdf false pdf
2020-02-27 14:32:45 +01:00
| Some OCGList ->
let pdf = get_single_pdf args.op true in
2020-02-27 15:14:51 +01:00
Cpdf.ocg_list pdf
2020-02-27 14:32:45 +01:00
| Some OCGRename ->
let pdf = get_single_pdf args.op false in
2020-02-27 15:14:51 +01:00
Cpdf.ocg_rename args.ocgrenamefrom args.ocgrenameto pdf;
write_pdf false pdf
2020-03-02 13:37:39 +01:00
| Some OCGOrderAll ->
let pdf = get_single_pdf args.op false in
Cpdf.ocg_order_all pdf;
write_pdf false pdf
| Some (StampAsXObject stamp) ->
let stamp_pdf =
match stamp with
| "stamp_use_stdin" -> pdf_of_stdin "" ""
| x -> pdfread_pdf_of_file None None x
in
let pdf = get_single_pdf args.op false in
2020-03-19 17:23:16 +01:00
let range = parse_pagespec pdf (get_pagespec ()) in
let pdf, xobj_name =
2020-03-19 17:23:16 +01:00
Cpdf.stamp_as_xobject pdf range stamp_pdf
in
Printf.printf "%s\n" xobj_name;
flush stdout;
write_pdf false pdf
2013-08-20 16:32:57 +02:00
(* Advise the user if a combination of command line flags makes little sense,
or error out if it make no sense at all. *)
let check_command_line () =
if args.gs_malformed && !Pdfread.error_on_malformed then
error "Setting both -gs-malformed and -error-on-malformed makes no sense"
let parse_argv () s specs anon_fun usage_msg =
if args.debug then
Array.iter (Printf.eprintf "arg: %s\n") Sys.argv;
Arg.parse_argv ~current:(ref 0) s specs anon_fun usage_msg;
check_command_line ()
2013-08-20 16:32:57 +02:00
let align_specs s =
Arg.align s
2013-10-09 15:31:55 +02:00
(* The old -control mechanism clashed with AND, but must be retained for
backwards compatibility. There is a new mechanism -args file which performs
direct textual substitution of the file, before any expansion of ANDs *)
let rec expand_args_inner prev = function
[] -> rev prev
| "-args"::filename::r ->
expand_args_inner (rev (parse_control_file filename) @ prev) r
| h::t -> expand_args_inner (h::prev) t
let expand_args argv =
let l = Array.to_list argv in
Array.of_list (expand_args_inner [] l)
2019-07-08 15:44:34 +02:00
let gs_malformed_force fi fo =
if args.path_to_ghostscript = "" then begin
Printf.eprintf "Please supply path to gs with -gs\n";
exit 2
end;
let gscall =
args.path_to_ghostscript ^
2019-08-18 16:22:13 +02:00
" -dNOPAUSE " ^ (if args.gs_quiet then "-dQUIET" else "") ^ " -sDEVICE=pdfwrite -sOUTPUTFILE=" ^ Filename.quote fo ^
" -dBATCH " ^ Filename.quote fi
2019-07-08 15:44:34 +02:00
in
match Sys.command gscall with
| 0 -> exit 0
| _ -> Printf.eprintf "Failed to mend file.\n"; exit 2
(* FIXME: Now we call this repeatedly from interactive programs, careful to
ensure that all memory is cleaned. See clearance of filenames hashtable, for
example. *)
2013-08-20 16:32:57 +02:00
(* Main function. *)
let go_withargv argv =
2019-07-08 15:44:34 +02:00
(* Check for the standalone -gs-malformed-force special command line. This
* has exactly one file input and exactly one output and just -gs <gs>
* -gs-malformed-force between. *)
match argv with
[|_; inputfilename; "-gs"; gslocation; "-gs-malformed-force"; "-o"; outputfilename|] ->
args.path_to_ghostscript <- gslocation;
ignore (gs_malformed_force inputfilename outputfilename);
exit 0
2019-08-05 13:35:07 +02:00
| [|_; inputfilename; "-gs"; gslocation; "-gs-malformed-force"; "-o"; outputfilename; "-gs-quiet"|] ->
args.path_to_ghostscript <- gslocation;
args.gs_quiet <- true;
ignore (gs_malformed_force inputfilename outputfilename);
exit 0
2019-07-08 15:44:34 +02:00
| _ ->
Hashtbl.clear filenames;
2013-08-20 16:32:57 +02:00
if demo then
flprint "This demo is for evaluation only. http://www.coherentpdf.com/\n";
2013-08-29 13:10:51 +02:00
if noncomp then
begin
prerr_string "For non-commercial use only\n";
prerr_string "To purchase a license visit http://www.coherentpdf.com/\n\n";
flush stderr
end;
2013-08-20 16:32:57 +02:00
try
2013-10-09 15:31:55 +02:00
(* Pre-expand -args *)
let argv = expand_args argv in
2013-08-20 16:32:57 +02:00
(* Split the arguments into sets either side of ANDs *)
let sets =
let args =
(map (fun l -> "cpdf"::l) (split_around (eq "AND") (tl (Array.to_list argv))))
in
match args with
| [] -> []
| _ -> combine (map Array.of_list args) (map (eq (length args)) (ilist 1 (length args)))
in
iter
(fun (s, islast) ->
(*Printf.printf "AND:%b, %s\n" islast (Array.fold_left (fun x y -> x ^ " " ^ y) "" s);
flprint "\n";*)
reset_arguments ();
parse_argv () s (align_specs specs) anon_fun usage_msg;
parse_argv () (Array.of_list ("cpdf"::!control_args)) (align_specs specs) anon_fun usage_msg;
let addrange pdf = AlreadyInMemory pdf, args.dashrange, "", "", ref false, None in
2013-08-20 16:32:57 +02:00
args.inputs <- rev (map addrange !output_pdfs) @ rev args.inputs;
output_pdfs := [];
go ())
sets;
2019-07-02 15:10:42 +02:00
flush stdout; (*r for Windows *)
exit 0
2013-08-20 16:32:57 +02:00
with
| Arg.Bad s ->
2013-10-09 15:31:55 +02:00
prerr_string
(implode (takewhile (neq '\n') (explode s)) ^ " Use -help for help.\n\n");
flush stderr;
2015-01-12 18:55:45 +01:00
if not !stay_on_error then exit 2 else raise StayOnError
2013-08-20 16:32:57 +02:00
| Arg.Help _ ->
Arg.usage (align_specs specs) usage_msg;
flush stderr (*r for Windows *)
| Sys_error s as e ->
prerr_string (s ^ "\n\n");
flush stderr;
if not !stay_on_error then
2015-01-12 18:55:45 +01:00
(if args.debug then raise e else exit 2)
else raise StayOnError
2013-08-20 16:32:57 +02:00
| Pdf.PDFError s as e ->
prerr_string
("cpdf encountered an error. Technical details follow:\n\n" ^ s ^ "\n\n");
flush stderr;
if not !stay_on_error then
if args.debug then raise e else exit 2
2015-01-12 18:55:45 +01:00
else
raise StayOnError
| Cpdf.SoftError s -> soft_error s
| Cpdf.HardError s -> error s
2013-08-20 16:32:57 +02:00
| e ->
prerr_string
("cpdf encountered an unexpected error. Technical Details follow:\n" ^
Printexc.to_string e ^ "\n\n");
flush stderr;
2015-01-12 18:55:45 +01:00
if not !stay_on_error then
(if args.debug then raise e else exit 2) else raise StayOnError
2013-08-20 16:32:57 +02:00
let go () =
go_withargv Sys.argv
2013-08-20 16:32:57 +02:00