UNPKG

n8n-nodes-json-to-pdf

Version:

Convert Markdown/JSON into PDF (single document or e-book) for n8n

223 lines (160 loc) 6.96 kB
# JSON to PDF A versatile **n8n** community node that converts JSON payloads (with Markdown formatting) into styled PDF documents—either a single PDF or a full e-book with chapters and table of contents. No external services required. --- ## 🚀 Features - **Two operation modes**: - **Single PDF**: treat a JSON field as Markdown text, split on a keyword to insert page breaks. - **Generate E-book**: treat a JSON field as an array of `{ title, content }` chapters, auto-generate a title page, table of contents with links, and chapter formatting. - **Custom typography**: - Choose any PDF font (Helvetica, Times-Roman, Courier, etc.). - Adjust base font size and line spacing. - **Page control**: - In **Single PDF** mode, specify a “page break keyword” (e.g. `## Chapter`) which, when encountered at the start of a line, forces a new page. - In **E-book** mode, control whether each chapter title appears on its own page or inline. - **Table of Contents** (E-book only): - Optionally include a TOC with clickable links to each chapter. - Customize the TOC title. - **Error handling**: - Clear error messages when the JSON field isn’t the expected type. - Detailed stack traces available in the n8n execution log. --- ## 📦 Installation 1. In your n8n instance, open **Settings → Community Nodes → Install** 2. In **npm Package Name** enter: ``` n8n-nodes-free-web-scrapping ```` 3. Click **Install** and restart n8n if prompted. --- ## ⚙️ Node Configuration ### Operation | Parameter | Description | | ----------- | -------------------------------------------------------------------------------------------------------------------- | | `operation` | **single**: render one PDF from a Markdown string. <br>**ebook**: render an entire e-book from an array of chapters. | ### Common Parameters | Parameter | Type | Default | Description | | ------------- | ------ | ----------- | ------------------------------------------------------------------------------------ | | `jsonField` | string | `data` | JSON field that holds either the Markdown string (single) or chapters array (ebook). | | `font` | string | `Helvetica` | PDF font name. | | `fontSize` | number | `12` | Base font size (points). | | `lineSpacing` | number | `1.15` | Line height multiplier. | ### Single PDF Mode | Parameter | Type | Default | Description | | -------------- | ------ | ------------ | -------------------------------------------------- | | `pageBreakKey` | string | `## Chapter` | Lines starting with this keyword force a new page. | **Example JSON input** for “single” mode: ```json { "data": "# Introduction\nThis is text.\n## Chapter 1\nFirst chapter content...\n## Chapter 2\nSecond chapter content..." } ``` * Output: one PDF, with page breaks at each `## Chapter`. ### Generate E-book Mode | Parameter | Type | Default | Description | | ------------------- | ------- | ------------------- | -------------------------------------------------------------------------- | | `includeToc` | boolean | `true` | Whether to add a “Table of Contents” page. | | `tocTitle` | string | `Table of Contents` | Title text for the TOC page. | | `chapterTitleStyle` | options | `newPage` | `newPage`: each chapter title on its own page. <br>`inline`: title inline. | **Expected JSON structure**: ```json { "title": "My Great E-Book", "data": [ { "title": "Chapter 1: Getting Started", "content": "## Hello\nThis is **bold** text..." }, { "title": "Chapter 2: Advanced Topics", "content": "More _Markdown_ here..." } ] } ``` * The node will: 1. Insert a title page. 2. Optionally generate a TOC with links to each chapter. 3. Iterate each chapter, adding titles and content. --- ## 🛠️ Examples ### Example 1: Single PDF 1. **Operation**: `single` 2. **JSON Field**: `data` 3. **Page Break Keyword**: `## PART` **Input JSON**: ```json { "data": "# Welcome\nSome intro text.\n## PART One\nContent of part one.\n## PART Two\nContent of part two." } ``` **Result**: PDF with three pages: * Page 1: “Welcome” + intro * Page 2: “PART One” content * Page 3: “PART Two” content ### Example 2: Generate E-book 1. **Operation**: `ebook` 2. **JSON Field**: `data` 3. **Include TOC**: `true` 4. **Chapter Title Style**: `newPage` **Input JSON** (inbound to the node): ```json { "title": "My Guide", "data": [ { "title": "Chapter 1: Beginnings", "content": "This is **first** chapter.\nWith multiple lines." }, { "title": "Chapter 2: Next Steps", "content": "Second chapter _content_." } ] } ``` **Result**: * Page 1: centered “My Guide” * Page 2: “Table of Contents” with links to #chapter-1 and #chapter-2 * Subsequent pages: each chapter on its own page with content rendered from Markdown. --- ## ⚠️ Common Errors **Error**: ``` Output 1 item Field "data" is not an array of chapters Error details Other info n8n version 1.100.1 (Self Hosted) Stack trace Error: Field "data" is not an array of chapters at ExecuteContext.execute (/…/JsonToPdf.node.js:155:23) at WorkflowExecute.runNode (…/workflow-execute.ts:1193:32) … ``` * **Cause**: In `ebook` mode, the node expected `jsonField` to point at an *array* of `{ title, content }`, but found something else. * **Solution**: Ensure your JSON conforms to the required structure: ```json { "data": [ { "title": "...", "content": "..." }, … ] } ``` **Error**: ``` Field "data" is not a string ``` * **Cause**: In `single` mode, the node expected `jsonField` to be a Markdown string. * **Solution**: Provide a plain string: ```json { "data": "# Hello\nSome text…" } ``` --- ## 🧑‍💻 Development & Contributing 1. **Clone** this repo. 2. Run `npm install`. 3. Edit `src/nodes/JsonToPdf/JsonToPdf.node.ts`. 4. Compile: `npm run build`. 5. Link locally in n8n: ```bash cd ~/.n8n/nodes ln -s /path/to/n8n-nodes-json-to-pdf dist ``` 6. Restart n8n and test. --- ## 📝 License MIT © \[Threndy]