(* * WRAPPER_CANON.ML-- * * Copyright (C) 2009 Antoine Durand-Gasselin * Author: Antoine Durand-Gasselin * * License: GPLv3 * * Dependances: libperl4ocaml-dev (>= 0.9.5-2), libwww-mechanize-perl * Compilation: ocamlfind ocamlc -linkpkg -package unix,perl wrapper_canon.ml * *) (* #use "topfind";; #require "perl";; #require "unix";; *) let (%) a b = Printf.sprintf a b;; let (&) f x = f x open Perl open Unix open Pl_WWW_Mechanize let timestamp = (** on a besoin d'un timestamp *) "%d" % (int_of_float (Unix.time())) (** {1 : Fonctions de conversion des options} *) let filename = (** le nom du fichier pdf *) ref "" let copies = ref 1 let _Copies () = "%d" % !copies let papier = ref "a4" let _MediaSize () = match !papier with "a4" | "A4" -> "5" | "a3" | "A3" -> "6" | "A5" -> "16" | "B4" -> "12" | "B5" -> "13" | "LTR" -> "1" | "LGL" -> "2" | "11x17" -> "3" | "EXEC" -> "4" | "Com-10" -> "7" | "Monarch" -> "8" | "C5 Env" -> "9" | "B5 Env" -> "10" | "DL Env" -> "11" | _ -> raise (Arg.Bad ("%s wrong papersize" % !papier)) let papertype = ref "Ordinaire" let _MediaType () = List.assoc !papertype& List.map (fun (x,y) -> y,x) [ "0", "Ordinaire"; "2", "Recyclé"; "3", "Couleur"; "21", "Ordinaire (Epais)"; "22", "Ordinaire (Fin)"; "1", "Papier épais 1"; "16", "Papier épais 2"; "9", "Carte postale"; "10", "Carte postale"; "11", "Enveloppe"; "6", "Transparent"; "23", "Transparent"; "24", "Couché"; "5", "Calque"; "7", "Etiquettes"; "12", "Papier machine"; "14", "Pré-perforé" ] let expand = ref false let _FitSize () = if !expand then "1" else "0" let duplex = ref true let binding_long_edge = ref true let _DuplexType() = if !duplex then if !binding_long_edge then "2" else "1" else "0" let agrafage = ref true let piqure = ref false let _Sort() = if !piqure then "3" else if !agrafage then "1" (* "134" *) else "1" let agrafe = ref "NW" let _StapleType () = List.assoc !agrafe [ "NW","5"; "NE","6"; "SW","7"; "SE","8"; "N","1"; "S","2"; "W","3"; "E","4"] let couleur = ref false let _ColorMode () = if !couleur then "2" else "1" (** {1: Le formulaire} *) let fields () = [ "Url","http://"; "Mode","100"; "ManualNo","0"; "DocPasswd",""; "WebUserName",""; "WebPasswd",""; "PageMode","0"; "StartPage","1"; "EndPage","1"; "Copies", _Copies(); "MediaSize", _MediaSize(); "MediaType", _MediaType(); "ManualFeed", "0"; "FitSize", _FitSize(); "DuplexType", _DuplexType(); "Sort", _Sort(); "PunchPos","0"; "StapleType", _StapleType(); "BookType","2"; "Annotation","2"; "ColorMode", _ColorMode(); "C_Render","0"; "C_RGB_Pro","1"; "C_CMYK_Pro","4";"C_OUT_Pro","1"; "C_GRAY_Pro","1"; "C_Matching","0"; "SPOT_Color","1"; "C_Pure_B","1"; "C_B_OVPrn","1"; "C_Bright", "100"; "C_Gray_Com","1";"C_OVR_EFF","1"; "WidePrn","0"; "NupPrint","0"; "NupStart","0"; "Resolution","1"; "HalfToneTxt","1"; "HalfToneGrp","1"; "HalfToneImg","1"; "AstIntensity","2"; "AstText","0"; "AstGrap", "1"; "AstImag","1"; "StoreBox","0"; "BoxNo","00"; "RGBDLName",""; "CMYKDLName",""; "OUTDLName",""; "BOXName",""; "Flag","Exec_Data_Pdf"; "Dummy",timestamp; "Direct","100"; "File", !filename ] let set_positive r s = try let a = int_of_string s in assert (a>0); r := a with _ -> raise (Arg.Bad "copies doivent être un entier positif") let options = Arg.align [ "-#", Arg.String (set_positive copies), "N imprime N copies"; "-PageSize", Arg.Set_string papier, "FORMAT Format du papier"; "-MediaType", Arg.Set_string papertype, "TYPE Type du papier"; "-pdf-expand", Arg.Set expand, " Agrandir/Reduire en fonction du papier"; "-one-sided", Arg.Clear duplex, " Impression recto"; "-two-sided", Arg.Set duplex, " Impression recto/verso"; "-two-sided-short-edge", Arg.Clear binding_long_edge, " Reliure sur le bord court"; "-Staple", Arg.Set agrafage, " Agrafage du travail d'impression"; "-StapleLocation", Arg.Set_string agrafe, "AZ Agrafe au NW,NE,SE,SW,N,S,E,W"; "-CNSaddleStitch", Arg.Set piqure, " Mode livret"; "-Monochrom", Arg.Clear couleur, " Impression en noir et blanc"; "-Color", Arg.Set couleur, " Impression en couleurs"; ] let usage = "Usage: wrapper_canon [OPTIONS] FILE" let argv = ref 0 let () = Arg.parse options (fun s -> incr argv; filename := s) usage let () = if !argv <> 1 then raise (Arg.Bad "Wrong number of file") (** {1 : Initialisations} *) let printer = (** url de l'imprimante *) ref "https://imprimante.adm.crans.org/" let b = (** On initialise le "browser" *) let sv = Perl.call_class_method "WWW::Mechanize" "new" [] in let browser = new Pl_WWW_Mechanize.www_mechanize sv in (* On a besoin d'un user_agent connu *) browser#agent_alias (List.nth browser#known_agent_aliases 1); (* On récupère un cookie, parce que les cookies c'est bon *) browser#get !printer; browser#get (!printer ^ "ppdf.cgi?Type=PDF&Dummy=%s" % timestamp); browser (* On balance la sauce *) let resp = b#submit_form ~form_name:"PDF_SEND_FORM" ~fields:(fields ()) ();; print_string resp#as_string;;