Generate LEDES invoices from email attachments with OpenAI, Gmail, and Sheets
Go to WorkflowDescription
Quick overview
This workflow watches an IMAP inbox for new emails with PDF or Word attachments, extracts legal time entries using OpenAI, generates LEDES 1998B, LEDES XML 2.0, and a PDF invoice (via a Gotenberg HTML-to-PDF endpoint), then logs results to Google Sheets and emails the files back via Gmail.
How it works
Triggers when a new unread email arrives via IMAP and downloads the first attachment.
Detects whether the attachment is a PDF or Word document and extracts the document text accordingly.
Uses OpenAI to extract structured time entries and summary metadata from the document text.
Uses OpenAI to extract the sender’s remittance details from the email content and merges them with the extracted time-entry data.
Reads existing invoice numbers from Google Sheets, calculates the next invoice number, and combines it with the invoice data.
Builds LEDES 1998B text, LEDES XML 2.0, and an invoice HTML, then converts the HTML to a PDF using a Gotenberg-compatible HTTP endpoint.
Appends the LEDES-style line items and the new invoice number to Google Sheets and sends an HTML email with the PDF, LEDES TXT, and LEDES XML files attached via Gmail.
Setup
Add IMAP credentials to the email trigger and ensure the mailbox contains unread emails with PDF/DOC/DOCX attachments.
Add OpenAI credentials for both chat models used by the workflow and verify the model-to-agent connections are linked after importing.
Add Google Sheets OAuth credentials and replace YOUR_SPREADSHEET_ID with your spreadsheet ID for the “Invoice Numbers” and “Time Records” sheets.
Set up a Gotenberg (or compatible) HTML-to-PDF endpoint and configure the HTTP request URL plus HTTP Basic Auth credentials.
Add Gmail OAuth credentials and confirm the workflow is allowed to send emails to the extracted sender address with attachments.
Requirements
Self-hosted n8n
IMAP email credentials
Google Sheets API credentials
OpenAI API key
Gmail API credentials
HTML to PDF conversion service URL
Customization
Adjust extraction logic for specialized LEDES formats in the "Build LEDES 1998B File" and "Build LEDES XML 2.0 File" nodes
Modify the email parsing logic in the "Detect Attachment Type" node to support additional file formats
Customize invoice HTML output in the "Build Invoice HTML" node for branding or additional details
Change Google Sheets column mapping in "Prepare Sheet Rows" to match your record structure
Update email templates in "Send Invoice via Gmail" to fit your organizational preferences