oxidize-pdf 2.5.0

A pure Rust PDF generation and manipulation library with zero external dependencies
Documentation
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Export functionality for semantic entities

use super::Entity;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[cfg(any(feature = "semantic", test))]
use super::EntityType;

#[cfg(any(feature = "semantic", test))]
use serde_json::{json, Value};

/// Map of entities organized by page
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityMap {
    /// Document-level metadata
    pub document_metadata: HashMap<String, String>,
    /// Entities organized by page number
    pub pages: HashMap<usize, Vec<Entity>>,
    /// Schema definitions used
    pub schemas: Vec<String>,
}

impl Default for EntityMap {
    fn default() -> Self {
        Self::new()
    }
}

impl EntityMap {
    pub fn new() -> Self {
        Self {
            document_metadata: HashMap::new(),
            pages: HashMap::new(),
            schemas: Vec::new(),
        }
    }

    /// Add an entity to the map
    pub fn add_entity(&mut self, entity: Entity) {
        self.pages.entry(entity.page).or_default().push(entity);
    }

    /// Export to JSON string (requires serde_json feature)
    #[cfg(any(feature = "semantic", test))]
    #[allow(unexpected_cfgs)]
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string_pretty(self)
    }

    /// Export to JSON with custom options (requires serde_json feature)
    #[cfg(any(feature = "semantic", test))]
    #[allow(unexpected_cfgs)]
    pub fn to_json_compact(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(self)
    }

    /// Get all entities of a specific type
    pub fn entities_by_type(&self, entity_type: super::EntityType) -> Vec<&Entity> {
        self.pages
            .values()
            .flat_map(|entities| entities.iter())
            .filter(|e| e.entity_type == entity_type)
            .collect()
    }

    /// Get all entities on a specific page
    pub fn entities_on_page(&self, page: usize) -> Option<&Vec<Entity>> {
        self.pages.get(&page)
    }

    /// Export to JSON-LD format with Schema.org context
    #[cfg(any(feature = "semantic", test))]
    #[allow(unexpected_cfgs)]
    pub fn to_json_ld(&self) -> Result<String, serde_json::Error> {
        let mut json_ld = json!({
            "@context": "https://schema.org",
            "@type": "DigitalDocument",
            "additionalType": "AI-Ready PDF",
            "hasPart": []
        });

        let mut parts = Vec::new();

        for (page_num, entities) in &self.pages {
            for entity in entities {
                let entity_json = entity_to_schema_org(entity, *page_num);
                parts.push(entity_json);
            }
        }

        json_ld["hasPart"] = Value::Array(parts);

        // Add schemas if any
        if !self.schemas.is_empty() {
            json_ld["conformsTo"] = json!(self.schemas);
        }

        // Add document metadata
        if !self.document_metadata.is_empty() {
            for (key, value) in &self.document_metadata {
                json_ld[key] = json!(value);
            }
        }

        serde_json::to_string_pretty(&json_ld)
    }
}

/// Convert EntityType to Schema.org type
#[cfg(any(feature = "semantic", test))]
fn entity_type_to_schema_org(entity_type: &EntityType) -> &'static str {
    match entity_type {
        // Financial Documents
        EntityType::Invoice => "Invoice",
        EntityType::InvoiceNumber => "identifier",
        EntityType::CustomerName => "customer",
        EntityType::TotalAmount => "totalPrice",
        EntityType::TaxAmount => "taxAmount",
        EntityType::DueDate => "paymentDueDate",
        EntityType::LineItem => "LineItem",
        EntityType::PaymentAmount => "price",

        // Identity & Contact
        EntityType::PersonName => "Person",
        EntityType::OrganizationName => "Organization",
        EntityType::Address => "PostalAddress",
        EntityType::PhoneNumber => "telephone",
        EntityType::Email => "email",
        EntityType::Website => "url",

        // Legal Documents
        EntityType::Contract => "DigitalDocument",
        EntityType::ContractParty => "Party",
        EntityType::ContractTerm => "OfferCatalog",
        EntityType::EffectiveDate => "datePublished",
        EntityType::ContractValue => "price",
        EntityType::Signature => "signatureValue",

        // Document Structure
        EntityType::Heading => "Heading",
        EntityType::Paragraph => "Paragraph",
        EntityType::Table => "Table",
        EntityType::List => "ItemList",
        EntityType::Image => "ImageObject",
        EntityType::Text => "Text",
        EntityType::Header => "WPHeader",
        EntityType::Footer => "WPFooter",
        EntityType::PageNumber => "pageStart",

        // Dates and Numbers
        EntityType::Date => "Date",
        EntityType::Amount => "MonetaryAmount",
        EntityType::Quantity => "quantityValue",
        EntityType::Percentage => "ratingValue",

        // Custom
        EntityType::Custom(_) => "Thing",
    }
}

/// Convert Entity to Schema.org JSON-LD
#[cfg(any(feature = "semantic", test))]
fn entity_to_schema_org(entity: &Entity, page_num: usize) -> Value {
    let schema_type = entity_type_to_schema_org(&entity.entity_type);

    let mut json = json!({
        "@type": schema_type,
        "spatialCoverage": {
            "@type": "Place",
            "geo": {
                "@type": "GeoCoordinates",
                "box": format!("{},{},{},{}", entity.bounds.0, entity.bounds.1,
                              entity.bounds.2, entity.bounds.3)
            }
        },
        "pageStart": page_num + 1
    });

    // Add ID if present
    if !entity.id.is_empty() {
        json["@id"] = json!(entity.id);
    }

    // Add properties from metadata
    for (key, value) in &entity.metadata.properties {
        json[key] = json!(value);
    }

    // Add confidence if present
    if let Some(confidence) = entity.metadata.confidence {
        json["confidence"] = json!(confidence);
    }

    // Add schema if present
    if let Some(schema) = &entity.metadata.schema {
        json["conformsTo"] = json!(schema);
    }

    json
}

/// Export format options
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExportFormat {
    /// JSON format (default)
    Json,
    /// JSON-LD with schema.org context
    JsonLd,
    /// XML format
    Xml,
}

impl Default for ExportFormat {
    fn default() -> Self {
        Self::Json
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::semantic::{EntityMetadata, EntityType};

    fn create_test_entity(id: &str, page: usize, entity_type: EntityType) -> Entity {
        Entity {
            id: id.to_string(),
            entity_type,
            bounds: (0.0, 0.0, 100.0, 50.0),
            page,
            metadata: EntityMetadata::new(),
        }
    }

    #[test]
    fn test_entity_map_new() {
        let map = EntityMap::new();

        assert!(map.document_metadata.is_empty());
        assert!(map.pages.is_empty());
        assert!(map.schemas.is_empty());
    }

    #[test]
    fn test_entity_map_default() {
        let map = EntityMap::default();

        assert!(map.document_metadata.is_empty());
        assert!(map.pages.is_empty());
        assert!(map.schemas.is_empty());
    }

    #[test]
    fn test_add_entity() {
        let mut map = EntityMap::new();
        let entity = create_test_entity("e1", 1, EntityType::Text);

        map.add_entity(entity);

        assert!(map.pages.contains_key(&1));
        assert_eq!(map.pages.get(&1).unwrap().len(), 1);
    }

    #[test]
    fn test_add_multiple_entities_same_page() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));
        map.add_entity(create_test_entity("e2", 1, EntityType::Image));
        map.add_entity(create_test_entity("e3", 1, EntityType::Table));

        assert_eq!(map.pages.get(&1).unwrap().len(), 3);
    }

    #[test]
    fn test_add_entities_different_pages() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));
        map.add_entity(create_test_entity("e2", 2, EntityType::Image));
        map.add_entity(create_test_entity("e3", 3, EntityType::Table));

        assert_eq!(map.pages.len(), 3);
        assert!(map.pages.contains_key(&1));
        assert!(map.pages.contains_key(&2));
        assert!(map.pages.contains_key(&3));
    }

    #[test]
    fn test_entities_on_page() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));
        map.add_entity(create_test_entity("e2", 1, EntityType::Image));

        let page_entities = map.entities_on_page(1);
        assert!(page_entities.is_some());
        assert_eq!(page_entities.unwrap().len(), 2);

        let missing_page = map.entities_on_page(99);
        assert!(missing_page.is_none());
    }

    #[test]
    fn test_entities_by_type() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));
        map.add_entity(create_test_entity("e2", 1, EntityType::Text));
        map.add_entity(create_test_entity("e3", 2, EntityType::Image));
        map.add_entity(create_test_entity("e4", 2, EntityType::Text));

        let text_entities = map.entities_by_type(EntityType::Text);
        assert_eq!(text_entities.len(), 3);

        let image_entities = map.entities_by_type(EntityType::Image);
        assert_eq!(image_entities.len(), 1);

        let table_entities = map.entities_by_type(EntityType::Table);
        assert_eq!(table_entities.len(), 0);
    }

    #[test]
    fn test_export_format_default() {
        let format = ExportFormat::default();
        assert_eq!(format, ExportFormat::Json);
    }

    #[test]
    fn test_export_format_variants() {
        assert_eq!(ExportFormat::Json, ExportFormat::Json);
        assert_eq!(ExportFormat::JsonLd, ExportFormat::JsonLd);
        assert_eq!(ExportFormat::Xml, ExportFormat::Xml);
        assert_ne!(ExportFormat::Json, ExportFormat::JsonLd);
    }

    #[test]
    fn test_document_metadata() {
        let mut map = EntityMap::new();
        map.document_metadata
            .insert("title".to_string(), "Test Document".to_string());
        map.document_metadata
            .insert("author".to_string(), "Test Author".to_string());

        assert_eq!(
            map.document_metadata.get("title"),
            Some(&"Test Document".to_string())
        );
        assert_eq!(
            map.document_metadata.get("author"),
            Some(&"Test Author".to_string())
        );
    }

    #[test]
    fn test_schemas() {
        let mut map = EntityMap::new();
        map.schemas.push("https://schema.org".to_string());
        map.schemas.push("https://example.com/schema".to_string());

        assert_eq!(map.schemas.len(), 2);
        assert!(map.schemas.contains(&"https://schema.org".to_string()));
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_to_json() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));

        let json = map.to_json();
        assert!(json.is_ok());

        let json_str = json.unwrap();
        assert!(json_str.contains("pages"));
        assert!(json_str.contains("e1"));
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_to_json_compact() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));

        let json = map.to_json_compact();
        assert!(json.is_ok());

        let json_str = json.unwrap();
        // Compact JSON should not have newlines
        assert!(!json_str.contains("\n  ")); // No indented newlines
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_to_json_ld() {
        let mut map = EntityMap::new();
        map.add_entity(create_test_entity("e1", 1, EntityType::Text));
        map.schemas.push("https://schema.org".to_string());

        let json_ld = map.to_json_ld();
        assert!(json_ld.is_ok());

        let json_str = json_ld.unwrap();
        assert!(json_str.contains("@context"));
        assert!(json_str.contains("schema.org"));
        assert!(json_str.contains("DigitalDocument"));
        assert!(json_str.contains("hasPart"));
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_type_to_schema_org_financial() {
        assert_eq!(entity_type_to_schema_org(&EntityType::Invoice), "Invoice");
        assert_eq!(
            entity_type_to_schema_org(&EntityType::InvoiceNumber),
            "identifier"
        );
        assert_eq!(
            entity_type_to_schema_org(&EntityType::TotalAmount),
            "totalPrice"
        );
        assert_eq!(
            entity_type_to_schema_org(&EntityType::TaxAmount),
            "taxAmount"
        );
        assert_eq!(
            entity_type_to_schema_org(&EntityType::DueDate),
            "paymentDueDate"
        );
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_type_to_schema_org_identity() {
        assert_eq!(entity_type_to_schema_org(&EntityType::PersonName), "Person");
        assert_eq!(
            entity_type_to_schema_org(&EntityType::OrganizationName),
            "Organization"
        );
        assert_eq!(
            entity_type_to_schema_org(&EntityType::Address),
            "PostalAddress"
        );
        assert_eq!(
            entity_type_to_schema_org(&EntityType::PhoneNumber),
            "telephone"
        );
        assert_eq!(entity_type_to_schema_org(&EntityType::Email), "email");
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_type_to_schema_org_structure() {
        assert_eq!(entity_type_to_schema_org(&EntityType::Heading), "Heading");
        assert_eq!(
            entity_type_to_schema_org(&EntityType::Paragraph),
            "Paragraph"
        );
        assert_eq!(entity_type_to_schema_org(&EntityType::Table), "Table");
        assert_eq!(entity_type_to_schema_org(&EntityType::List), "ItemList");
        assert_eq!(entity_type_to_schema_org(&EntityType::Image), "ImageObject");
        assert_eq!(entity_type_to_schema_org(&EntityType::Text), "Text");
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_type_to_schema_org_custom() {
        assert_eq!(
            entity_type_to_schema_org(&EntityType::Custom("MyType".to_string())),
            "Thing"
        );
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_to_schema_org_basic() {
        let entity = create_test_entity("test_id", 1, EntityType::Text);
        let json = entity_to_schema_org(&entity, 1);

        assert_eq!(json["@type"], "Text");
        assert_eq!(json["@id"], "test_id");
        assert_eq!(json["pageStart"], 2); // page_num + 1
    }

    #[cfg(any(feature = "semantic", test))]
    #[test]
    fn test_entity_to_schema_org_with_metadata() {
        let mut entity = create_test_entity("test_id", 0, EntityType::Invoice);
        entity.metadata = entity.metadata.with_property("total", "1000.00");
        entity.metadata = entity.metadata.with_confidence(0.95);
        entity.metadata = entity.metadata.with_schema("https://schema.org/Invoice");

        let json = entity_to_schema_org(&entity, 0);

        assert_eq!(json["@type"], "Invoice");
        assert_eq!(json["total"], "1000.00");
        // Use approximate comparison for f32 -> f64 conversion
        let confidence = json["confidence"].as_f64().unwrap();
        assert!((confidence - 0.95).abs() < 0.001);
        assert_eq!(json["conformsTo"], "https://schema.org/Invoice");
    }
}