Automatically Detecting Page Numbers and Splitting PDFs into Batches of 10 with n8n + PDF.co
By the end of this tutorial, you’ll have a fully automated workflow that:
- Watches your inbox for new PDF invoice packages
- Uploads each file to PDF.co for secure processing
- Automatically detects the number of pages in the PDF
- Splits the PDF into batches of 10 pages each (only if 15 pages or more)
- Collects all split PDF files individually
- Uploads each file directly to a Google Drive folder (not as a ZIP, but as individual files)
This saves hours of manual splitting and ensures that large PDFs are neatly organized into smaller files in Drive.
Prerequisites
Before you begin, you’ll need:
- A PDF.co API Key (Get one here)
- An IMAP email account (for receiving invoice PDFs)
- A Google Drive account with OAuth2 credentials set up in n8n
- An n8n instance (self-hosted or n8n.cloud)
Quick Start Options
Option A: I Want It Working Now
- Import this workflow template and sample files → Download Workflow File
- Connect your IMAP, Google Drive, and PDF.co accounts
- Define naming conventions and split rules
- Test with sample document collection
- Activate and run
Option B – Learn by Building: Follow the step-by-step tutorial below.
What This Automation Does (Overview)
- Email trigger → Watches inbox for emails with subject “Multiple Invoices”
- Upload to PDF.co → Sends the attached PDF to PDF.co
- Extract Page Info → Gets total page count of the uploaded PDF
- Custom JS Logic → Decides whether to split or keep whole (rules below)
- If < 10 pages → Keep whole file
- If 10–14 pages → Keep whole file
- If 15+ pages → Split into 10-page batches (1–10, 11–20, …)
- PDF Split → Splits the document into parts
- Flatten URLs → Collects each split file URL into separate workflow items
- Download → HTTP Request node downloads each PDF
Upload to Google Drive → Saves each file individually in your chosen folder
Step 1: Trigger Workflow on Incoming Email
Watch your inbox for new emails with the subject “Multiple Invoices”. When found, it automatically downloads the attached PDF so the workflow can process it.
Node: Email Trigger (IMAP)
- Mailbox Name: INBOX
- Action: Mark as Read
- Download Attachments: True
- Custom Email Rules:
["UNSEEN", ["SUBJECT", "Multiple Invoices "]]
Success: Workflow triggers only when a new Multiple Invoices email with attachments is received.
Step 2: Upload the PDF to PDF.co
Sends the attached PDF file securely to PDF.co. This makes the file available for further processing, like checking page count or splitting.
Node: PDF.co API → Upload File
- Binary Data: true
- Binary Property Name:
attachment_0
- Name:
={{ $json.subject }}
Success: Email attachment is uploaded, and PDF.co returns a file URL.
Step 3: Extract Page Count
Uses PDF.co to analyze the uploaded file. It retrieves the total number of pages (PageCount
), which is needed to decide if splitting is required.
Node: PDF.co API → PDF Information & Form Fields
- URL:
={{ $json.url }}
Success: PDF.co responds with metadata, including PageCount
.
Step 4: Apply Page-Splitting Logic (Custom JS)
A custom JavaScript block checks how many pages the PDF has:
- If less than 10 pages → keep the whole file.
- If 10–14 pages → keep the whole file.
- If 15+ pages → split into batches of 10 pages each.
This ensures only big PDFs get split.
Node: Code
Here’s the logic you used:
// Get PageCount safely
const pageCount = parseInt($json.body.PageCount, 10);
if (isNaN(pageCount) || pageCount <= 0) {
throw new Error("Invalid or missing PageCount value.");
}
const ranges = [];
// Rule 1: If below 10 pages → keep whole file
if (pageCount < 10) {
ranges.push(`1-${pageCount}`);
}
// Rule 2: If 15 pages or more → split into 10-page batches
else if (pageCount >= 15) {
const batchSize = 10;
for (let start = 1; start <= pageCount; start += batchSize) {
const end = Math.min(start + batchSize - 1, pageCount);
ranges.push(`${start}-${end}`);
}
}
// Rule 3: If between 10 and 14 → keep whole file
else {
ranges.push(`1-${pageCount}`);
}
return [{
pages: ranges.join(",")
}];
Success: Produces page ranges like 1-10,11-20,21-30
.
Step 5: Split the PDF
Calls PDF.co again with the page ranges from Step 4. PDF.co creates separate files for each range (e.g., 1-10.pdf
, 11-20.pdf
).
Node: PDF.co API → Split PDF
- URL:
={{ $('Assign URLs to Binary Files').item.json.url }}
- Pages:
={{ $json.pa
ges }}
Success: Returns an array of split file URLs.
Step 6: Flatten the Split URLs
The split step returns all file URLs in one array. This code breaks them into individual items, so each file can be processed one by one.
Node: Code
// Get the array of URLs from the Split PDF output
const urls = $json.body;
if (!Array.isArray(urls)) {
throw new Error("Expected 'body' to be an array of URLs, got: " + typeof urls);
}
return urls.map(url => ({
json: { url }
}));
Success: Each split file URL is turned into an individual n8n item.
Step 7: Download Each PDF
For each file URL, the HTTP Request node downloads the PDF. This prepares the binary data so it can be uploaded to Google Drive.
Node: HTTP Request
- Method: GET
- URL:
={{ $json.url }}
- Response Format: File
- Batching: 1 file per request
Success: Downloads each split file individually.
Step 8: Upload to Google Drive
Finally, each file is uploaded individually into your Google Drive folder (not zipped). Each file is named according to the original split part (like Multiple_Invoices_page1-10.pdf
).
Node: Google Drive → Upload
- Drive: My Drive
- Folder: OUTPUT1
- Binary Property:
data
File Name: {{$json["url"].split("/").pop().split("?")[0]}}
Success: Each split file is uploaded to Drive as a standalone PDF.
Congratulations!
You’ve now automated a workflow that:
- Detects PDF page numbers automatically
- Splits only when needed
- Uploads each resulting PDF into your Google Drive folder
No more manual splitting — just drop invoices into your inbox, and everything else happens automatically.
Related Tutorials



