Coder Social home page Coder Social logo

spring-boot-pdf-generator's Introduction

PDF Generator

spring-boot-pdf-generator

Application to create a PDF using an API. This can be used as reference for creating PDF documents using the iText java library.

API Documentation

openapi: 3.0.1
info:
  title: PDF Generator
  description: Application to create a PDF using an API
  contact:
    name: Ewantha Udagama
    url: https://github.com/Ewantha
    email: [email protected]
  version: 0.0.2-SNAPSHOT
externalDocs:
  description: itext Website
  url: https://itextpdf.com/
servers:
  - url: http://localhost:8080
    description: Generated server url
paths:
  /document/pdf:
    post:
      tags:
        - document-controller
      summary: Create a PDF Document
      operationId: generateDocument
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateDocumentRequest'
        required: true
      responses:
        "201":
          description: Created
components:
  schemas:
    CreateDocumentRequest:
      required:
        - DocumentName
      type: object
      properties:
        DocumentName:
          type: string
          description: Name of the PDF document
          example: HelloWorld
        Content:
          type: array
          items:
            anyOf:
              - $ref: '#/components/schemas/DocumentContentParagraph'
              - $ref: '#/components/schemas/DocumentContentAnchor'
              - $ref: '#/components/schemas/DocumentContentImage'
              - $ref: '#/components/schemas/DocumentContentTable'
              - $ref: '#/components/schemas/DocumentContentWhitespace'
    DocumentContentParagraph:
      required:
        - Type
      type: object
      properties:
        Type:
          type: string
          description: Type of document element to be created.
          enum:
            - Paragraph
        Font:
          $ref: '#/components/schemas/ElementFont'
        Text:
          type: string
          description: Input text.
          example: Lorem ipsum...
    DocumentContentAnchor:
      required:
        - Type
      type: object
      properties:
        Type:
          type: string
          description: Type of document element to be created.
          enum:
            - Anchor
        Font:
          $ref: '#/components/schemas/ElementFont'
        Text:
          type: string
          description: Input text.
          example: Lorem ipsum...
        Reference:
          type: string
          description: Only for anchor elements and image elements. Set url or image
            source.
          example: www.google.com
    DocumentContentImage:
      required:
        - Type
      type: object
      properties:
        Type:
          type: string
          description: Type of document element to be created.
          enum:
            - Image
        Reference:
          type: string
          description: Only for anchor elements and image elements. Set url or image
            source.
          example: www.google.com
        Scale:
          type: number
          description: Only for image and table elements.
          format: float
          example: 1.2
    DocumentContentTable:
      required:
        - Type
      type: object
      properties:
        Type:
          type: string
          description: Type of document element to be created.
          enum:
            - Table
        Font:
          $ref: '#/components/schemas/ElementFont'
        Scale:
          type: number
          description: Only for image and table elements.
          format: float
          example: 1.2
        Columns:
          type: integer
          description: Only for table elements.
          format: int32
          example: 2
        Data:
          type: array
          description: Only for table elements.
          items:
            type: string
            description: Only for table elements.
        Header:
          type: array
          description: Only for table elements.
          items:
            type: string
            description: Only for table elements.
        HeaderFont:
          $ref: '#/components/schemas/ElementFont'
    DocumentContentWhitespace:
      required:
        - Type
      type: object
      properties:
        Type:
          type: string
          description: Type of document element to be created.
          enum:
            - Whitespace
    DocumentFontColor:
      type: object
      properties:
        Red:
          maximum: 255
          minimum: 0
          type: integer
          format: int32
          default: 0
        Green:
          maximum: 255
          minimum: 0
          type: integer
          format: int32
          default: 0
        Blue:
          maximum: 255
          minimum: 0
          type: integer
          format: int32
          default: 0
    ElementFont:
      type: object
      properties:
        Type:
          type: string
          description: Font type
          default: Helvetica
          enum:
            - Courier
            - Courier-Bold
            - Courier-Oblique
            - Courier-BoldOblique
            - Helvetica
            - Helvetica-Bold
            - Helvetica-Oblique
            - Helvetica-BoldOblique
            - Symbol
            - Times
            - Times-Roman
            - Times-Bold
            - Times-Italic
            - Times-BoldItalic
            - ZapfDingbats
        Size:
          type: integer
          description: Font size
          format: int32
          default: 12
        Style:
          type: integer
          description: "Font style. Normal:0, Bold:1, Italic:2, Underline:4, Strikethrough:8,\
            \ Bold-Italic:3"
          format: int32
          default: 0
        Color:
          $ref: '#/components/schemas/DocumentFontColor'
      description: Only for table elements.

Open API Docs

Swagger UI

Getting Started

Documents will be created at ${localPath.pdf}

Sample curl request to create a PDF document.

curl --location --request POST 'http://localhost:8080/document/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
    "DocumentName": "Document1",
    "Content": []
}'

Fill the Content json array with the required elements given below.

Each PDF element is added using a JSON object via the API

Content

  • Paragraph
{
  "Type": "Paragraph",
  "Font": {
    "Type": "Helvetica",
    "Size": 12,
    "Style": 0,
    "Color": {
      "Red": 0,
      "Green": 0,
      "Blue": 0
    }
  },
  "Text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum commodo enim id ex aliquam, vel finibus lectus sollicitudin. Curabitur varius nec erat sit amet lobortis. Suspendisse sollicitudin ut massa a euismod. Etiam auctor augue vel urna commodo, vel posuere ipsum imperdiet. Suspendisse lorem nulla, interdum ut erat molestie, dapibus mattis metus. Donec finibus lorem erat, at egestas neque facilisis id. Vestibulum ac erat maximus, placerat mi vel, maximus elit. Praesent quis dictum ex, quis rutrum elit. Maecenas ullamcorper quam quam, ac ullamcorper sapien condimentum in. Phasellus nec laoreet nulla. Fusce dignissim, magna in cursus porttitor, ligula dui volutpat dui, id pulvinar erat odio sit amet nisi. Vestibulum luctus sit amet lorem consequat mollis. Suspendisse ornare bibendum diam, vel volutpat augue mollis nec. Duis maximus libero et mi scelerisque, ut volutpat tellus ullamcorper."
}
  • Anchor
{
  "Type": "Anchor",
  "Font": {
    "Type": "Helvetica",
    "Size": 12,
    "Style": 0,
    "Color": {
      "Red": 0,
      "Green": 0,
      "Blue": 0
    }
  },
  "Text": "Lorem ipsum...",
  "Reference": "www.google.com"
}
  • Image
{
  "Type": "Image",
  "Reference": "www.google.com",
  "Scale": 1.2
}
  • Table
{
  "Type": "Table",
  "Font": {
    "Type": "Helvetica",
    "Size": 12,
    "Style": 0,
    "Color": {
      "Red": 0,
      "Green": 0,
      "Blue": 0
    }
  },
  "Scale": 1.2,
  "Columns": 2,
  "Data": [
    "string"
  ],
  "Header": [
    "string"
  ],
  "HeaderFont": {
    "Type": "Helvetica",
    "Size": 12,
    "Style": 0,
    "Color": {
      "Red": 0,
      "Green": 0,
      "Blue": 0
    }
  }
}
  • Whitespace
{
  "Type": "Whitespace"
}

Example

  1. Create a simple document with a heading and two paragraphs.
curl --location --request POST 'http://localhost:8080/document/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
    "DocumentName": "Document1",
    "Content": [
        {
            "Type": "Paragraph",
            "Font": {
                "Type": "Helvetica-Bold",
                "Size": 24,
                "Style": 4,
                "Color": {
                    "Red": 0,
                    "Green": 0,
                    "Blue": 0
                }
            },
            "Text": "Title"
        },
        {
            "Type": "Whitespace"
        },
        {
            "Type": "Paragraph",
            "Font": {
                "Type": "Helvetica",
                "Size": 12,
                "Style": 0,
                "Color": {
                    "Red": 0,
                    "Green": 0,
                    "Blue": 0
                }
            },
            "Text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum commodo enim id ex aliquam, vel finibus lectus sollicitudin. Curabitur varius nec erat sit amet lobortis. Suspendisse sollicitudin ut massa a euismod. Etiam auctor augue vel urna commodo, vel posuere ipsum imperdiet. Suspendisse lorem nulla, interdum ut erat molestie, dapibus mattis metus. Donec finibus lorem erat, at egestas neque facilisis id. Vestibulum ac erat maximus, placerat mi vel, maximus elit. Praesent quis dictum ex, quis rutrum elit. Maecenas ullamcorper quam quam, ac ullamcorper sapien condimentum in. Phasellus nec laoreet nulla. Fusce dignissim, magna in cursus porttitor, ligula dui volutpat dui, id pulvinar erat odio sit amet nisi. Vestibulum luctus sit amet lorem consequat mollis. Suspendisse ornare bibendum diam, vel volutpat augue mollis nec. Duis maximus libero et mi scelerisque, ut volutpat tellus ullamcorper."
        },
        {
            "Type": "Whitespace"
        },
        {
            "Type": "Paragraph",
            "Font": {
                "Type": "Helvetica",
                "Size": 12,
                "Style": 0,
                "Color": {
                    "Red": 0,
                    "Green": 0,
                    "Blue": 0
                }
            },
            "Text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum commodo enim id ex aliquam, vel finibus lectus sollicitudin. Curabitur varius nec erat sit amet lobortis. Suspendisse sollicitudin ut massa a euismod. Etiam auctor augue vel urna commodo, vel posuere ipsum imperdiet. Suspendisse lorem nulla, interdum ut erat molestie, dapibus mattis metus. Donec finibus lorem erat, at egestas neque facilisis id. Vestibulum ac erat maximus, placerat mi vel, maximus elit. Praesent quis dictum ex, quis rutrum elit. Maecenas ullamcorper quam quam, ac ullamcorper sapien condimentum in. Phasellus nec laoreet nulla. Fusce dignissim, magna in cursus porttitor, ligula dui volutpat dui, id pulvinar erat odio sit amet nisi. Vestibulum luctus sit amet lorem consequat mollis. Suspendisse ornare bibendum diam, vel volutpat augue mollis nec. Duis maximus libero et mi scelerisque, ut volutpat tellus ullamcorper."
        }
    ]
}'

img.png

  1. Create a table
curl --location --request POST 'http://localhost:8080/document/pdf' \
--header 'Content-Type: application/json' \
--data-raw '{
    "DocumentName": "Document2",
    "Content": [
        {
            "Type": "Paragraph",
            "Font": {
                "Type": "Helvetica-Bold",
                "Size": 24,
                "Style": 4,
                "Color": {
                    "Red": 0,
                    "Green": 20,
                    "Blue": 160
                }
            },
            "Text": "Shoping Cart"
        },
        {
            "Type": "Whitespace"
        },
        {
            "Type": "Table",
            "Font": {
                "Type": "Helvetica",
                "Size": 12,
                "Style": 0,
                "Color": {
                    "Red": 0,
                    "Green": 0,
                    "Blue": 0
                }
            },
            "Scale": 80,
            "Columns": 2,
            "Data": [
                "Milk", "12.00",
                "Chocholate", "20.00"
            ],
            "Header": [
                "Item",
                "Price"
            ],
            "HeaderFont": {
                "Type": "Helvetica-Bold",
                "Size": 12,
                "Style": 0,
                "Color": {
                    "Red": 0,
                    "Green": 0,
                    "Blue": 0
                }
            }
        }
    ]
}'

img_1.png

spring-boot-pdf-generator's People

Contributors

ewantha avatar ewanthaudagamaadl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.