Quick overview
This workflow watches Gmail for emails with PDF attachments, extracts per-page text to detect scanned documents, runs OCR via pdfRest when needed, classifies each PDF using keyword rules, and uploads the file to the appropriate Google Drive folder.
How it works
- Triggers every minute by polling Gmail for messages that match a search query and downloads their attachments.
- Splits each email into individual PDF attachment items and keeps the PDF binary for processing.
- Extracts text from each PDF page and flags the document for OCR when pages are blank or have very little text.
- Sends sparse-text PDFs to pdfRest OCR to add a searchable text layer, then re-extracts per-page text from the OCR output.
- Classifies each document as invoice, contract, or review using simple keyword rules and assigns a destination Google Drive folder ID.
- Uploads the PDF to Google Drive with a category-based filename into the selected folder.
Setup
- Add Gmail credentials and adjust the Gmail search query (and mailbox/label strategy) to target the messages you want to process.
- Add pdfRest credentials for the OCR operation and set the OCR language options if needed.
- Add Google Drive credentials, create three Drive folders (invoice/contract/review), and replace the folder ID placeholders in the classification code with your real folder IDs.