ajout des endpoints styles et formats

This commit is contained in:
Francois Pelletier 2023-01-04 22:18:29 -05:00
parent bfa1f824ce
commit 060f453bbd
3 changed files with 30 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/.idea/
/fabriquedoc.iml
/out/

View file

@ -5,4 +5,4 @@
docker stop fabriquedoc
docker rm fabriquedoc
# Ce programme sert à lancer le job_dispatcher dans un docker localement pour tester
docker run -p 8080:8080 --name fabriquedoc --network host local/fabriquedoc
docker run -p 8000:8000 --name fabriquedoc --network host local/fabriquedoc

32
main.py
View file

@ -4,6 +4,7 @@ import logging
from fastapi import FastAPI
from fastapi.responses import FileResponse
from pydantic import BaseModel
from typing import List
import pypandoc
import json
import httpx
@ -13,7 +14,6 @@ from wand.image import Image
from wand.color import Color
import shutil
class DocumentSpecs(BaseModel):
format: str
style: str
@ -28,6 +28,15 @@ class DocumentSpecs(BaseModel):
vmargin: int
extension: str
class Styles(BaseModel):
styles: List[str]
class Formats(BaseModel):
formats: List[str]
class App(BaseModel):
app: str
def convert_pdf(filename, filetype, output_path, resolution=300):
""" Convert a PDF into images.
@ -41,8 +50,8 @@ def convert_pdf(filename, filetype, output_path, resolution=300):
for i, page in enumerate(all_pages.sequence):
with Image(page) as img:
img.format = filetype
img.background_color = Color('black')
#img.alpha_channel = 'remove'
img.background_color = Color('white')
img.alpha_channel = 'remove'
image_filename = os.path.splitext(os.path.basename(filename))[0]
image_filename = f'{image_filename}-{i}.{filetype}'
@ -52,13 +61,28 @@ def convert_pdf(filename, filetype, output_path, resolution=300):
app = FastAPI()
@app.get("/")
async def get_root():
app = App(app='fabriquedoc')
return app
@app.get("/styles/")
async def get_styles():
styles = Styles(styles=os.listdir("./styles"))
return styles
@app.get("/formats/{style}/")
async def get_formats(style: str):
formats = Formats(formats=os.listdir(f"./styles/{style}/"))
return formats
@app.get("/generer/")
async def generer(specs: DocumentSpecs):
header_file = f'{os.getcwd()}/styles/{specs.style}/{specs.format}/header.tex'
cover_file = f'{os.getcwd()}/styles/{specs.style}/{specs.format}/cover.tex'
datef = datetime.datetime.now().strftime("%m-%d-%Y")
output_file = f"./{specs.style}-{specs.format}-{datef}-output.pdf"
os.makedirs("out",exist_ok=True)
output_file = f"./out/{specs.style}-{specs.format}-{datef}-output.pdf"
filters = []
pdoc_args = [
f'--include-in-header={header_file}',