diff options
| author | Teddy Wing | 2021-05-04 21:19:00 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2021-05-04 21:19:00 +0200 | 
| commit | a6b23b8dd102b8be935292b2b27b180d3925cbc1 (patch) | |
| tree | b7c2375459f5c297df6338f272b03102a61b6e34 | |
| parent | 6b25d87d570d5b0950231a7809bf1913af9c5a9d (diff) | |
| download | pdf-form-replace-font-a6b23b8dd102b8be935292b2b27b180d3925cbc1.tar.bz2 | |
Get variables from command line arguments
| -rw-r--r-- | Cargo.lock | 16 | ||||
| -rw-r--r-- | src/main.rs | 16 | 
2 files changed, 26 insertions, 6 deletions
| @@ -147,6 +147,15 @@ dependencies = [  ]  [[package]] +name = "getopts" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5" +dependencies = [ + "unicode-width", +] + +[[package]]  name = "itoa"  version = "0.4.7"  source = "registry+https://github.com/rust-lang/crates.io-index" @@ -236,6 +245,7 @@ dependencies = [  name = "pdf-form-replace-font"  version = "0.0.1"  dependencies = [ + "getopts",   "lopdf",  ] @@ -452,6 +462,12 @@ dependencies = [  ]  [[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + +[[package]]  name = "unicode-xid"  version = "0.2.2"  source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/src/main.rs b/src/main.rs index e9e198d..4dc7ddb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use lopdf::{Document, Object};  use std::env; -fn main() { +fn main() -> Result<(), Box<dyn std::error::Error>> {      let args: Vec<String> = env::args().collect();      let mut opts = Options::new(); @@ -20,12 +20,14 @@ fn main() {      let input_pdf = if opt_matches.free.is_empty() {          "-"      } else { -        opt_matches.free[0] +        &opt_matches.free[0]      }; -    let output_pdf = opt_matches.opt_str("output").unwrap_or("-"); +    let find = opt_matches.opt_str("find").unwrap(); +    let replace = opt_matches.opt_str("replace").unwrap(); +    let output_pdf = opt_matches.opt_str("output").unwrap_or("-".to_owned()); -    let mut doc = Document::load("./f1040.pdf").unwrap(); +    let mut doc = Document::load(input_pdf).unwrap();      for (_, mut obj) in &mut doc.objects {          match &mut obj { @@ -38,7 +40,7 @@ fn main() {                          let new_properties = std::str::from_utf8(properties)                              .unwrap() -                            .replace("HelveticaLTStd-Bold", "CourierNewPSMT"); +                            .replace(&find, &replace);                          *properties = new_properties.into_bytes();                      } @@ -48,5 +50,7 @@ fn main() {          }      } -    doc.save("./new.pdf").unwrap(); +    doc.save(output_pdf).unwrap(); + +    Ok(())  } | 
