|
| 1 | +""" |
| 2 | +Redoc Invoice Template Demo |
| 3 | +
|
| 4 | +This script demonstrates bidirectional document conversion using Redoc's template system. |
| 5 | +It shows how to: |
| 6 | +1. Generate a PDF invoice from a JSON data file and HTML template |
| 7 | +2. Extract data from an existing PDF invoice |
| 8 | +3. Convert between different document formats |
| 9 | +""" |
| 10 | + |
| 11 | +import json |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | +from typing import Dict, Any, Optional |
| 15 | + |
| 16 | +# Add the project root to the Python path |
| 17 | +sys.path.insert(0, str(Path(__file__).parent.parent.parent)) |
| 18 | + |
| 19 | +from redoc.templates.pdf_handler import PDFTemplateHandler |
| 20 | +from redoc.templates.base import TemplateError |
| 21 | + |
| 22 | +# Paths |
| 23 | +TEMPLATE_DIR = Path(__file__).parent / "templates" |
| 24 | +DATA_DIR = Path(__file__).parent / "data" |
| 25 | +OUTPUT_DIR = Path(__file__).parent / "output" |
| 26 | + |
| 27 | +# Ensure output directory exists |
| 28 | +OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| 29 | + |
| 30 | +def load_json_data(file_path: Path) -> Dict[str, Any]: |
| 31 | + """Load JSON data from a file.""" |
| 32 | + try: |
| 33 | + with open(file_path, 'r', encoding='utf-8') as f: |
| 34 | + return json.load(f) |
| 35 | + except Exception as e: |
| 36 | + print(f"Error loading JSON file {file_path}: {e}") |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | +def generate_invoice() -> None: |
| 40 | + """Generate a PDF invoice from a template and data file.""" |
| 41 | + print("\n=== Generating Invoice ===") |
| 42 | + |
| 43 | + # Load invoice data |
| 44 | + data_file = DATA_DIR / "sample_invoice.json" |
| 45 | + print(f"Loading data from {data_file}") |
| 46 | + invoice_data = load_json_data(data_file) |
| 47 | + |
| 48 | + # Initialize PDF template handler |
| 49 | + handler = PDFTemplateHandler(template_dir=str(TEMPLATE_DIR)) |
| 50 | + |
| 51 | + # Generate PDF |
| 52 | + output_pdf = OUTPUT_DIR / "generated_invoice.pdf" |
| 53 | + print(f"Generating PDF: {output_pdf}") |
| 54 | + |
| 55 | + try: |
| 56 | + # Add calculated fields to the data |
| 57 | + subtotal = sum(item['quantity'] * item['unit_price'] for item in invoice_data['items']) |
| 58 | + tax_amount = sum(item['quantity'] * item['unit_price'] * item['tax_rate'] |
| 59 | + for item in invoice_data['items']) |
| 60 | + total = subtotal + tax_amount - invoice_data.get('discount', 0) |
| 61 | + |
| 62 | + invoice_data.update({ |
| 63 | + 'subtotal': subtotal, |
| 64 | + 'tax_amount': tax_amount, |
| 65 | + 'total': total |
| 66 | + }) |
| 67 | + |
| 68 | + # Render the template and generate PDF |
| 69 | + handler.render_pdf( |
| 70 | + template_name="invoice.html", |
| 71 | + data=invoice_data, |
| 72 | + output_pdf=str(output_pdf) |
| 73 | + ) |
| 74 | + print(f"Successfully generated: {output_pdf}") |
| 75 | + |
| 76 | + # Also generate HTML for reference |
| 77 | + html_output = OUTPUT_DIR / "generated_invoice.html" |
| 78 | + with open(html_output, 'w', encoding='utf-8') as f: |
| 79 | + template = handler.renderer.get_template("invoice.html") |
| 80 | + html_content = template.render(**invoice_data) |
| 81 | + f.write(html_content) |
| 82 | + print(f"Generated HTML version: {html_output}") |
| 83 | + |
| 84 | + return output_pdf |
| 85 | + |
| 86 | + except TemplateError as e: |
| 87 | + print(f"Error generating invoice: {e}") |
| 88 | + sys.exit(1) |
| 89 | + |
| 90 | +def extract_invoice_data(pdf_path: Path) -> Dict[str, Any]: |
| 91 | + """Extract structured data from a PDF invoice.""" |
| 92 | + print("\n=== Extracting Data from Invoice ===") |
| 93 | + print(f"Processing PDF: {pdf_path}") |
| 94 | + |
| 95 | + if not pdf_path.exists(): |
| 96 | + print(f"Error: File not found: {pdf_path}") |
| 97 | + return {} |
| 98 | + |
| 99 | + try: |
| 100 | + handler = PDFTemplateHandler() |
| 101 | + extracted_data = handler.extract_data(str(pdf_path)) |
| 102 | + |
| 103 | + # Save extracted data to JSON |
| 104 | + output_json = OUTPUT_DIR / "extracted_invoice_data.json" |
| 105 | + with open(output_json, 'w', encoding='utf-8') as f: |
| 106 | + json.dump(extracted_data, f, indent=2) |
| 107 | + |
| 108 | + print(f"Extracted data saved to: {output_json}") |
| 109 | + return extracted_data |
| 110 | + |
| 111 | + except Exception as e: |
| 112 | + print(f"Error extracting data from PDF: {e}") |
| 113 | + return {} |
| 114 | + |
| 115 | +def convert_document( |
| 116 | + input_path: Path, |
| 117 | + output_path: Path, |
| 118 | + from_format: str = 'pdf', |
| 119 | + to_format: str = 'html' |
| 120 | +) -> None: |
| 121 | + """Convert a document between different formats.""" |
| 122 | + print(f"\n=== Converting {from_format.upper()} to {to_format.upper()} ===") |
| 123 | + print(f"Input: {input_path}") |
| 124 | + print(f"Output: {output_path}") |
| 125 | + |
| 126 | + if not input_path.exists(): |
| 127 | + print(f"Error: Input file not found: {input_path}") |
| 128 | + return |
| 129 | + |
| 130 | + try: |
| 131 | + # In a real implementation, you would use the appropriate converter |
| 132 | + # based on the input and output formats |
| 133 | + if from_format == 'pdf' and to_format == 'html': |
| 134 | + # Simple conversion using pdf2htmlEX or similar |
| 135 | + # This is a placeholder - you'd need to implement the actual conversion |
| 136 | + print(f"Converting {input_path} to HTML (simulated)") |
| 137 | + with open(input_path, 'rb') as f_in, open(output_path, 'w', encoding='utf-8') as f_out: |
| 138 | + f_out.write(f"<!-- Converted from {input_path.name} -->\n") |
| 139 | + f_out.write("<html><body>") |
| 140 | + f_out.write("<h1>Converted Document</h1>") |
| 141 | + f_out.write(f"<p>This is a simulated conversion from {from_format} to {to_format}.</p>") |
| 142 | + f_out.write("</body></html>") |
| 143 | + else: |
| 144 | + print(f"Conversion from {from_format} to {to_format} not implemented in this demo.") |
| 145 | + return |
| 146 | + |
| 147 | + print(f"Successfully converted to: {output_path}") |
| 148 | + |
| 149 | + except Exception as e: |
| 150 | + print(f"Error during conversion: {e}") |
| 151 | + |
| 152 | +def main(): |
| 153 | + """Run the demo script.""" |
| 154 | + print("=" * 50) |
| 155 | + print("Redoc Document Conversion Demo") |
| 156 | + print("=" * 50) |
| 157 | + |
| 158 | + # 1. Generate a PDF invoice from template and data |
| 159 | + pdf_path = generate_invoice() |
| 160 | + |
| 161 | + # 2. Extract data from the generated PDF |
| 162 | + if pdf_path and pdf_path.exists(): |
| 163 | + extracted_data = extract_invoice_data(pdf_path) |
| 164 | + |
| 165 | + # 3. Demonstrate document conversion |
| 166 | + if extracted_data: |
| 167 | + # Convert the extracted data to a different format |
| 168 | + output_html = OUTPUT_DIR / "converted_invoice.html" |
| 169 | + convert_document(pdf_path, output_html, 'pdf', 'html') |
| 170 | + |
| 171 | + print("\nDemo completed!") |
| 172 | + print(f"Check the '{OUTPUT_DIR}' directory for generated files.") |
| 173 | + |
| 174 | +if __name__ == "__main__": |
| 175 | + main() |
0 commit comments