Git Product home page Git Product logo

fastdocx-api's Introduction

FastDocx-API

MIT License typedlib_mypy Ruff GitHub Actions Workflow Status GitHub Actions Workflow Status GitHub Actions Workflow Status codecov

A FastAPI based project to generate DOCX and PDF documents from a DOCX template, JSON data and images.

The project uses easy-template-x to fill the DOCX template: all tags and filters provided by this project are available.

Example

"LibreOffice to PDF"

System Requirements

Python 3, NodeJS and LibreOffice must be installed globally.

The Docker image will install all requirements when built.

Installation

Create and activate a virtual environment

python3 -m venv .venv
source .venv/bin/activate

Install requirements

pip install -r requirements.txt

Then setup the Node package with

cd app/scripts/
npm install

Running Locally

uvicorn app.main:app --reload

Swagger Documentation

Install and run locally then visit http://localhost:8000/docs.

Development Environment

For development and testing, install additional requirements

pip install -r requirements.dev.txt

Testing

Install the development dependencies, then run pytest from the project root.

Sample Commands

Use the sample files in app/tests/sample_data.

cd app/tests/sample_data

curl -X POST http://localhost:8000/api/v1/render/to_docx \
  -F 'json_data={"hello": "Bonjour", "world": "à tous"}' \
  -F 'docx_file=@./1_hello_world.docx' > result_1.docx

curl -X POST http://localhost:8000/api/v1/render/to_pdf \
  -F 'json_data={
    "sample_document_header": "FastDocxAPI sample document",
    "title": "FastDocxAPI - A Full Sample",
    "heading_1": "Style",
    "paragraph_1": "All Libreoffice features are available. Style is not modified by templating, and images can be included too!",
    "heading_2": "Lorem Ipsum",
    "paragraph_2": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    "table_first": [
        {
            "first_col": "Monday",
            "second_col": "Tuesday",
            "third_col": "Other..."
        },
        {
            "first_col": "Hello",
            "second_col": "World",
            "third_col": "Good Bye"
        },
        {
            "first_col": "Alice",
            "second_col": "Bob",
            "third_col": "Charlie"
        }
    ],
		"other_table": [
        {
            "value_1": "Something",
            "value_2": "to"
        },
        {
            "value_1": "test",
            "value_2": "here."
        }
    ],
		"simplelist": [
			{"item": "one"},
			{"item": "two"},
			{"item": "and more"}
		],
		"otherlist": [
			{"level_main": "Top", "sub_levels": [{"content": "a"}, {"content": "b"}]},
			{"level_main": "Not top", "sub_levels": [{"content": "fgh"}, {"content": "ijk"}]}
		]
}' \
  -F 'docx_file=@./2_all_features.docx' \
  -F 'image_files=@./image.jpg' \
  -F 'image_files=@./logo.png' > result_2.pdf

Docker

Build the image: this can take several minutes to install LibreOffice and NodeJS.

docker build -t fastdocxapi

Run with docker run -p 8000:80 fastdocxapi.

The latest build can be found on Docker Hub.

References

  1. FastAPI official docs
  2. easy-template-x repository
  3. LibreOffice website

fastdocx-api's People

Contributors

mbging avatar

Watchers

 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.