-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b4f105
commit deaa1f7
Showing
10 changed files
with
1,497 additions
and
38 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[package] | ||
name = "rusty_pdf" | ||
description = "Crate adding text and images to existing pdf files" | ||
version = "0.2.0" | ||
version = "0.21.0" | ||
authors = ["Joshua Pauline <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -19,3 +19,5 @@ lopdf = { version = "0.27.0", features = [ | |
], default-features = false, git = "https://github.com/J-F-Liu/lopdf", branch = "master" } | ||
png = "0.17.2" | ||
imagesize = "0.9" | ||
headless_chrome = "0.9.0" | ||
tiny_http = "0.6" |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use rusty_pdf::PDFSigningDocument; | ||
|
||
fn main() { | ||
let merged_doc = PDFSigningDocument::generate_pdf_from_html(include_str!("data/test.html")); | ||
|
||
merged_doc.finished().save("generated_html.pdf").unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background-color: #000000; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,42 @@ use std::fs; | |
|
||
use rusty_pdf::{Font, PDFSigningDocument}; | ||
|
||
#[derive(Debug)] | ||
enum EventType { | ||
Sent, | ||
Completed, | ||
Signed, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct EventUser<'a> { | ||
email: &'a str, | ||
ip: &'a str, | ||
audit_id: &'a str, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct TableRow<'a> { | ||
event: EventType, | ||
time: &'a str, | ||
user: EventUser<'a>, | ||
} | ||
|
||
fn main() { | ||
let rows = vec![TableRow { | ||
event: EventType::Signed, | ||
time: "1234", | ||
user: EventUser { | ||
email: "[email protected]", | ||
ip: "00.00.00.00", | ||
audit_id: "12345qwert", | ||
}, | ||
}]; | ||
|
||
for i in rows { | ||
print!("{:?}", i); | ||
} | ||
|
||
let doc_mem = fs::read("examples/data/pdf_example.pdf").unwrap_or(vec![]); | ||
|
||
let doc = Document::load_mem(&doc_mem).unwrap_or_default(); | ||
|
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use std::sync::{atomic, Arc}; | ||
use std::thread::JoinHandle; | ||
use std::time::Duration; | ||
use std::{fs, io}; | ||
|
||
pub struct Server { | ||
server: Arc<tiny_http::Server>, | ||
handler: Option<JoinHandle<Result<(), io::Error>>>, | ||
shall_exit: Arc<atomic::AtomicBool>, | ||
} | ||
|
||
impl Server { | ||
pub fn new( | ||
mut responder: impl FnMut(tiny_http::Request) -> Result<(), io::Error> + Send + 'static, | ||
) -> Self { | ||
let server = Arc::new(tiny_http::Server::http("127.0.0.1:0").unwrap()); | ||
let shall_exit = Arc::new(atomic::AtomicBool::new(false)); | ||
let srv = server.clone(); | ||
let exit = shall_exit.clone(); | ||
let handler = std::thread::spawn(move || { | ||
loop { | ||
if let Some(r) = srv.recv_timeout(Duration::from_millis(1000))? { | ||
responder(r)?; | ||
} | ||
if exit.load(atomic::Ordering::Relaxed) { | ||
break; | ||
} | ||
} | ||
Ok(()) | ||
}); | ||
Server { | ||
server, | ||
handler: Some(handler), | ||
shall_exit, | ||
} | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn with_dumb_html(data: &'static str) -> Self { | ||
let responder = move |r: tiny_http::Request| { | ||
let response = tiny_http::Response::new( | ||
200.into(), | ||
vec![ | ||
tiny_http::Header::from_bytes(&b"Content-Type"[..], &b"text/html"[..]).unwrap(), | ||
], | ||
io::Cursor::new(data), | ||
Some(data.len()), | ||
None, | ||
); | ||
r.respond(response) | ||
}; | ||
Self::new(responder) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn url(&self) -> String { | ||
format!("http://127.0.0.1:{}", self.port()) | ||
} | ||
|
||
pub fn port(&self) -> u16 { | ||
self.server.server_addr().port() | ||
} | ||
|
||
pub fn exit(&mut self) -> Result<(), io::Error> { | ||
self.shall_exit.store(true, atomic::Ordering::Relaxed); | ||
match self.handler.take() { | ||
Some(h) => h.join().unwrap(), | ||
None => Ok(()), | ||
} | ||
} | ||
} | ||
|
||
impl Drop for Server { | ||
fn drop(&mut self) { | ||
self.exit().unwrap() | ||
} | ||
} | ||
|
||
fn basic_http_response<'a>( | ||
body: &'a str, | ||
content_type: &'static str, | ||
) -> tiny_http::Response<&'a [u8]> { | ||
tiny_http::Response::new( | ||
200.into(), | ||
vec![tiny_http::Header::from_bytes(&b"Content-Type"[..], content_type.as_bytes()).unwrap()], | ||
body.as_bytes(), | ||
Some(body.len()), | ||
None, | ||
) | ||
} | ||
|
||
#[allow(dead_code)] | ||
fn not_found_response() -> tiny_http::Response<io::Empty> { | ||
tiny_http::Response::new_empty(404.into()) | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn file_server(path: &'static str) -> Server { | ||
Server::new(move |request: tiny_http::Request| { | ||
let url = if request.url() == "/" { | ||
"/index.html" | ||
} else { | ||
request.url() | ||
}; | ||
|
||
let file_path = format!("{}{}", path, url); | ||
|
||
if let Ok(file_contents) = fs::read_to_string(file_path) { | ||
let content_type = if url.ends_with(".js") { | ||
"application/javascript" | ||
} else if url.ends_with(".css") { | ||
"text/css" | ||
} else { | ||
"text/html" | ||
}; | ||
|
||
let response = basic_http_response(&file_contents, content_type); | ||
request.respond(response) | ||
} else { | ||
let response = not_found_response(); | ||
request.respond(response) | ||
} | ||
}) | ||
} |