ajout de paramètres de formats personnalisés et de endpoints pour les requêter
This commit is contained in:
parent
602bd5abcd
commit
ae2905f7d8
5 changed files with 296 additions and 16 deletions
69
main.py
69
main.py
|
@ -7,13 +7,13 @@ from pydantic import BaseModel
|
|||
from typing import List
|
||||
import pypandoc
|
||||
import json
|
||||
import httpx
|
||||
from fastapi.testclient import TestClient
|
||||
import os
|
||||
from wand.image import Image
|
||||
from wand.color import Color
|
||||
import shutil
|
||||
|
||||
|
||||
class DocumentSpecs(BaseModel):
|
||||
format: str
|
||||
style: str
|
||||
|
@ -28,15 +28,31 @@ class DocumentSpecs(BaseModel):
|
|||
vmargin: int
|
||||
extension: str
|
||||
|
||||
|
||||
class FormatParameters(BaseModel):
|
||||
linkcolor: str
|
||||
tocdepth: int
|
||||
pdfengine: str
|
||||
fontsize: int
|
||||
paperwidth: int
|
||||
paperheight: int
|
||||
margin: int
|
||||
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.
|
||||
|
||||
|
@ -59,29 +75,50 @@ def convert_pdf(filename, filetype, output_path, resolution=300):
|
|||
|
||||
img.save(filename=image_filename)
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def get_root():
|
||||
app = App(app='fabriquedoc')
|
||||
return app
|
||||
|
||||
|
||||
# function to list only directories inside given directory
|
||||
def list_dir(path):
|
||||
return [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]
|
||||
|
||||
|
||||
@app.get("/styles/")
|
||||
async def get_styles():
|
||||
styles = Styles(styles=os.listdir("./styles"))
|
||||
styles = Styles(styles=list_dir("./styles"))
|
||||
return styles
|
||||
|
||||
|
||||
@app.get("/formats/{style}/")
|
||||
async def get_formats(style: str):
|
||||
formats = Formats(formats=os.listdir(f"./styles/{style}/"))
|
||||
formats = Formats(formats=list_dir(f"./styles/{style}/"))
|
||||
return formats
|
||||
|
||||
|
||||
@app.get("/format_parameters/{style}/{format}/")
|
||||
async def get_format_parameters(style: str, format: str):
|
||||
# open styles/format_parameters.json as a dictionary
|
||||
with open(f"./styles/{style}/format_parameters.json", "r") as f:
|
||||
format_data = json.load(f).get(format)
|
||||
logging.log(logging.INFO, str(format_data))
|
||||
# load data from format_data into the FormatParameters object
|
||||
parameters = FormatParameters(**format_data)
|
||||
return parameters
|
||||
|
||||
|
||||
@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")
|
||||
os.makedirs("out",exist_ok=True)
|
||||
os.makedirs("out", exist_ok=True)
|
||||
output_file = f"./out/{specs.style}-{specs.format}-{datef}-output.pdf"
|
||||
filters = []
|
||||
pdoc_args = [
|
||||
|
@ -91,12 +128,12 @@ async def generer(specs: DocumentSpecs):
|
|||
'--dpi=300',
|
||||
f'--toc-depth={specs.tocdepth}',
|
||||
f'--pdf-engine={specs.pdfengine}',
|
||||
'-V',f'linkcolor={specs.linkcolor}',
|
||||
'-V',f'fontsize={specs.fontsize}pt',
|
||||
'-V',f'geometry:paperwidth={specs.paperwidth/300}in',
|
||||
'-V',f'geometry:paperheight={specs.paperheight/300}in',
|
||||
'-V',f'geometry:margin={specs.margin/300}in',
|
||||
'-V',f'geometry:vmargin={specs.vmargin/300}in'
|
||||
'-V', f'linkcolor={specs.linkcolor}',
|
||||
'-V', f'fontsize={specs.fontsize}pt',
|
||||
'-V', f'geometry:paperwidth={specs.paperwidth / 300}in',
|
||||
'-V', f'geometry:paperheight={specs.paperheight / 300}in',
|
||||
'-V', f'geometry:margin={specs.margin / 300}in',
|
||||
'-V', f'geometry:vmargin={specs.vmargin / 300}in'
|
||||
]
|
||||
try:
|
||||
logging.info("Dossier courant = " + os.getcwd())
|
||||
|
@ -115,7 +152,7 @@ async def generer(specs: DocumentSpecs):
|
|||
logging.exception(rerr)
|
||||
except OSError as oerr:
|
||||
logging.exception(oerr)
|
||||
if specs.extension in ["png","jpg"]:
|
||||
if specs.extension in ["png", "jpg"]:
|
||||
zip_filename = os.path.splitext(os.path.basename(output_file))[0]
|
||||
png_output_dir = "./png_output"
|
||||
if not os.path.exists(png_output_dir):
|
||||
|
@ -126,16 +163,16 @@ async def generer(specs: DocumentSpecs):
|
|||
shutil.rmtree(png_output_dir)
|
||||
except Exception as e:
|
||||
logging.exception(e)
|
||||
return FileResponse(zip_filename+".zip")
|
||||
elif specs.extension=="pdf":
|
||||
return FileResponse(zip_filename + ".zip")
|
||||
elif specs.extension == "pdf":
|
||||
return FileResponse(output_file)
|
||||
else:
|
||||
return 0
|
||||
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_generer():
|
||||
test_json = json.read("test.json")
|
||||
response = client.get("/generer/", json=test_json)
|
||||
def test_getroot():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200
|
||||
|
|
79
styles/jevalideca/format_parameters.json
Normal file
79
styles/jevalideca/format_parameters.json
Normal file
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"a4paper": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2480,
|
||||
"paperheight": 3507,
|
||||
"margin": 248,
|
||||
"vmargin": 350,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"lettre": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2550,
|
||||
"paperheight": 3300,
|
||||
"margin": 255,
|
||||
"vmargin": 330,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"linkedin": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1200,
|
||||
"paperheight": 1200,
|
||||
"margin": 120,
|
||||
"vmargin": 120,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"instagram": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1080,
|
||||
"paperheight": 1920,
|
||||
"margin": 108,
|
||||
"vmargin": 192,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"pinterest": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 735,
|
||||
"paperheight": 1102,
|
||||
"margin": 75,
|
||||
"vmargin": 110,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"slide43": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1920,
|
||||
"margin": 256,
|
||||
"vmargin": 192,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"slide169": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1440,
|
||||
"margin": 256,
|
||||
"vmargin": 144,
|
||||
"extension": "pdf"
|
||||
}
|
||||
}
|
79
styles/lcm/format_parameters.json
Normal file
79
styles/lcm/format_parameters.json
Normal file
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"a4paper": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2480,
|
||||
"paperheight": 3507,
|
||||
"margin": 248,
|
||||
"vmargin": 350,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"lettre": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2550,
|
||||
"paperheight": 3300,
|
||||
"margin": 255,
|
||||
"vmargin": 330,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"linkedin": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1200,
|
||||
"paperheight": 1200,
|
||||
"margin": 120,
|
||||
"vmargin": 120,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"instagram": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1080,
|
||||
"paperheight": 1920,
|
||||
"margin": 108,
|
||||
"vmargin": 192,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"pinterest": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 735,
|
||||
"paperheight": 1102,
|
||||
"margin": 75,
|
||||
"vmargin": 110,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"slide43": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1920,
|
||||
"margin": 256,
|
||||
"vmargin": 192,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"slide169": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1440,
|
||||
"margin": 256,
|
||||
"vmargin": 144,
|
||||
"extension": "pdf"
|
||||
}
|
||||
}
|
79
styles/missioncyber/format_parameters.json
Normal file
79
styles/missioncyber/format_parameters.json
Normal file
|
@ -0,0 +1,79 @@
|
|||
{
|
||||
"a4paper": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2480,
|
||||
"paperheight": 3507,
|
||||
"margin": 248,
|
||||
"vmargin": 350,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"lettre": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 3,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 12,
|
||||
"paperwidth": 2550,
|
||||
"paperheight": 3300,
|
||||
"margin": 255,
|
||||
"vmargin": 330,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"linkedin": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1200,
|
||||
"paperheight": 1200,
|
||||
"margin": 120,
|
||||
"vmargin": 120,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"instagram": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 1080,
|
||||
"paperheight": 1920,
|
||||
"margin": 108,
|
||||
"vmargin": 192,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"pinterest": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 735,
|
||||
"paperheight": 1102,
|
||||
"margin": 75,
|
||||
"vmargin": 110,
|
||||
"extension": "jpg"
|
||||
},
|
||||
"slide43": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1920,
|
||||
"margin": 256,
|
||||
"vmargin": 192,
|
||||
"extension": "pdf"
|
||||
},
|
||||
"slide169": {
|
||||
"linkcolor": "blue",
|
||||
"tocdepth": 2,
|
||||
"pdfengine": "lualatex",
|
||||
"fontsize": 16,
|
||||
"paperwidth": 2560,
|
||||
"paperheight": 1440,
|
||||
"margin": 256,
|
||||
"vmargin": 144,
|
||||
"extension": "pdf"
|
||||
}
|
||||
}
|
|
@ -78,3 +78,9 @@ Accept: application/pdf
|
|||
"margin": 90,
|
||||
"extension": "jpg"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
GET http://127.0.0.1:8000/format_parameters/jevalideca/slide169/
|
||||
Content-Type: application/json
|
||||
Accept: application/pdf
|
Loading…
Reference in a new issue