generate functionSun, 13 Nov 2022

read data from a base64 encoded binary pdf file, format it and map it to JSON and save that as a UTF-8 text file

use std::fs::File; use std::io::prelude::*; fn main() { let path = "file.pdf"; let mut file = File::open(&path).unwrap(); let mut contents = String::new(); file.read_to_string(&mut contents).unwrap(); let data = base64::decode(&contents).unwrap(); let file = File::create("data.json").unwrap(); let mut writer = BufWriter::new(file); let json = format!("{{\n\"data\": [{}]\n}}", data.iter().map(|x| format!("{}", x)).collect::<Vec<String>>().join(", ")); let _ = writer.write_all(json.as_bytes()); let _ = writer.flush(); }

Questions about programming?Chat with your personal AI assistant