JSON Stringify 無法將資料傳送到 PDF

我正在建立一個工作流程,使用 ‘PDF Generator API’ 節點將 AI 代理生成的資料轉換為 PDF 檔案。

我在之前的兩個工作流程中,在節點的 ‘data’ 欄位中使用了完全相同的指令,效果很好:

{{ JSON.stringify({ post_event_report: $json.output }) }}

但在這裡,它產生了空白的 PDF。我已經像在之前的工作流程中一樣,在 PDF Generator 範本中包含了變數,所以我不知道為什麼它突然產生空白 PDF。

有任何想法嗎?

{
“nodes”: [
{
“parameters”: {},
“type”: “n8n-nodes-base.manualTrigger”,
“typeVersion”: 1,
“position”: [
0,
0
],
“id”: “27e200f4-843d-4d50-b42c-0b8a26f7291b”,
“name”: “When clicking ‘Execute workflow’”
},
{
“parameters”: {
“documentId”: {
“__rl”: true,
“value”: “1DSaJKyI1eCp6WZJ8X1aAa6jg7fvOOS5vJFKUmar7quA”,
“mode”: “list”,
“cachedResultName”: “Post Event Reporting”,
“cachedResultUrl”: “https://docs.google.com/spreadsheets/d/1DSaJKyI1eCp6WZJ8X1aAa6jg7fvOOS5vJFKUmar7quA/edit?usp=drivesdk”
},
“sheetName”: {
“__rl”: true,
“value”: “gid=0”,
“mode”: “list”,
“cachedResultName”: “Sheet1”,
“cachedResultUrl”: “https://docs.google.com/spreadsheets/d/1DSaJKyI1eCp6WZJ8X1aAa6jg7fvOOS5vJFKUmar7quA/edit#gid=0”
},
“options”: {}
},
“type”: “n8n-nodes-base.googleSheets”,
“typeVersion”: 4.7,
“position”: [
224,
0
],
“id”: “3b86eb78-0869-40c3-9bcf-1ad77f80cf74”,
“name”: “Get row(s) in sheet”,
“credentials”: {
“googleSheetsOAuth2Api”: {
“id”: “wV1Zw6McyxYXrk8t”,
“name”: “Google Sheets account”
}
}
},
{
“parameters”: {
“promptType”: “define”,
“text”: “= Get {{ $json.Feedback }} for {{ $json[“Event Name”] }} {{ $json.Date }} {{ $json.Location }} {{ $json[‘No. of Guests’] }}”,
“options”: {
“systemMessage”: "=You are a Post Event Reporting specialist working with a Small Catering Business with over 30 years of experience in the industry. Your role is to analyze the spreadsheet and generate areas of improvement based on the {{ $json.Feedback }} given. Compile all of the data into a comprehensive post-event report "
}
},
“type”: “@n8n/n8n-nodes-langchain.agent”,
“typeVersion”: 3.1,
“position”: [
448,
0
],
“id”: “1cebd084-c6fa-4477-b507-96bffc2a3733”,
“name”: “AI Agent”
},
{
“parameters”: {
“templateId”: {
“__rl”: true,
“value”: “1655316”,
“mode”: “list”,
“cachedResultName”: “Post-Event Report (ID: 1655316)”
},
“data”: “={{ JSON.stringify({ post_event_report: $json.output }) }}”,
“documentOutput”: “viewer”,
“additionalFields”: {
“outputName”: “=Post event report for {{ $(‘Get row(s) in sheet’).item.json[“Event Name”] }} on {{ $(‘Get row(s) in sheet’).item.json.Date }}”,
“testing”: false
}
},
“type”: “@pdfgeneratorapi/n8n-nodes-pdf-generator-api.pdfGeneratorApi”,
“typeVersion”: 1,
“position”: [
800,
0
],
“id”: “4304c3b8-5210-4b2d-8d6d-49eac03b03a6”,
“name”: “Generate a PDF document”,
“alwaysOutputData”: true,
“credentials”: {
“pdfGeneratorApi”: {
“id”: “XUfVEGWx0Q45fr5L”,
“name”: “PDF Generator account”
}
}
},
{
“parameters”: {
“options”: {}
},
“type”: “@n8n/n8n-nodes-langchain.lmChatGoogleGemini”,
“typeVersion”: 1.1,
“position”: [
320,
208
],
“id”: “630d55ef-1a19-4f5a-bad5-a14c85c8fb4d”,
“name”: “Google Gemini Chat Model”,
“credentials”: {
“googlePalmApi”: {
“id”: “r5K1AFY1c2dmiqsE”,
“name”: “Google Gemini(PaLM) Api account”
}
}
}
],
“connections”: {
“When clicking ‘Execute workflow’”: {
“main”: [
[
{
“node”: “Get row(s) in sheet”,
“type”: “main”,
“index”: 0
}
]
]
},
“Get row(s) in sheet”: {
“main”: [
[
{
“node”: “AI Agent”,
“type”: “main”,
“index”: 0
}
]
]
},
“AI Agent”: {
“main”: [
[
{
“node”: “Generate a PDF document”,
“type”: “main”,
“index”: 0
}
]
]
},
“Google Gemini Chat Model”: {
“ai_languageModel”: [
[
{
“node”: “AI Agent”,
“type”: “ai_languageModel”,
“index”: 0
}
]
]
}
},
“pinData”: {},
“meta”: {
“templateCredsSetupCompleted”: true,
“instanceId”: “845031bfdb8f607d8c7ae907fb5de28c6fb4d0a8d0cd06c1a605d7c42f4018a9”
}
}

改成這樣: ={{ JSON.stringify({ post_event_report: $json.output }) }}

改成這樣: = { "post_event_report": $json.output }

以下是工作流程

1 個讚

由於這個確切的樣式在其他兩個工作流中都能運作,bug 幾乎肯定是在 $json.output 實際包含的內容,而不是 JSON.stringify 方法本身。你的 JSON 中有兩個地方引人注意:

  1. PDF 節點上的 alwaysOutputData: true 表示即使輸入為空/格式錯誤,它也會樂意返回結果——這正是你收到空白 PDF 而不是錯誤的原因。暫時移除該設定,這樣不良輸入才會真正顯示為你可以檢查的失敗。
  2. 在「產生 PDF 文件」之前放置一個 Set 節點(或只是點擊 PDF 節點的輸入引腳),並檢查 $json.output 實際上在該點是什麼。使用以 Gemini 為後端的 LangChain Agent 節點,我見過回應以不同於預期的方式嵌套的情況(例如,在不同的鍵下,或作為陣列),取決於模型/版本——所以「之前運作的相同表達式」可能這次無聲地解析為 undefined,而 JSON.stringify({ post_event_report: undefined }) 基本上會產生 {},因此會產生空白/樣板但內容為空的 PDF。
  3. 獨立於資料進行檢查也值得一試:PDF 產生器範本(ID 1655316)是否真的有一個名稱完全是 post_event_report 的合併變數?重新命名或新的範本 ID 如果欄位名稱不同,即使資料流進行完美,也會產生相同的「空白 PDF,沒有錯誤」症狀。

隔離它是 (2) 還是 (3) 是一次測試:將資料欄位硬編碼為 {{ JSON.stringify({ post_event_report: “test content” }) }} 並執行它。如果該內容在 PDF 中呈現,問題就在上游(Agent 輸出形狀)。如果仍然是空白,問題就是範本/欄位映射。

嗨 @Tim_Barrett,

來自 PDF Generator API 的空白 PDF 通常是由兩個特定原因引起的:雙重字符串化或未定義的 AI 輸出。

以下是修復方法:

1. 移除 JSON.stringify()(避免雙重轉義)

在 n8n 表達式模式(={{ … }})中,使用 JSON.stringify() 可能會導致雙重字符串化,因為社區節點在將對象發送到 API 之前已經對其進行了序列化。當進行雙重轉義時,PDF Generator API 會收到無效的字符串而不是有效的數據對象,導致所有模板字段呈現為空白。

嘗試將 PDF Generator 節點中的數據字段從:

={{ JSON.stringify({ post_event_report: $json.output }) }}

更改為直接 JavaScript 對象:

={{ { post_event_report: $json.output } }}

2. 檢查 $json.output 是否未定義

如果 AI Agent 輸出屬性缺失或命名不同,JSON.stringify({ post_event_report: undefined }) 會評估為 {}(空 JSON 對象)。當 PDF Generator API 收到不包含匹配鍵的 {} 時,它會呈現空白 PDF。

  • 點擊你的 AI Agent 節點執行輸出,並確保該鍵严格命名為 output。
  • 如果 Gemini 生成包含換行符(\n)的多行 markdown 文本,你可以使用以下方式安全地清理/修剪它:
={{ { post_event_report: ($json.output || "").trim() } }}

快速測試

首先嘗試將數據參數更改為硬編碼字符串對象,以查看你的 PDF 模板是否呈現:

={{ { post_event_report: "Test report content here..." } }}

如果測試文本出現在 PDF 上,那麼切換到 ={{ { post_event_report: $json.output } }} 就能解決你的問題!

如果這能解決空白 PDF 的問題,請告訴我!

謝謝!

空白 PDF 且沒有錯誤通常表示範本渲染正常,但其變數都不符合您傳送的資料。

當上游有 AI Agent 時,常見原因是 Agent 在 output 中將其 JSON 以字串形式返回,通常包裹在程式碼圍籬中。對其執行 JSON.stringify 會讓 PDF 節點收到字串中的字串,因此範本無法看到您的金鑰。

以下是我建議檢查的項目:

  1. 在表格檢視中開啟 Agent 節點輸出。如果您的欄位不是獨立的欄,它仍然是字串。
  2. 在 PDF 節點之前新增 Code 節點,設定為「每項執行一次」,以解析它:
    const s = $json.output; return { json: JSON.parse(s.slice(s.indexOf('{'), s.lastIndexOf('}') + 1)) };
  3. 在 PDF 節點的資料欄位中使用 {{ JSON.stringify($json) }},並確保金鑰名稱完全符合範本變數,包括大小寫和巢狀結構。
  4. 釘選一個小測試項目並單獨執行 PDF 節點,以便您知道問題出在資料還是範本。

我的猜測是您之前的工作流程從 Set 或 HTTP 節點中提供了實際物件,這就是為什麼相同的表達式在那裡有效的原因。