> ## Documentation Index
> Fetch the complete documentation index at: https://docs.htmldocs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Generate from HTML

> Generates a PDF document from raw HTML content and/or a URL. If both are provided, the HTML content will be injected into the page at the specified URL.

This endpoint allows you to generate a PDF document directly from HTML content, without needing to publish a template first.

For generating documents from published templates, see the [Generate Document](/api/generate-document) endpoint instead.


## OpenAPI

````yaml POST /api/generate
openapi: 3.0.1
info:
  title: htmldocs API
  description: API for generating PDF documents from JSX templates
  version: 1.0.0
servers:
  - url: https://htmldocs.com
security:
  - bearerAuth: []
paths:
  /api/generate:
    post:
      description: >-
        Generates a PDF document from raw HTML content and/or a URL. If both are
        provided, the HTML content will be injected into the page at the
        specified URL.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                html:
                  type: string
                  description: >-
                    HTML content to convert to PDF or inject into the page at
                    the specified URL
                url:
                  type: string
                  format: uri
                  description: >-
                    URL of the webpage to convert to PDF or use as a base for
                    HTML injection
                format:
                  type: string
                  enum:
                    - pdf
                    - base64
                    - json
                  description: >-
                    Response format. `pdf` returns a binary PDF file, `base64`
                    returns the PDF encoded as base64 in JSON, `json` returns a
                    URL to download the PDF. Defaults to `pdf`
                  default: pdf
                size:
                  $ref: '#/components/schemas/DocumentSize'
                orientation:
                  $ref: '#/components/schemas/Orientation'
              minProperties: 1
              anyOf:
                - required:
                    - html
                - required:
                    - url
      responses:
        '200':
          description: Generated document in requested format
          content:
            application/pdf:
              schema:
                type: string
                description: Binary PDF file (when format=pdf)
                format: binary
            application/json:
              schema:
                oneOf:
                  - type: object
                    description: Base64 encoded PDF (when format=base64)
                    properties:
                      data:
                        type: string
                        format: base64
                  - type: object
                    description: PDF URL (when format=json)
                    properties:
                      url:
                        type: string
                        format: uri
        '400':
          description: Invalid request body
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '422':
          description: Generation limit exceeded
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error'
components:
  schemas:
    DocumentSize:
      type: string
      description: Page size (A3, A4, A5, letter, legal, or custom size like '8.5in 11in')
    Orientation:
      type: string
      enum:
        - portrait
        - landscape
    Error:
      type: object
      required:
        - error
      properties:
        error:
          type: string
          description: Error message describing what went wrong
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: API-Key
      description: 'API key for authentication. Use format: Bearer <api-key>'

````