Articles on: Integrations

How to integrate PDF Generator API with n8n Cloud

From Data to Document in 5 Minutes: Your First n8n - PDF Generator API Workflow




Introduction


The true power of automation platforms like n8n is unlocked when you connect them to specialized external services like PDF Generator API. If you've ever needed to dynamically create invoices, reports, or certificates, the PDF Generator API is an excellent tool for the job. In this tutorial, we will build a simple but practical n8n workflow that authenticates and makes an HTTP request to the PDF Generator API, allowing you to create custom documents automatically. The process involves two key nodes: one to generate a JWT with JavaScript and another to make the final API call.


N8N Canvas


Code Node



1: Generating a JWT with JavaScript


Our first node in this n8n workflow will be dedicated to generating this required token. We'll use the versatile Code node to run a small JavaScript snippet that creates the JWT for us.


  1. On the n8n canvas, click the + icon to add the first node to your workflow.


  1. In the "Search nodes..." dialog, type Code and select the Code node from the list. This node is incredibly powerful as it lets you run custom JavaScript or Python code at any point in your workflow.


  1. With the Code node selected, ensure the Language parameter is set to JavaScript.


  1. Next, copy the complete JavaScript snippet for generating the JWT and paste it into the JavaScript Code editor panel.
    (Paste your JavaScript snippet here in your article)


  1. The provided script is designed to perform the authentication logic and, most importantly, to return a JSON object. In n8n, the data returned by a node becomes its output, which can then be used as input by all subsequent nodes. In this case, the code will output an object containing our newly created token.


Code Node


To confirm everything is working correctly, click the Execute Node button at the bottom of the parameters panel. You should see a successful run and, in the Output tab on the right, a JSON object containing a jwt_token property.


Code Sample for JSON Web Token (JWT) Generation in JavaScript


const crypto = require('crypto');
const apiKey = 'REPLCE_WITH_YOUR_API_KEY';
const apiSecret = 'REPLCE_WITH_YOUR_SECRET';
const workspace = 'REPLCE_WITH_YOUR_WORKSPACE_IDENTIFIER';


// 1. Create the JWT Header and Payload
const header = {
alg: 'HS256',
typ: 'JWT'
};

const payload = {
iss: apiKey,
sub: workspace,
exp: Math.floor(Date.now() / 1000) + (1 * 60) // Token expires in 60 second
};

// Helper function to Base64Url encode data
const base64UrlEncode = (data) => {
return Buffer.from(JSON.stringify(data))
.toString('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');
};

const encodedHeader = base64UrlEncode(header);
const encodedPayload = base64UrlEncode(payload);

// 2. Create the signature using the correct crypto function
// This is what the Crypto node was failing to do.
const signatureInput = `${encodedHeader}.${encodedPayload}`;

// The 'crypto' module is globally available in n8n Function nodes
const signature = crypto
.createHmac('sha256', apiSecret)
.update(signatureInput)
.digest('base64')
.replace(/=/g, '')
.replace(/\+/g, '-')
.replace(/\//g, '_');

// 3. Combine the parts into the final JWT
const finalJwt = `${signatureInput}.${signature}`;

// 4. Return the result for the next n8n node to use
return [{
json: {
jwt: finalJwt
}
}];


Security Warning: Hardcoding API credentials directly in the code, as done here for illustrative purposes, creates a significant security vulnerability. In production, always store credentials securely.


HTTP request



2: Making the HTTP Request to Generate the PDF


Now that our workflow can generate an authentication token, we need to use it to make a successful API call. This is where the HTTP Request node comes in. We will configure this node to send our document data and our JWT to the PDF Generator API, which will then return the finished PDF file.




  1. Click the + icon on the Code node you just created to add the second step to the workflow.


  1. Search for and select the HTTP Request node.


  1. Now, we need to configure the node's parameters:



  1. Next, we'll handle authentication. This is the most critical step.


  • Under the Authentication dropdown, select None.
  • Switch the Send Headers toggle.
  • In the Name field, enter Authorization. This is the standard header for sending authentication credentials.
  • In the Value field, we need to dynamically insert the token from our first node.
  • Click the "Expressions" icon (</>).
      • In the expression editor, type Bearer (with a space at the end).
      • After the space, drag the jwt_token value from the Input Data > JSON section on the left. This links the output of the first node to the input of the second.
      • The final expression should look like this: Bearer {{$json["jwt_token"]}}
      • This tells n8n to construct an Authorization header with the word "Bearer" followed by the token generated in the Previous Step.


The Content-Type: application/json header is included to inform the API that the data we are sending in the request's body is structured in JSON format, ensuring the server can correctly interpret it.



Finally, we need to specify the document we want to create.


At the bottom of the node, click on the Body tab.
Set the Body Content Type to JSON.
In the Body field, paste the JSON payload that defines your document. This includes the template ID and the data you want to merge.


JSON Payload



Example JSON payload


{
"template": {
"id": "REPLACE_WITH_TEMPLATE_ID",
"data": {
"id": 123,
"name": "John Smith",
"birthdate": "2000-01-01",
"role": "Developer"
}
},
"format": "pdf",
"output": "url",
"name": "My First Document from PDFGeneratorAPI",
"testing": false
}


The current implementation uses a static JSON payload for demonstration purposes. In a production environment, this will be replaced with a dynamic data object populated by values from earlier steps in the automation.


Executing the Workflow and Viewing Your PDF


With both the Code node and the HTTP Request node fully configured, your automated document generation workflow is now complete. It's time to run the entire process from start to finish and view the generated document.


Click the Execute Workflow button located in the bottom-centre of the n8n canvas.


You will see n8n execute each node in sequence. First, the Code node will run, generating the JWT. Then, the HTTP Request node will take that token and the JSON data, make the POST request to the PDF Generator API, and receive a response. If everything was configured correctly, both nodes will have a green border, indicating a successful run.



API Response


By using the output=url parameter, the HTTP request prompts the API to respond with a public URL instead of the raw file data. This URL can be opened directly in your browser to view the generated document.





Updated on: 12/06/2025

Was this article helpful?

Share your feedback

Cancel

Thank you!