{
  "name": "Merge every PDF in a Google Drive folder into one file and email it",
  "id": "pdfops-drive-folder-merge",
  "nodes": [
    {
      "parameters": {
        "content": "## Merge every PDF in a Google Drive folder into one file and email it\n\nEvery Monday morning, all PDFs in one Google Drive folder are combined into a single file in name order, saved to a separate output folder and emailed as one attachment. Useful for weekly receipts, signed forms or report bundles.\n\n### How it works\n- **Every Monday** starts the run; **Set Merge Options** holds the source folder, output folder, file name and recipient.\n- **List PDFs in Folder** finds every PDF directly inside the source folder.\n- **Filter Prior Outputs** drops anything named like an earlier bundle, then checks the count. Fewer than 2 or more than 20 PDFs sends the recipient a notice instead of merging.\n- **Sort by File Name** fixes the page order; prefix files `01-`, `02-` to control it.\n- **Download PDF** fetches each file and **Merge All PDFs** joins them in item order.\n- **Save Merged PDF to Drive** stores the result in the output folder and **Email Merged PDF** sends it. A failed merge or save also sends a notice. A Gmail outage fails the run instead.\n\n### Setup\n1. Install the **PDFops** community node (`n8n-nodes-pdfops`) from Settings > Community nodes. It is verified by n8n.\n2. Add a **PDFops API** credential on **Merge All PDFs**: a free key (250 requests/month) is at pdfops.dev/docs/signup?utm_source=template&utm_medium=n8n&utm_campaign=merge-pdfs-in-google-drive-folder#n8n. Keyless works but is capped at 100 requests per IP per month.\n3. Connect Google Drive and Gmail credentials.\n4. In **Set Merge Options**, set the source folder URL, an output folder ID that is a different folder, and the recipient.\n\n### Limits\nOne merge takes 2 to 20 PDFs, each under 4 MB, 2,000 pages at most, and the whole request must stay under 4.5 MB. Keep outputName starting with outputPrefix.",
        "width": 560,
        "height": 1500
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -800,
        -180
      ],
      "id": "note-overview",
      "name": "Overview"
    },
    {
      "parameters": {
        "content": "## 1. Find and check the PDFs\nList the folder's PDFs, drop earlier bundles, and require 2 to 20 files.",
        "width": 1100,
        "height": 600,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -160,
        60
      ],
      "id": "note-section-1",
      "name": "Section 1"
    },
    {
      "parameters": {
        "content": "## 2. Sort, download and merge\nSort into page order, download each file, then PDFops joins them into one.",
        "width": 700,
        "height": 600,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        960,
        60
      ],
      "id": "note-section-2",
      "name": "Section 2"
    },
    {
      "parameters": {
        "content": "## 3. Deliver\nSave the merged file to the output folder and email it.",
        "width": 520,
        "height": 600,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1680,
        60
      ],
      "id": "note-section-3",
      "name": "Section 3"
    },
    {
      "parameters": {
        "content": "## Failure path\nToo few or too many PDFs, or a failed merge or save, sends the recipient a notice instead of failing silently.",
        "width": 520,
        "height": 280,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        800,
        700
      ],
      "id": "note-section-4",
      "name": "Section 4"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 8
            }
          ]
        }
      },
      "id": "trigger-schedule",
      "name": "Every Monday",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -80,
        260
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "cfg-folder",
              "name": "folderUrl",
              "value": "https://drive.google.com/drive/folders/REPLACE_WITH_YOUR_FOLDER_ID",
              "type": "string"
            },
            {
              "id": "cfg-prefix",
              "name": "outputPrefix",
              "value": "merged-",
              "type": "string"
            },
            {
              "id": "cfg-name",
              "name": "outputName",
              "value": "={{ 'merged-' + $now.toFormat('yyyy-MM-dd') + '.pdf' }}",
              "type": "string"
            },
            {
              "id": "cfg-outfolder",
              "name": "outputFolderId",
              "value": "REPLACE_WITH_OUTPUT_FOLDER_ID",
              "type": "string"
            },
            {
              "id": "cfg-recipient",
              "name": "recipient",
              "value": "team@example.com",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": false,
        "options": {}
      },
      "id": "set-options",
      "name": "Set Merge Options",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        140,
        260
      ]
    },
    {
      "parameters": {
        "resource": "fileFolder",
        "searchMethod": "query",
        "queryString": "mimeType = 'application/pdf' and trashed = false",
        "returnAll": true,
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "url",
            "value": "={{ $json.folderUrl }}"
          },
          "whatToSearch": "files"
        },
        "options": {
          "fields": [
            "id",
            "name"
          ]
        }
      },
      "id": "drive-search",
      "name": "List PDFs in Folder",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        360,
        260
      ],
      "onError": "stopWorkflow",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "jsCode": "// A bundle must never feed the next run, and one merge has hard limits.\nconst MAX_MERGE_PDFS = 20; // src/api/limits.ts\nconst prefix = String($('Set Merge Options').first().json.outputPrefix || 'merged-').toLowerCase();\nconst isPriorOutput = (name) => String(name || '').toLowerCase().startsWith(prefix);\nconst files = $input.all().filter((i) => i.json.id && !isPriorOutput(i.json.name));\nconst count = files.length;\n\n// One item only, so the notice is sent once.\nif (count < 2) {\n  return [{ json: { fileCount: count, problem: `Found ${count} PDF(s) to merge (earlier \"${prefix}\" bundles are ignored). At least 2 are needed, so nothing was merged.` } }];\n}\nif (count > MAX_MERGE_PDFS) {\n  return [{ json: { fileCount: count, problem: `Found ${count} PDFs but one merge accepts at most ${MAX_MERGE_PDFS}, so nothing was merged. Move some into another folder.` } }];\n}\nreturn files.map((i) => ({ json: { ...i.json, fileCount: count } }));"
      },
      "id": "code-filter",
      "name": "Filter Prior Outputs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        580,
        260
      ],
      "onError": "stopWorkflow"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "cond-within-limits",
              "leftValue": "={{ !$json.problem }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-within-limits",
      "name": "Within Limits?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        800,
        260
      ]
    },
    {
      "parameters": {
        "sortFieldsUi": {
          "sortField": [
            {
              "fieldName": "name"
            }
          ]
        },
        "options": {}
      },
      "id": "sort-name",
      "name": "Sort by File Name",
      "type": "n8n-nodes-base.sort",
      "typeVersion": 1,
      "position": [
        1000,
        260
      ],
      "onError": "stopWorkflow"
    },
    {
      "parameters": {
        "operation": "download",
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {}
      },
      "id": "drive-download",
      "name": "Download PDF",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1220,
        260
      ],
      "onError": "stopWorkflow"
    },
    {
      "parameters": {
        "operation": "merge",
        "binaryPropertyName": "data",
        "outputBinaryPropertyName": "data",
        "fileName": "={{ $('Set Merge Options').first().json.outputName }}"
      },
      "id": "pdfops-merge",
      "name": "Merge All PDFs",
      "type": "n8n-nodes-pdfops.pdfops",
      "typeVersion": 1,
      "position": [
        1440,
        260
      ],
      "credentials": {
        "pdfopsApi": {
          "id": "",
          "name": "PDFops account"
        }
      },
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "inputDataFieldName": "data",
        "name": "={{ $('Set Merge Options').first().json.outputName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive",
          "cachedResultName": "My Drive"
        },
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $('Set Merge Options').first().json.outputFolderId }}"
        },
        "options": {}
      },
      "id": "drive-upload",
      "name": "Save Merged PDF to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "typeVersion": 3,
      "position": [
        1720,
        200
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $('Set Merge Options').first().json.recipient }}",
        "subject": "={{ 'Merged PDF: ' + $('Set Merge Options').first().json.outputName }}",
        "emailType": "text",
        "message": "={{ 'Attached: ' + $('Merge All PDFs').first().json.mergedCount + ' PDFs from the shared folder, merged in file-name order.' }}",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {
                "property": "data"
              }
            ]
          }
        }
      },
      "id": "gmail-send",
      "name": "Email Merged PDF",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1720,
        400
      ],
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "send",
        "sendTo": "={{ $('Set Merge Options').first().json.recipient }}",
        "subject": "Weekly PDF merge needs attention",
        "emailType": "text",
        "message": "={{ $json.problem ? $json.problem : ('A step of the weekly PDF merge failed (execution ' + $execution.id + '): ' + (($json.error && $json.error.message) ? $json.error.message : $json.error) + ($json.message ? ': ' + $json.message : '')) }}",
        "options": {}
      },
      "id": "gmail-notify",
      "name": "Notify Recipient of Failure",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        860,
        820
      ],
      "onError": "stopWorkflow"
    }
  ],
  "connections": {
    "Every Monday": {
      "main": [
        [
          {
            "node": "Set Merge Options",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Merge Options": {
      "main": [
        [
          {
            "node": "List PDFs in Folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List PDFs in Folder": {
      "main": [
        [
          {
            "node": "Filter Prior Outputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter Prior Outputs": {
      "main": [
        [
          {
            "node": "Within Limits?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Within Limits?": {
      "main": [
        [
          {
            "node": "Sort by File Name",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Recipient of Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sort by File Name": {
      "main": [
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "Merge All PDFs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge All PDFs": {
      "main": [
        [
          {
            "node": "Save Merged PDF to Drive",
            "type": "main",
            "index": 0
          },
          {
            "node": "Email Merged PDF",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Notify Recipient of Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Merged PDF to Drive": {
      "main": [
        [],
        [
          {
            "node": "Notify Recipient of Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Merged PDF": {
      "main": [
        [],
        [
          {
            "node": "Notify Recipient of Failure",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
