11 KiB
Convert HTML to PDF and Compress Files with CustomJS API
https://n8nworkflows.xyz/workflows/convert-html-to-pdf-and-compress-files-with-customjs-api-3869
Convert HTML to PDF and Compress Files with CustomJS API
1. Workflow Overview
This workflow demonstrates how to convert HTML content into a PDF file, compress the resulting PDF, and handle PDF files accessed via URLs by compressing them as well. It leverages the PDF Toolkit nodes from the CustomJS API, requiring a self-hosted n8n instance and a valid CustomJS API key.
The workflow is logically split into three main blocks:
-
1.1 Input Trigger and HTML to PDF Conversion: Starts the workflow manually, converts hardcoded HTML content into a PDF using the CustomJS HTML to PDF node.
-
1.2 PDF File Compression (from HTML Conversion): Compresses the PDF generated by the HTML to PDF conversion.
-
1.3 PDF Compression from URL: Accepts a PDF URL, fetches the PDF, and compresses it, enabling handling of existing PDF files from external sources.
2. Block-by-Block Analysis
2.1 Input Trigger and HTML to PDF Conversion
-
Overview:
This block initiates the workflow manually and converts a static HTML snippet into a PDF document. -
Nodes Involved:
- When clicking ‘Test workflow’ (Manual Trigger)
- HTML to PDF (CustomJS HTML to PDF node)
-
Node Details:
-
When clicking ‘Test workflow’
- Type: Manual Trigger
- Role: Starts workflow on user action.
- Configuration: Default manual trigger, no parameters required.
- Inputs: None (start node)
- Outputs: Connected to both HTML to PDF and Set PDF URL nodes.
- Edge Cases: None expected; user must manually trigger workflow.
-
HTML to PDF
- Type: CustomJS PDF Toolkit - HTML to PDF node
- Role: Converts given HTML string into a PDF binary.
- Configuration:
htmlInputparameter is set statically to<h1>Hello World</h1>.- Uses CustomJS API credentials for authentication.
- Inputs: Receives trigger from manual node.
- Outputs: Sends generated PDF binary data to Compress PDF file node.
- Edge Cases:
- API authentication failure if API key invalid or missing.
- HTML input too large or malformed could cause conversion errors or timeouts.
- Version Requirements: CustomJS PDF Toolkit nodes require self-hosted n8n and valid API key.
-
2.2 PDF File Compression (from HTML Conversion)
-
Overview:
Compresses the PDF generated from the HTML to PDF conversion, optimizing file size. -
Nodes Involved:
- Compress PDF file (CustomJS Compress PDF node)
-
Node Details:
- Compress PDF file
- Type: CustomJS PDF Toolkit - Compress PDF node
- Role: Compresses PDF data received from HTML to PDF node.
- Configuration: Default compression settings; no additional parameters specified.
- Inputs: Receives PDF binary data from HTML to PDF node.
- Outputs: End node (no further nodes connected).
- Credentials: Uses the same CustomJS API key credentials.
- Edge Cases:
- Compression API failure or timeout.
- Large PDF files may exceed API limits.
- Version Requirements: Same as above.
- Compress PDF file
2.3 PDF Compression from URL
-
Overview:
Handles compressing a PDF file accessible via a URL. First sets a PDF URL, then compresses the PDF retrieved from that URL. -
Nodes Involved:
- Set PDF URL (Code node)
- Compress PDF file1 (CustomJS Compress PDF node)
-
Node Details:
-
Set PDF URL
- Type: Code (JavaScript) node
- Role: Defines a JSON output containing a
pathkey with the PDF URL string. - Configuration:
- Hardcoded to return
{ "path": "https://www.nlbk.niedersachsen.de/download/164891/Test-pdf_3.pdf.pdf" }.
- Hardcoded to return
- Inputs: Triggered by manual trigger node.
- Outputs: JSON object with URL path to Compress PDF file1 node.
- Edge Cases:
- URL may be inaccessible or invalid, leading to failure in the next node.
- Version Requirements: n8n v0.146.0+ recommended for Code node v2.
-
Compress PDF file1
- Type: CustomJS PDF Toolkit - Compress PDF node
- Role: Compresses the PDF file located at the given URL.
- Configuration:
resourceparameter set to"url".field_nameparameter set dynamically via expression={{ $json.path }}, indicating the URL to fetch and compress.
- Inputs: Receives JSON with URL from Set PDF URL node.
- Outputs: End node.
- Credentials: Uses the same CustomJS API key.
- Edge Cases:
- Network failure or URL inaccessible.
- API errors during compression.
- Version Requirements: Same as above.
-
3. Summary Table
| Node Name | Node Type | Functional Role | Input Node(s) | Output Node(s) | Sticky Note |
|---|---|---|---|---|---|
| When clicking ‘Test workflow’ | Manual Trigger | Starts workflow manually | None | HTML to PDF, Set PDF URL | |
| HTML to PDF | CustomJS HTML to PDF | Converts HTML input to PDF | When clicking ‘Test workflow’ | Compress PDF file | ### HTML to PDF - Request HTML Data - Convert HTML to PDF |
| Compress PDF file | CustomJS Compress PDF | Compresses PDF from HTML to PDF conversion | HTML to PDF | None | ### Compress Pages from PDF - Compress PDF as a binary file. |
| Set PDF URL | Code | Outputs JSON with PDF URL | When clicking ‘Test workflow’ | Compress PDF file1 | ### Set PDF URL - Request PDF from URL. |
| Compress PDF file1 | CustomJS Compress PDF | Compresses PDF file from provided URL | Set PDF URL | None | ### Compress Pages from PDF - Compress PDF as a binary file. |
| Sticky Note | Sticky Note | Notes for HTML to PDF block | None | None | ### HTML to PDF - Request HTML Data - Convert HTML to PDF |
| Sticky Note1 | Sticky Note | Notes for compressing PDF pages | None | None | ### Compress Pages from PDF - Compress PDF as a binary file. |
| Sticky Note2 | Sticky Note | Notes for setting PDF URL | None | None | ### Set PDF URL - Request PDF from URL. |
| Sticky Note3 | Sticky Note | Notes for compressing PDF from URL block | None | None | ### Compress Pages from PDF - Compress PDF as a binary file. |
4. Reproducing the Workflow from Scratch
-
Create a Manual Trigger node
- Type: Manual Trigger
- Position: Starting point
- No parameters needed.
-
Add a CustomJS HTML to PDF node
- Node Name:
HTML to PDF - Type:
@custom-js/n8n-nodes-pdf-toolkit.html2Pdf - Parameters:
- Set
htmlInputfield to<h1>Hello World</h1>(or your desired HTML content).
- Set
- Credentials:
- Configure with your CustomJS API key credentials (create under Credentials > CustomJS API).
- Connect: Output of Manual Trigger → Input of HTML to PDF.
- Node Name:
-
Add a CustomJS Compress PDF node for local PDF compression
- Node Name:
Compress PDF file - Type:
@custom-js/n8n-nodes-pdf-toolkit.CompressPDF - Parameters: Default (compress the incoming binary PDF).
- Credentials: Use the same CustomJS API key credentials.
- Connect: Output of HTML to PDF → Input of Compress PDF file.
- Node Name:
-
Add a Code node to set the PDF URL
- Node Name:
Set PDF URL - Type: Code (JavaScript)
- Parameters: Insert the following JS code:
return { json: { path: "https://www.nlbk.niedersachsen.de/download/164891/Test-pdf_3.pdf.pdf" } }; - Connect: Output of Manual Trigger → Input of Set PDF URL.
- Node Name:
-
Add a CustomJS Compress PDF node for URL-based compression
- Node Name:
Compress PDF file1 - Type:
@custom-js/n8n-nodes-pdf-toolkit.CompressPDF - Parameters:
- Set
resourceto"url". - Set
field_nameto expression={{ $json.path }}(to use the URL from Code node).
- Set
- Credentials: Use the same CustomJS API key credentials.
- Connect: Output of Set PDF URL → Input of Compress PDF file1.
- Node Name:
-
Verify all nodes are correctly connected:
- Manual Trigger outputs to both HTML to PDF and Set PDF URL nodes.
- HTML to PDF outputs to Compress PDF file.
- Set PDF URL outputs to Compress PDF file1.
-
Set up CustomJS API Credentials:
- Go to Credentials in n8n.
- Create new credentials for CustomJS API.
- Paste your API key obtained from https://www.customjs.space (your profile page > Show API key).
- Assign these credentials to all CustomJS nodes.
-
Save and activate workflow.
- Test by clicking ‘Execute Workflow’ in the Manual Trigger node.
5. General Notes & Resources
| Note Content | Context or Link |
|---|---|
Community nodes like @custom-js/n8n-nodes-pdf-toolkit require self-hosted n8n instances. |
https://docs.n8n.io/integrations/community-nodes/ |
| Obtain CustomJS API key by signing up and checking your profile on https://www.customjs.space | https://www.customjs.space |
| This workflow can be triggered by other means, e.g., Webhook, and output adapted accordingly. | Replace Manual Trigger with Webhook and add response nodes. |
| For large files (>6MB), consider additional logic to handle timeouts or API limits in CustomJS. | Best practice for production workflows |
This document fully describes the workflow’s logic, nodes, configurations, and setup instructions to enable advanced users and AI agents to understand, reproduce, and maintain the workflow effectively.