8
n8n 中文网amn8n.com

生成发票

中级

这是一个Finance, AI, IT Ops领域的自动化工作流,包含 12 个节点。主要使用 If, Set, Code, Webhook, EmailSend 等节点,结合人工智能技术实现智能自动化。 生成发票,保存到云端硬盘并使用JS+谷歌表格发送邮件给客户

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • Google Drive API 凭证
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "K4XpJbwBJYnDiYkF",
  "meta": {
    "instanceId": "b3c467df4053d13fe31cc98f3c66fa1d16300ba750506bfd019a0913cec71ea3",
    "templateCredsSetupCompleted": true
  },
  "name": "生成发票",
  "tags": [],
  "nodes": [
    {
      "id": "e56cbf88-1e0d-4dd7-a269-091ad2ce8420",
      "name": "Webhook 模拟器",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -240,
        20
      ],
      "webhookId": "7f884a07-861e-49da-bb0e-1ac6ca40ea00",
      "parameters": {
        "path": "7f884a07-861e-49da-bb0e-1ac6ca40ea00",
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "b01307f7-b5cb-421e-833a-0889925bb09c",
      "name": "生成发票 ID",
      "type": "n8n-nodes-base.code",
      "position": [
        -20,
        20
      ],
      "parameters": {
        "jsCode": "function generateOrderID(length = 6) {\n  const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';\n  let result = 'INV-';\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_id: generateOrderID()\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "bf50ca10-4021-4e6f-9c08-56fd84ce13ed",
      "name": "检查 ID 是否已存在",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        200,
        20
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json.order_id }}",
              "lookupColumn": "Invoice ID"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM/edit#gid=0",
          "cachedResultName": "Invoices"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM/edit?usp=drivesdk",
          "cachedResultName": "Uppfy Shop Invoices"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "d0qeLhShx9sGXalR",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.5,
      "alwaysOutputData": true
    },
    {
      "id": "c63871fa-ca0f-4069-ac0d-422d39018a9e",
      "name": "如果不存在",
      "type": "n8n-nodes-base.if",
      "position": [
        420,
        20
      ],
      "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": "feb9ed84-a62c-4cec-bf36-b53a76711bcd",
      "name": "设置字段",
      "type": "n8n-nodes-base.set",
      "position": [
        740,
        -140
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "1bd457b4-2c15-433a-96b3-5fabb8566866",
              "name": "order_id",
              "type": "string",
              "value": "={{ $('Generate Invoice ID').item.json.order_id }}"
            },
            {
              "id": "15c51334-b127-485e-9be4-051b086d74b2",
              "name": "customer_name",
              "type": "string",
              "value": "={{ $('Webhook Simulator').item.json.customer_name }}"
            },
            {
              "id": "8c4f2d5c-09c8-4b8c-a77f-ab3c1d752c13",
              "name": "customer_email",
              "type": "string",
              "value": "={{ $('Webhook Simulator').item.json.customer_email }}"
            },
            {
              "id": "ea1bbfe4-c12d-450f-9bee-fff8710eed42",
              "name": "products",
              "type": "array",
              "value": "={{ $('Webhook Simulator').item.json.products }}"
            },
            {
              "id": "025d0783-7eb0-4ac4-b373-39f744692cfd",
              "name": "total",
              "type": "number",
              "value": "={{ $('Webhook Simulator').item.json.total }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "0532fa4b-cdd9-4857-8701-6a5baf48fd7c",
      "name": "创建发票 HTML",
      "type": "n8n-nodes-base.code",
      "position": [
        740,
        100
      ],
      "parameters": {
        "jsCode": "const { order_id, customer_name, customer_email, products, total } = $json;\n\n// Format products table rows\nconst productRows = products.map((item, index) => {\n  const subtotal = item.quantity * item.price;\n  return `\n    <tr>\n      <td style=\"padding: 8px; border: 1px solid #ccc;\">${index + 1}</td>\n      <td style=\"padding: 8px; border: 1px solid #ccc;\">${item.name}</td>\n      <td style=\"padding: 8px; border: 1px solid #ccc;\">${item.quantity}</td>\n      <td style=\"padding: 8px; border: 1px solid #ccc;\">$${item.price.toFixed(2)}</td>\n      <td style=\"padding: 8px; border: 1px solid #ccc;\">$${subtotal.toFixed(2)}</td>\n    </tr>\n  `;\n}).join('');\n\nconst html = `\n<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"UTF-8\">\n  <title>Invoice ${order_id}</title>\n</head>\n<body style=\"font-family: Arial, sans-serif; color: #333; max-width: 700px; margin: auto; padding: 20px; border: 1px solid #ddd;\">\n  <h2 style=\"text-align: center;\">Invoice</h2>\n  <p><strong>Order ID:</strong> ${order_id}</p>\n  <p><strong>Customer Name:</strong> ${customer_name}</p>\n  <p><strong>Email:</strong> ${customer_email}</p>\n\n  <table style=\"width: 100%; border-collapse: collapse; margin-top: 20px;\">\n    <thead>\n      <tr>\n        <th style=\"padding: 8px; border: 1px solid #ccc;\">#</th>\n        <th style=\"padding: 8px; border: 1px solid #ccc;\">Product</th>\n        <th style=\"padding: 8px; border: 1px solid #ccc;\">Qty</th>\n        <th style=\"padding: 8px; border: 1px solid #ccc;\">Unit Price</th>\n        <th style=\"padding: 8px; border: 1px solid #ccc;\">Subtotal</th>\n      </tr>\n    </thead>\n    <tbody>\n      ${productRows}\n    </tbody>\n    <tfoot>\n      <tr>\n        <td colspan=\"4\" style=\"text-align: right; padding: 8px; border: 1px solid #ccc;\"><strong>Total:</strong></td>\n        <td style=\"padding: 8px; border: 1px solid #ccc;\"><strong>$${total.toFixed(2)}</strong></td>\n      </tr>\n    </tfoot>\n  </table>\n\n  <p style=\"margin-top: 40px; text-align: center;\">Thank you for your order!</p>\n</body>\n</html>\n`;\n\nreturn [\n  {\n    json: {\n      html\n    }\n  }\n];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "5a3adb6a-9f64-477e-9622-3d2b9ef2e6de",
      "name": "HTML 转 PDF",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -20,
        320
      ],
      "parameters": {
        "url": "https://html2pdf2.p.rapidapi.com/html2pdf",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "html",
              "value": "={{ $json.html }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "x-rapidapi-host",
              "value": "html2pdf2.p.rapidapi.com"
            },
            {
              "name": "x-rapidapi-key",
              "value": "\"your_rapid_api_key\""
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "cebf6d0d-d43f-4e50-abc0-79ff8da1817a",
      "name": "上传 PDF 到 Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        420,
        320
      ],
      "parameters": {
        "name": "={{ $('Set Fields').item.json.order_id }} - {{ $('Set Fields').item.json.customer_name }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "l8Cc2MEVE7foBfbK",
          "name": "Google Drive account"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "b181eddc-e786-4d44-a334-e1b581b3981f",
      "name": "通过邮件发送发票给客户",
      "type": "n8n-nodes-base.emailSend",
      "position": [
        640,
        320
      ],
      "webhookId": "cdc852fc-03b9-4a89-ba56-879064da5cbb",
      "parameters": {
        "html": "=Hello  {{ $('Set Fields').item.json.customer_name }},\n\nPlease pay this invoice\n\n\n{{ $('Create Invoice HTML').item.json.html }}\n\n\n<!DOCTYPE html>\n<html>\n<head>\n    <style>\n        .button {\n            display: inline-block;\n            background-color: #4CAF50;\n            border: none;\n            border-radius: 4px;\n            color: white;\n            padding: 15px 32px;\n            text-align: center;\n            text-decoration: none;\n            font-size: 16px;\n            font-weight: bold;\n            margin: 4px 2px;\n            cursor: pointer;\n            font-family: Arial, sans-serif;\n        }\n    </style>\n</head>\n<body>\n    <div style=\"text-align: center;\">\n        <a href=\"YOUR_PAYMENT_LINK_HERE\" class=\"button\">Pay Now</a>\n    </div>\n</body>\n</html>\n\n",
        "options": {
          "appendAttribution": false
        },
        "subject": "={{ $('Set Fields').item.json.order_id }} - New Invoice from Uppfy Shop ",
        "toEmail": "={{ $('Set Fields').item.json.customer_email }}",
        "fromEmail": "Uppfy Shop <your_from_email>"
      },
      "credentials": {
        "smtp": {
          "id": "xcnAVXwtuOa1IOqE",
          "name": "SMTP account"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "883e32b6-a812-44c8-b525-97e819af90bd",
      "name": "将详细信息附加到发票表格",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        860,
        320
      ],
      "parameters": {
        "columns": {
          "value": {
            "Status": "Unpaid",
            "Invoice ID": "={{ $('Set Fields').item.json.order_id }}",
            "Invoice URL": "={{ $('Upload PDF to GDrive').item.json.webViewLink }}",
            "Invoice Amt.": "=${{ $('Set Fields').item.json.total }}",
            "Customer Name": "={{ $('Set Fields').item.json.customer_name }}",
            "Customer Email": "={{ $('Set Fields').item.json.customer_email }}"
          },
          "schema": [
            {
              "id": "Invoice ID",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Invoice ID",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer Name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Customer Name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Customer Email",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Customer Email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Invoice Amt.",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Invoice Amt.",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Invoice URL",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Invoice URL",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Status",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "Invoice ID"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM/edit#gid=0",
          "cachedResultName": "Invoices"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QW_Lg1aoEBku8GaxwPbZfBY5ITAuSdvoAXRyNdCEujM/edit?usp=drivesdk",
          "cachedResultName": "Uppfy Shop Invoices"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "d0qeLhShx9sGXalR",
          "name": "Google Sheets"
        }
      },
      "typeVersion": 4.5
    },
    {
      "id": "cd06d6c9-903d-4af8-8327-eb36ba3bc73a",
      "name": "从 API 下载 PDF",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        200,
        320
      ],
      "parameters": {
        "url": " https://rhodium-tecnologia-html2pdf.s3.amazonaws.com/public/pdfs/1747392997249.pdf",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "7d308e22-9b2d-4993-8aad-cc701303d38d",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -280,
        -160
      ],
      "parameters": {
        "width": 180,
        "height": 320,
        "content": "## 固定数据以模拟 Webhook 查询"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {
    "Webhook Simulator": [
      {
        "json": {
          "total": 65,
          "products": [
            {
              "name": "T-shirt",
              "price": 25,
              "quantity": 2
            },
            {
              "name": "Hat",
              "price": 15,
              "quantity": 1
            }
          ],
          "customer_name": "Jane Doe",
          "customer_email": "jane@example.com"
        }
      }
    ]
  },
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "dec45f2b-cb36-4f86-a8cc-d3c498ae7aa2",
  "connections": {
    "Set Fields": {
      "main": [
        [
          {
            "node": "Create Invoice HTML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTML to PDF": {
      "main": [
        [
          {
            "node": "Download PDF from API",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Does not Exist": {
      "main": [
        [
          {
            "node": "Set Fields",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Generate Invoice ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook Simulator": {
      "main": [
        [
          {
            "node": "Generate Invoice ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Invoice HTML": {
      "main": [
        [
          {
            "node": "HTML to PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Invoice ID": {
      "main": [
        [
          {
            "node": "Check if ID Already Exists",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload PDF to GDrive": {
      "main": [
        [
          {
            "node": "Email Invoice to Customer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF from API": {
      "main": [
        [
          {
            "node": "Upload PDF to GDrive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Email Invoice to Customer": {
      "main": [
        [
          {
            "node": "Append Details to Invoices Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check if ID Already Exists": {
      "main": [
        [
          {
            "node": "If Does not Exist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 财务, 人工智能, IT 运维

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量12
分类3
节点类型9
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Joseph

Joseph

@mjomba

Automation expert specializing in building smart, scalable workflows using tools like n8n, Make, and Airtable. I help businesses save time, reduce manual work, and grow faster with tailored automation solutions. Feel free to reach out at joseph@uppfy.com to discuss your project. I am also on x.com/juppfy

外部链接
在 n8n.io 查看

分享此工作流