Transforming Web Content to Images and PDFs with magpai-app/cog-puppeteer Actions

When developing applications that require the conversion of HTML content or URLs into image or PDF formats, the magpai-app/cog-puppeteer Cognitive Actions provide a powerful solution. These pre-built actions allow developers to easily process web content, making it a breeze to create visual representations of online information. With customizable dimensions and output formats, these actions can streamline your workflow and enhance your applications.
Prerequisites
Before you begin integrating the Cognitive Actions, ensure you have:
- An API key for the Cognitive Actions platform.
- A basic understanding of making API calls.
Authentication typically involves passing your API key in the request headers, which ensures secure access to the Cognitive Actions.
Cognitive Actions Overview
Process HTML or URL to Image or PDF
The Process HTML or URL to Image or PDF action is designed to convert HTML content or a URL into either an image or PDF format. This action supports various output formats (png, jpeg, and pdf), allowing for flexible use cases, from generating thumbnails to complete document conversions.
Input
The input schema for this action requires several fields:
- data: Specifies the HTML content or URL to process.
- width: The width in pixels for the output (default is 512).
- height: The height in pixels for the output (default is 512).
- pageMode: Indicates whether the input data is in 'html' or 'url' form (default is 'html').
- pdfPageSize: Sets the page size for PDF outputs (default is 'A4').
- outputFormat: Determines the file format for the output (default is 'png').
Example Input:
{
"width": 512,
"height": 512,
"outputFormat": "png"
}
Output
The action typically returns a URL pointing to the generated image or PDF.
Example Output:
{
"image": "https://assets.cognitiveactions.com/invocations/b8536d72-e603-4414-a239-27b509e5032e/21104bef-3728-409a-b84c-499b4fd1fb21.png"
}
Conceptual Usage Example (Python)
Here's an illustrative Python snippet demonstrating how to call this action:
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 = "1ec39921-ca11-4387-afd1-f622c7ef3810" # Action ID for Process HTML or URL to Image or PDF
# Construct the input payload based on the action's requirements
payload = {
"data": "https://example.com", # Example URL
"width": 512,
"height": 512,
"outputFormat": "png"
}
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 example, you'll replace the YOUR_COGNITIVE_ACTIONS_API_KEY and COGNITIVE_ACTIONS_EXECUTE_URL with your actual API key and endpoint. Notice how the input payload is structured to meet the action's requirements.
Conclusion
The magpai-app/cog-puppeteer Cognitive Action for converting HTML content and URLs into image and PDF formats offers a robust solution for developers looking to enhance their applications with document processing capabilities. By utilizing these actions, you can automate workflows, improve user experience, and create visually appealing content with ease. Explore the potential of these actions and consider how they can fit into your next project!