Learn how to use template variables and preview props in your document templates
import { Document } from "@htmldocs/react"; interface InvoiceProps { customerName: string; items: { name: string; quantity: number; price: number; }[]; } function Invoice({ customerName, items }: InvoiceProps) { return ( <Document> <h1>Invoice for {customerName}</h1> <div className="items"> {items.map((item, index) => ( <div key={index} className="item"> <span>{item.name}</span> <span>{item.quantity}</span> <span>${item.price}</span> </div> ))} </div> </Document> ); } export default Invoice;
PreviewProps
Invoice.PreviewProps = { customerName: "Acme Corporation", items: [ { name: "Premium License", quantity: 1, price: 999.00 }, { name: "Support Package", quantity: 2, price: 299.00 } ] };
interface CompanyInfo { name: string; address: { street: string; city: string; state: string; zip: string; }; contact: { email: string; phone: string; }; } interface ReceiptProps { orderNumber: string; date: string; company: CompanyInfo; items: { name: string; quantity: number; price: number; }[]; } Receipt.PreviewProps = { orderNumber: "ORD-123456", date: new Date().toLocaleDateString(), company: { name: "Acme Corp", address: { street: "123 Business Ave", city: "San Francisco", state: "CA", zip: "94105" }, contact: { email: "info@acme.com", phone: "(555) 123-4567" } }, items: [ { name: "Product A", quantity: 1, price: 99.99 } ] };
Document.PreviewProps = { orderNumber: `ORD-${Math.random().toString(36).substr(2, 9).toUpperCase()}`, date: new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' }), total: calculateTotal(items) };