n8n Workflow
Intermediate

Convert HTML to PDF using ConvertAPI

Automate HTML to PDF conversion using the ConvertAPI. This workflow generates a PDF from an HTML string and saves it to your disk, perfect for document generation.

Overview

Generates a PDF document from a given HTML string by calling the ConvertAPI and saves the resulting file to the local disk.

🧠 Description & Use Case

This workflow demonstrates how to convert an HTML string into a PDF file using the ConvertAPI service. It's a straightforward process for anyone needing to programmatically generate PDF documents from web content, reports, or invoices.

🔄 How It Works:

  1. Trigger: The workflow is initiated manually by clicking 'Test workflow'.
  2. Create HTML: A Set node defines a simple HTML document as a string. In a real-world scenario, this HTML could be dynamically generated from other data sources.
  3. Prepare File for Upload: A Code node converts the HTML string into a binary file format that can be sent in an HTTP request. This is a crucial step to prepare the data for the API.
  4. Convert to PDF via API: The HTTP Request node sends the prepared HTML file to the ConvertAPI endpoint (/convert/html/to/pdf). It uses a POST request with multipart-form-data and handles the authentication using your ConvertAPI secret.
  5. Save the Result: The Write Result File to Disk node takes the PDF file returned by the ConvertAPI and saves it locally as document.pdf.

✅ Real-World Use Cases:

  • Invoice Generation: Automatically create PDF invoices from order data stored in your database or spreadsheet.
  • Report Automation: Generate daily, weekly, or monthly reports in a printable PDF format from analytics data.
  • Web Page Archiving: Save snapshots of important web pages or online receipts as permanent PDF files.
  • Document Creation: Create standardized documents like contracts, proposals, or certificates from user-submitted form data.

Instructions

Basic steps

  1. Download workflow JSON
  2. Import into n8n
  3. Configure node parameters as needed
  4. Test and enable the workflow

💡 Tips

After importing, verify all connectors and credentials match your environment.

Preview

Workflow Visualization

6 nodes
100%
When clicking ‘Test workflow’manualTriggerWrite Result File to DiskreadWriteFileSticky NotestickyNoteCreate HTMLsetConvert HTML to FilecodeConvert File to PDFhttpRequest

Nodes

1

When clicking ‘Test workflow’

manualTrigger

v1
2

Write Result File to Disk

readWriteFile

v1
3

Sticky Note

stickyNote

v1
4

Create HTML

set

v3.3
5

Convert HTML to File

code

v2
6

Convert File to PDF

httpRequest

v4.2

Statistics

Total nodes:6
Disabled nodes:0
Node types:6
Connections:4
View full JSON structure
{
  "nodes": [
    {
      "id": "3409b6e3-aef1-4eb4-acfb-72a73101e109",
      "name": "When clicking ‘Test workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        380,
        240
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "4942cdfc-bc9a-43ac-a60d-06e1ddf52d07",
      "name": "Write Result File to Disk",
      "type": "n8n-nodes-base.readWriteFile",
      "position": [
        1360,
        240
      ],
      "parameters": {
        "options": {},
        "fileName": "document.pdf",
        "operation": "write",
        "dataPropertyName": "=data"
      },
      "typeVersion": 1
    },
    {
      "id": "1467a9ab-144d-48cc-a52f-3dca86ca0e8b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        880,
        100
      ],
      "parameters": {
        "width": 218,
        "height": 132,
        "content": "## Authentication\nConversion requests must be authenticated. Please create \n[ConvertAPI account to get authentication secret](https://www.convertapi.com/a/signin)"
      },
      "typeVersion": 1
    },
    {
      "id": "4d85a311-8e39-48ce-868e-95efec509247",
      "name": "Create HTML",
      "type": "n8n-nodes-base.set",
      "position": [
        580,
        240
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "ad325c1b-1597-45ab-98cd-1801da32e3f1",
              "name": "data",
              "type": "string",
              "value": "=<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>ConvertAPI Test Document</title>\n</head>\n<body>\n    <h1>ConvertAPI Test Document</h1>\n    <p>This is a minimal HTML5 document used for testing ConvertAPI's conversion capabilities.</p>\n    <section>\n        <h2>Section Title</h2>\n        <p>This is a section within the document.</p>\n    </section>\n    <footer>\n        <p>&copy; 2024 Test Document</p>\n    </footer>\n</body>\n</html>"
            }
          ]
        }
      },
      "typeVersion": 3.3
    },
    {
      "id": "a0e4e17a-097f-4127-9b60-c6ae637816a0",
      "name": "Convert HTML to File",
      "type": "n8n-nodes-base.code",
      "position": [
        760,
        240
      ],
      "parameters": {
        "jsCode": "const text = $node[\"Create HTML\"].json[\"data\"]\nconst buffer = Buffer.from(text, 'utf8');\nconst binaryData = {\n  data: buffer.toString('base64'),\n  mimeType: 'application/octet-stream',\n  fileName: 'file.html',\n};\nitems[0].binary = { data: binaryData };\nreturn items;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "653b21eb-dae5-44e0-858a-a2905f495911",
      "name": "Convert File to PDF",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        940,
        240
      ],
      "parameters": {
        "url": "https://v2.convertapi.com/convert/html/to/pdf",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file"
            }
          }
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "file",
              "parameterType": "formBinaryData",
              "inputDataFieldName": "data"
            }
          ]
        },
        "genericAuthType": "httpQueryAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/octet-stream"
            }
          ]
        }
      },
      "credentials": {
        "httpQueryAuth": {
          "id": "WdAklDMod8fBEMRk",
          "name": "Query Auth account"
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    }
  ],
  "connections": {
    "Create HTML": {
      "main": [
        [
          {
            "node": "Convert HTML to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert File to PDF": {
      "main": [
        [
          {
            "node": "Write Result File to Disk",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert HTML to File": {
      "main": [
        [
          {
            "node": "Convert File to PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "Create HTML",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Actions

Share
Categories:
Tags:
manualtriggersethttprequeststickynotecodereadwritefile

Technical Specs

Complexity:
Intermediate
Node Count:
6 nodes
Published:11 months ago
Updated:6 days ago

Compatibility

Platform:
N8N Cloud & Self-hosted
Min Version:
N8N v1.0.0+
Supports cloud and self-hosted deployment

License

This workflow template follows MIT license, you can freely use, modify and distribute.

Please comply with relevant third-party service terms when using.

Related Workflows