
OCR and route email PDF attachments with pdfRest verified n8n node
Gmail + pdfRest + n8n + Google Drive
Turn email attachments into organized documents
Automate email PDF processing with little or no code. Connect Gmail, pdfRest, and Google Drive to add OCR when needed and route attachments into invoice, contract, or review folders.
OCR is a Pro Tool. You can test OCR on the Starter plan, which adds a watermark to the output. Premium applies a per-call Pro Tool fee, while Pro and Enterprise include Pro Tools. Compare pdfRest plans.
What is n8n?
n8n is a low-code automation platform for connecting applications and services. Import this template, then configure its nodes to add pdfRest processing to your workflow.
From inbox to the right folder
Receive
Download PDF attachments from new matching Gmail messages and split them into individual files.
Inspect
Read text page by page. A sparse page triggers OCR using the configured language.
Classify
Apply editable invoice and contract keyword rules. Unmatched, conflicting, or sparse results go to review.
Route
Save the PDF to its configured Drive folder. Keep the original email untouched.
Import and connect your workflow
Import the template
Create a workflow, open the editor’s three-dot menu, and choose Import from File. Select the downloaded JSON and configure it before activating.
Search pdfRest in the nodes panel to install the verified node. Use the setup guide below if you need help.
Connect your accounts
Connect Gmail, Google Drive, and pdfRest. n8n Cloud supports Google sign-in; self-hosted n8n requires custom Google OAuth setup.
Choose your folders
In Classify and choose folder, replace the three folder placeholders with Drive folder IDs. Keep the quotation marks. This is a small JavaScript configuration step.
Set your routing rules
Review the Gmail filter, OCR language, and invoice/contract keywords. OCR defaults to English; changing its language does not translate the routing rules.
Google credentials and Drive folder IDs
Connect Gmail on the trigger and Google Drive on the upload node. Select pdfRest credentials on Add searchable text. If these credentials are not already configured, n8n Cloud users can use managed Google sign-in. For OAuth credentials on self-hosted n8n, follow the custom Google OAuth setup, including the Gmail and Drive APIs.
Find the folders object at the top of the Classify and choose folder Code node. Replace CHOOSE_INVOICE_FOLDER, CHOOSE_CONTRACT_FOLDER, and CHOOSE_REVIEW_FOLDER with your Google Drive folder IDs.
The folder ID is the portion after /folders/ in its URL, not its name or full URL. Keep the quotation marks around each ID. These settings are JavaScript configuration. Keep the folders private to the intended team.
Gmail filter, OCR language, and routing rules
In Watch PDF emails, filters.q currently contains has:attachment filename:pdf. In Add searchable text, options.languages defaults to English.
In Classify and choose folder, invoice detection requires invoice plus total, amount due, or balance due. Contract detection requires agreement or contract plus parties, effective date, or signature. Changing the OCR language does not translate these English routing terms.
The sparse-text threshold is 20 non-space characters per page in both Check whether OCR is needed and Classify and choose folder. Keep both values consistent if you change it. Test any changes with representative documents.
Test, review, then activate
Test text-based, scanned, and mixed PDFs. Check routing and configure error notifications before activating. Failed runs stop; they are not automatically filed for review.
Open the workflow test checklist
- A text-based invoice with invoice and amount-due language routes to invoices without OCR when every page has enough text.
- A scanned PDF invokes OCR, then routes using the extracted result.
- A mixed PDF with a text page and a blank-text scanned page invokes OCR.
- A document matching both categories, neither category, or still containing sparse text routes to review.
- An email containing multiple PDFs and a JPG uploads one file per PDF and ignores the JPG.
- Test PDF processing and Google Drive upload failures. Confirm node errors stop the execution, check for files already uploaded, and verify your error notifications before retrying.
Know what to review
When OCR needs a closer look
The workflow decides whether to run OCR by checking how much text it can extract from each page. It may process a blank page unnecessarily or miss image text beside existing text.
How classification works
Classification uses starter keyword rules, not Claude or another AI model. It is separate from the already-published Claude workflow.
Handling failed runs and duplicates
Review receives processed PDFs with unclear categories or sparse text. Node errors stop the execution; failed items do not go to review. Set up an error-notification workflow before unattended use. Check for existing uploads before retrying to avoid duplicates.
In this template, n8n reads the Gmail attachments. Only PDFs taking the OCR branch are sent to the selected pdfRest endpoint; the other branch bypasses pdfRest. Successful results from either branch are saved to Google Drive. Review access permissions and execution-data retention before production use.
Build your workflow
Download, connect your accounts, and test with a sample PDF.