Ask AI

How to Save Text as a PDF Without External Services

If you want to convert plain text into a PDF file without relying on third-party APIs or external services, hereโ€™s a simple two-step solution using JavaScript nodes and the pdfkit library.


๐Ÿ”ง Step 1: Generate Base64-Encoded PDF from Text

First, create a JavaScript node and paste the following code.

/** @CustomParams
{
    "text_content": {
        "type": "string",
        "title": "Text Content",
        "description": "The text content to be included in the PDF file"
    }
}
*/

import PDFDocument from 'pdfkit';

export default async function run({ execution_id, input, data, store, db }) {
    const textContent = data.text_content;
    if (!textContent) {
        throw new Error('Text content is required.');
    }

    const doc = new PDFDocument();
    const chunks = [];

    doc.on('data', chunk => chunks.push(chunk));
    const endPromise = new Promise(resolve => {
        doc.on('end', () => {
            const pdfBuffer = Buffer.concat(chunks);
            const base64 = pdfBuffer.toString('base64');
            resolve({ base64 }); 
        });
    });

    doc.text(textContent);
    doc.end();

    return await endPromise;
}

Then click Generate params to create an input field for your text:

Notion image

This node will generate a base64 string that represents your text as a PDF document.

Notion image

๐Ÿ“„ Step 2: Convert Base64 into a BinaryPDF File

Create a second JavaScript node, paste the code below:

/** @CustomParams
{
    "base64_string": {
        "type": "string",
        "title": "Base64 String",
        "description": "Base64 encoded data to be written into a PDF file"
    }
}
*/

import fs from 'fs';
import { v4 as uuidv4 } from 'uuid';

export default async function run({ execution_id, input, data, store, db }) {
    try {
        // Generate a random file name with .pdf extension
        const fileName = `${uuidv4()}.pdf`;

        // Ensure base64 data is provided
        const base64Data = data.base64_string;
        if (!base64Data) {
            throw new Error('Base64 data is required.');
        }

        // Decode base64 data
        const binaryData = Buffer.from(base64Data, 'base64');

        // Write binary data to a PDF file
        fs.writeFileSync(fileName, binaryData);

        return {
            file: file(fileName)
        };
    } catch (error) {
        console.error(error.message);
        return {
            error: error.message
        };
    }
}

and also click Generate params to create a field for the base64 string

Then, insert the variable from the previous node (the base64 string) into the base64_string field.

Notion image

Run the node, and as a result youโ€™ll get a ready-to-use PDF file, which you can download or pass further into your automation flow.

Notion image
Notion image

โœ… Example Workflow

  1. Enter your desired text in the Text Content field (e.g., Hello, world!).
  1. Run the first node to get a base64 string.
  1. Paste that base64 string into the second node's Base64 String field.
  1. Run the second node and download the resulting .pdf file.

๐Ÿ”ฅ Thatโ€™s it! A simple, local solution with no external dependencies, no fees, and total control.

Did this answer your question?
๐Ÿ˜ž
๐Ÿ˜
๐Ÿคฉ