Create Stunning PDFs from Plain Text with EasyTeX Cognitive Actions

22 Apr 2025
Create Stunning PDFs from Plain Text with EasyTeX Cognitive Actions

In today's fast-paced digital world, converting text into professional-looking PDF documents is an essential task for many developers. The georgedavila/cog-easytex API offers a powerful solution through its Cognitive Actions, specifically designed for generating PDFs from minimally formatted plaintext using TeX. This service not only simplifies the document creation process but also allows for customization, making it an ideal tool for developers looking to enhance their applications with document generation capabilities.

Prerequisites

Before you start integrating the Cognitive Actions from the EasyTeX API, ensure you have the following:

  • API Key: You will need an API key from the Cognitive Actions platform to authenticate your requests.
  • Basic Setup: Familiarity with JSON and Python programming will help you effectively utilize these actions.

Authentication typically involves passing your API key in the headers of your HTTP requests, which will ensure secure access to the service.

Cognitive Actions Overview

Convert Text to PDF Using TeX

The Convert Text to PDF Using TeX action allows you to convert plain text into a beautifully formatted PDF document. This action supports various customizations, including the document title, author name, submission date, and the option for a separate title page, giving you flexibility in document creation.

Input

The input for this action requires a JSON object structured according to the following schema:

{
  "mainText": "#SECTION A \n Hello \n #SUBSECTION B \n Lorem ipsum...",
  "useTitle": true,
  "authorName": "George",
  "documentTitle": "Report",
  "submissionDate": "May 8th, 2024",
  "separateTitlePage": false
}
  • mainText: (string) The main content of the document formatted in sections. Defaults to example content.
  • useTitle: (boolean) Specifies if a title should be included in the document. Default is true.
  • authorName: (string) The author's name. Default is 'George'.
  • documentTitle: (string) The title of the document. Default is 'Report'.
  • submissionDate: (string) The submission date in the format 'Month day, year'. Default is 'May 8th, 2024'.
  • separateTitlePage: (boolean) Indicates if the title should appear on a separate page. Default is false.

Output

Upon successful execution, this action will return a URL to the generated PDF document. Here’s an example of the output you can expect:

https://assets.cognitiveactions.com/invocations/3b3e7a77-2dbd-4e27-963d-d742c470fc90/3be51abb-4192-4ee6-9dd9-d7a231f110ee.pdf

This URL can be used to access or download the created PDF document.

Conceptual Usage Example (Python)

Here’s how you can call the Convert Text to PDF Using TeX action using Python:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute" # Hypothetical endpoint

action_id = "9c1bfb6b-e6b4-489c-a5ec-aa1879c4cc29" # Action ID for Convert Text to PDF Using TeX

# Construct the input payload based on the action's requirements
payload = {
    "mainText": "#SECTION A \n Hello \n #SUBSECTION B \n Lorem ipsum...",
    "useTitle": True,
    "authorName": "George",
    "documentTitle": "Report",
    "submissionDate": "May 8th, 2024",
    "separateTitlePage": False
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload} # Hypothetical structure
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body: {e.response.text}")

In this code snippet, replace YOUR_COGNITIVE_ACTIONS_API_KEY with your actual API key. The action ID is specified for the PDF conversion action, and the input payload is structured accordingly. The example demonstrates how to handle the response and potential errors.

Conclusion

The georgedavila/cog-easytex Cognitive Actions provide a straightforward way to convert plain text into professional PDFs, with customizable options that cater to a range of document needs. By leveraging this API, developers can enhance their applications with powerful document generation capabilities, saving time and effort in the process. Consider exploring additional use cases, such as generating reports, summaries, or academic papers, to take full advantage of this versatile tool.