Debian Codegraph @

debian/missing-sources/lopdf/examples/verify_pdf.rs

raw
1 use lopdf::{Document, Object};
2 use std::env;
3
4 #[cfg(feature = "async")]
5 use tokio::runtime::Builder;
6
7 #[cfg(not(feature = "async"))]
8 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
9 Document::load(path)
10 }
11
12 #[cfg(feature = "async")]
13 fn load_document(path: &str) -> Result<Document, lopdf::Error> {
14 Builder::new_current_thread()
15 .enable_all()
16 .build()
17 .unwrap()
18 .block_on(async move {
19 Document::load(path).await
20 })
21 }
22
23 fn main() {
24 let args: Vec<String> = env::args().collect();
25 if args.len() < 2 {
26 eprintln!("Usage: {} <pdf_file>", args[0]);
27 std::process::exit(1);
28 }
29
30 let pdf_path = &args[1];
31 println!("Verifying PDF: {}", pdf_path);
32
33 match load_document(pdf_path) {
34 Ok(doc) => {
35 println!("✓ PDF loaded successfully");
36 println!(" Version: {:?}", doc.version);
37 println!(" Objects: {}", doc.objects.len());
38
39 // Count pages
40 let pages = doc.get_pages();
41 println!(" Pages: {}", pages.len());
42
43 // Check for object streams
44 let mut obj_stream_count = 0;
45 for (_id, obj) in &doc.objects {
46 if let Object::Stream(stream) = obj {
47 if let Ok(type_obj) = stream.dict.get(b"Type") {
48 if let Ok(type_name) = type_obj.as_name() {
49 if type_name == b"ObjStm" {
50 obj_stream_count += 1;
51 }
52 }
53 }
54 }
55 }
56
57 if obj_stream_count > 0 {
58 println!(" Object streams: {}", obj_stream_count);
59 }
60
61 println!("\nPDF is valid and can be opened!");
62 }
63 Err(e) => {
64 eprintln!("✗ Failed to load PDF: {}", e);
65 std::process::exit(1);
66 }
67 }
68 }