API Overview

The PDF Converter service offers a set of APIs for converting HTML to PDF, deleting PDFs, and authorizing users via JWT authentication. Below is a detailed description of each API, including request parameters, response formats, and examples.

Check

HTML to PDF Conversion

Convert any HTML content to PDF with high fidelity, preserving the layout, fonts, and styles.

Check

Customization Options

Customize your PDFs with various options like font size, font family, page format, and margins.

Check

Base64 Encoding

Get the PDF as a base64 encoded string for easy storage and transmission.

Check

JWT Authentication

Secure your API endpoints with JWT-based authentication, ensuring only authorized users can access the service.

Check

IP Whitelisting

Restrict access to the API by allowing requests only from specified IP addresses.

Check

Serverless Deployment

Deployed on AWS Lambda using the Serverless Framework for scalability and cost efficiency.

Check

Support for External Styles

Apply external CSS styles to your HTML content for consistent and professional document formatting.


1. Authorize

/authorize
Method: POST
Description: Authenticates the user and generates a JWT token.

Request Parameters

  • username (required): The username of the user.
  • password (required): The password of the user.
{
    "username": "user",
    "password": "pass"
}

Example Response

{
    "statusCode": 200,
    "body": {
        "token": "eyJhbGciOiJIUzI1NiIsInR..."
    }
}

API Headers

To ensure a consistent and secure handling of CORS and headers, the following headers are set for each response:

{
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Headers": "Content-Type, Authorization",
    "Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE"
}

Authentication

All APIs except for /authorize require JWT authentication. The token should be included in the Authorization header as follows:

Authorization: Bearer <your-jwt-token>

2. Convert HTML to PDF

/convert
Method: POST
Description: Converts HTML content or a URL to a PDF document

Request Parameters

  • html (optional): The HTML content to convert.
  • htmlUrl (optional): A URL pointing to an HTML file to convert.
  • fontSize (optional): Font size to use in the PDF.
  • fontFamily (optional): Font family to use in the PDF.
  • topMargin (optional): Top margin for the PDF.
  • rightMargin (optional): Right margin for the PDF.
  • bottomMargin (optional): Bottom margin for the PDF.
  • leftMargin (optional): Left margin for the PDF.
  • pageFormat (optional): Page format (e.g., 'A4', 'Letter').
  • orientation (optional): Page orientation ('Portrait' or 'Landscape').

IP Whitelisting

To restrict access to the API by IP address, the IP addresses should be configured in the environment variables (ALLOWED_IPS). This ensures that only requests from specified IP addresses are processed.

Example cURL Requests

Convert HTML to PDF

curl -X POST https://your-api-endpoint/convert \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <your-jwt-token>" \
     -d '{
             "html": "<html><body><h1>Example</h1></body></html>",
             "fontSize": 12,
             "fontFamily": "Arial, sans-serif",
             "topMargin": "10mm",
             "rightMargin": "10mm",
             "bottomMargin": "10mm",
             "leftMargin": "10mm",
             "pageFormat": "A4",
             "orientation": "Portrait"
         }'
Response:
Success:
{
    "pdf": "<base64_encoded_pdf>"
}
Error:
{
    "message": "Error message here"
}

3. Delete PDF

/delete
Method: DELETE
Description: Deletes a PDF document. Requires JWT token for authentication.
Headers:
  • Authorization: Bearer
  • Request Body:
    {
        "fileName": "example.pdf"
    }
    Response:
    Success:
    {
        "message": "PDF deleted successfully"
    }
    Error:
    {
        "message": "Error message here"
    }

    Error Handling

    401 Unauthorized: The provided JWT token is invalid or missing.
    403 Forbidden: The request IP address is not in the allowed IP list.
    500 Internal Server Error: An error occurred on the server.