1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
mod errors;
extern crate lopdf;
use std::path::Path;
use std::str;
use std::string::String;
use std::vec::Vec;
use lopdf::{Document, Object};
use errors::Result;
/// Given a file path to a PDF, return a Vec of all URLs in the document.
pub fn get_urls_from_pdf<P: AsRef<Path>>(path: P) -> Result<Vec<String>> {
let doc = Document::load(path)?;
let mut urls = Vec::new();
for (_, obj) in doc.objects {
match obj {
Object::Dictionary(d) => {
for (k, v) in d.iter() {
let key = str::from_utf8(&k)?;
if object_is_link_annotation(key) {
let url_objects = v.as_dict()?;
for (k, v) in url_objects {
let key = str::from_utf8(&k)?;
if key == "URI" {
match v {
Object::String(s, _) => {
urls.push(String::from_utf8(s.to_vec())?);
},
_ => (),
}
}
}
}
}
},
_ => (),
}
}
urls.dedup();
Ok(urls)
}
/// Returns true if the given PDF object key is a link annotation.
fn object_is_link_annotation(key: &str) -> bool {
key == "A"
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_urls_from_pdf_extracts_urls_from_pdf() {
let expected = vec![
"http://www.gutenberg.org/ebooks/11",
"https://ia800908.us.archive.org/6/items/alicesadventures19033gut/19033-h/images/i002.jpg",
"https://science.nasa.gov/news-article/black-hole-image-makes-history",
];
let urls = get_urls_from_pdf("testdata/Alice's Adventures in Wonderland.pdf").unwrap();
// Allow URLs to be out of order.
for url in expected {
assert!(urls.contains(&url.to_owned()));
}
}
}
|