{
  "id": "NmF3SAX03cSvsHpW",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Generate, Encrypt, and Send Invoices with PDF Generator API & Google Suite",
  "tags": [],
  "nodes": [
    {
      "id": "e30f9a26-b270-41ea-b732-95df22a8b3ed",
      "name": "Generate Invoice ID",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        208
      ],
      "parameters": {
        "jsCode": "function generateOrderNumber(length = 8) {\n  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';\n  let result = 'INVOICE-';\n  for (let i = 0; i < length; i++) {\n    result += chars.charAt(Math.floor(Math.random() * chars.length));\n  }\n  return result;\n}\n\nreturn [\n  {\n    json: {\n      order_number: generateOrderNumber()\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "5fb09b61-d55e-4b78-bdd8-cfc997737434",
      "name": "Check if ID Already Exists",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        688,
        208
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1915774434,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1WO-vIxG5lSWWF7_BwDDjLSfEbwNkFtIcMSgFRKAoUCU/edit#gid=1915774434",
          "cachedResultName": "Sheet2"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1WO-vIxG5lSWWF7_BwDDjLSfEbwNkFtIcMSgFRKAoUCU",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1WO-vIxG5lSWWF7_BwDDjLSfEbwNkFtIcMSgFRKAoUCU/edit?usp=drivesdk",
          "cachedResultName": "n8n pokus x"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.5,
      "alwaysOutputData": true
    },
    {
      "id": "7a7132cf-2847-4dc6-812c-036339b6d30d",
      "name": "If Does not Exist",
      "type": "n8n-nodes-base.if",
      "position": [
        912,
        208
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "loose"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cee7659a-cea3-494a-b4f3-c76d035a3a32",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json.row_number }}",
              "rightValue": ""
            }
          ]
        },
        "looseTypeValidation": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "23696cc6-71e0-4a5f-a391-5116024ad19d",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        128,
        0
      ],
      "parameters": {
        "color": 3,
        "height": 352,
        "content": "## Pinned TEST data \nThis pinned data simulates the test payload (JSON) from the Webhook. It contains sample customer and line item details needed to test the invoice generation."
      },
      "typeVersion": 1
    },
    {
      "id": "32b3abc7-9cc3-4586-8bc2-10234c1071f2",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        128
      ],
      "parameters": {
        "width": 656,
        "height": 288,
        "content": "## Generate a random INVOICE number and check for duplicities"
      },
      "typeVersion": 1
    },
    {
      "id": "d0c8d1a6-02df-4038-af24-ee607f67a1ae",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1152,
        16
      ],
      "parameters": {
        "color": 5,
        "width": 224,
        "height": 528,
        "content": "## Generate a document and Encrypt"
      },
      "typeVersion": 1
    },
    {
      "id": "1d61d3f5-251f-43b2-9d75-e916fc04c2ca",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        416,
        448
      ],
      "parameters": {
        "color": 4,
        "width": 624,
        "height": 272,
        "content": "## Upload the file and send the invoice to the customer"
      },
      "typeVersion": 1
    },
    {
      "id": "e6b468a5-abc2-4a81-b7ec-a1fc133b7cfa",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        192,
        208
      ],
      "parameters": {
        "path": "4707540d-382e-45b2-a385-4ee35b447c27",
        "options": {}
      },
      "typeVersion": 2.1
    },
    {
      "id": "c4a87e74-50e3-4e67-a313-9503502987e7",
      "name": "Generate a PDF document",
      "type": "@pdfgeneratorapi/n8n-nodes-pdf-generator-api.pdfGeneratorApi",
      "position": [
        1216,
        128
      ],
      "parameters": {
        "data": "={\n  \"order_number\": \"{{ $('Generate Invoice ID').item.json.order_number }}\",\n  \"name\": \"{{ $('Webhook').item.json.name }}\",\n  \"company\": \"{{ $('Webhook').item.json.company }}\",\n  \"address1\": \"{{ $('Webhook').item.json.address1 }}\",\n  \"city\": \"{{ $('Webhook').item.json.city }}\",\n  \"additional_fee\": \"{{ $('Webhook').item.json.additional_fee }}\",\n  \"payment_qr_data\": \"{{ $('Webhook').item.json.payment_qr_data }}\",\n  \"line_items\": {{ $('Webhook').item.json.line_items.toJsonString() }}\n}",
        "templateId": {
          "__rl": true,
          "mode": "list",
          "value": "496040",
          "cachedResultName": "n8n Invoice Example (ID: 496040)"
        },
        "documentOutput": "url",
        "additionalFields": {}
      },
      "credentials": {
        "pdfGeneratorApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "44d10eb3-8b32-465d-8482-2bcd0791dd26",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        912,
        -224
      ],
      "parameters": {
        "color": 5,
        "width": 464,
        "height": 224,
        "content": "## PDF Generator API\nThe PDF Generator API node connects to the pdfgeneratorapi.com service. In this workflow, it dynamically generates a PDF document (like an invoice) by populating a custom document template\u2014which can be downloaded from this workflow's description\u2014using data passed from earlier steps. After generation, it uses a separate 'pdfServices' operation to encrypt the PDF with a password, ensuring the file is secure before it's sent to the customer."
      },
      "typeVersion": 1
    },
    {
      "id": "afa73496-801f-4ad4-a412-045466d88175",
      "name": "Upload file",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        496,
        560
      ],
      "parameters": {
        "name": "={{ $('Generate Invoice ID').item.json.order_number }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive",
          "cachedResultUrl": "https://drive.google.com/drive/my-drive",
          "cachedResultName": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "16TmHxVNxPHVoXZX5_an_dPAeFCx_eGKb",
          "cachedResultUrl": "https://drive.google.com/drive/folders/16TmHxVNxPHVoXZX5_an_dPAeFCx_eGKb",
          "cachedResultName": "n8n_folder"
        },
        "inputDataFieldName": "={{ $json.filename }}"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "52a1e363-9295-4772-bdce-bbbe4027ebb9",
      "name": "Send a message + file",
      "type": "n8n-nodes-base.gmail",
      "position": [
        736,
        560
      ],
      "parameters": {
        "sendTo": "={{ $('Webhook').item.json.email }}",
        "message": "=Dear {{ $('Webhook').item.json.name}},\n\nPlease find your invoice ({{ $('Generate Invoice ID').item.json.order_number }}) attached.\n\nThe document is encrypted with your customer_id as a password.\n\nPayment details are included on the invoice. Please don't hesitate to reach out if you have any questions.\n\nWe appreciate your business.\n\nBest regards,",
        "options": {
          "attachmentsUi": {
            "attachmentsBinary": [
              {
                "property": "={{ $('Encrypt PDF document').item.json.filename }}"
              }
            ]
          }
        },
        "subject": "=Here's your {{ $('Generate Invoice ID').item.json.order_number }}",
        "emailType": "text"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1,
      "alwaysOutputData": false
    },
    {
      "id": "2093fdde-1ea0-4237-8bba-d52c014b50b3",
      "name": "Encrypt PDF document",
      "type": "@pdfgeneratorapi/n8n-nodes-pdf-generator-api.pdfGeneratorApi",
      "position": [
        1216,
        352
      ],
      "parameters": {
        "fileUrl": "={{ $json.response }}",
        "resource": "pdfServices",
        "operation": "encrypt",
        "outputFormat": "file",
        "ownerPassword": "={{ $('Webhook').item.json.customer_id }}"
      },
      "credentials": {
        "pdfGeneratorApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1bf2294e-f7ea-4df1-b167-087052a7d957",
  "connections": {
    "Webhook": {
      "main": [
        [
          {
            "node": "Generate Invoice ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload file": {
      "main": [
        [
          {
            "node": "Send a message + file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Does not Exist": {
      "main": [
        [
          {
            "node": "Generate a PDF document",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generate Invoice ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Invoice ID": {
      "main": [
        [
          {
            "node": "Check if ID Already Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Encrypt PDF document": {
      "main": [
        [
          {
            "node": "Upload file",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate a PDF document": {
      "main": [
        [
          {
            "node": "Encrypt PDF document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if ID Already Exists": {
      "main": [
        [
          {
            "node": "If Does not Exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}