Skip to main content

generate_pdf/
generate_pdf.rs

1//! Generate a PDF from a simple document and from the feature showcase sample.
2//!
3//! Run with: cargo run --example generate_pdf
4
5use rdocx::Document;
6
7fn main() {
8    // Test 1: Simple document
9    let doc = Document::new();
10    match doc.to_pdf() {
11        Ok(bytes) => {
12            std::fs::write("/tmp/rdocx_simple.pdf", &bytes).unwrap();
13            println!("Simple PDF: {} bytes -> /tmp/rdocx_simple.pdf", bytes.len());
14        }
15        Err(e) => println!("Simple PDF failed: {e}"),
16    }
17
18    // Test 2: Document with content
19    let mut doc = Document::new();
20    doc.set_title("Test PDF Document");
21    doc.set_author("rdocx-pdf");
22    doc.add_paragraph("Chapter 1: Introduction")
23        .style("Heading1");
24    doc.add_paragraph(
25        "This is a test document generated by rdocx and rendered to PDF. \
26         It demonstrates text rendering with proper font shaping and pagination.",
27    );
28    doc.add_paragraph("Section 1.1").style("Heading2");
29    doc.add_paragraph("More content in a sub-section.");
30
31    {
32        let mut table = doc.add_table(2, 3);
33        for r in 0..2 {
34            for c in 0..3 {
35                if let Some(mut cell) = table.cell(r, c) {
36                    cell.set_text(&format!("R{}C{}", r + 1, c + 1));
37                }
38            }
39        }
40    }
41
42    doc.add_paragraph("After the table.");
43
44    match doc.to_pdf() {
45        Ok(bytes) => {
46            std::fs::write("/tmp/rdocx_content.pdf", &bytes).unwrap();
47            println!(
48                "Content PDF: {} bytes -> /tmp/rdocx_content.pdf",
49                bytes.len()
50            );
51        }
52        Err(e) => println!("Content PDF failed: {e}"),
53    }
54
55    // Test 3: From feature_showcase.docx
56    let showcase_path = concat!(
57        env!("CARGO_MANIFEST_DIR"),
58        "/../../samples/feature_showcase.docx"
59    );
60    match Document::open(showcase_path) {
61        Ok(doc) => match doc.to_pdf() {
62            Ok(bytes) => {
63                std::fs::write("/tmp/rdocx_showcase.pdf", &bytes).unwrap();
64                println!(
65                    "Showcase PDF: {} bytes -> /tmp/rdocx_showcase.pdf",
66                    bytes.len()
67                );
68            }
69            Err(e) => println!("Showcase PDF failed: {e}"),
70        },
71        Err(e) => println!("Failed to open showcase: {e}"),
72    }
73}