2022-12-28 05:04:27 +00:00
|
|
|
import datetime
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from fastapi.responses import FileResponse
|
|
|
|
from pydantic import BaseModel
|
2023-01-05 03:18:29 +00:00
|
|
|
from typing import List
|
2022-12-28 05:04:27 +00:00
|
|
|
import pypandoc
|
|
|
|
import json
|
|
|
|
from fastapi.testclient import TestClient
|
|
|
|
import os
|
|
|
|
from wand.image import Image
|
|
|
|
from wand.color import Color
|
|
|
|
import shutil
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2022-12-28 05:04:27 +00:00
|
|
|
class DocumentSpecs(BaseModel):
|
|
|
|
format: str
|
|
|
|
style: str
|
|
|
|
linkcolor: str
|
|
|
|
tocdepth: int
|
|
|
|
pdfengine: str
|
|
|
|
content: str
|
|
|
|
fontsize: int
|
|
|
|
paperwidth: int
|
|
|
|
paperheight: int
|
2023-02-12 01:35:32 +00:00
|
|
|
ratio: int
|
2022-12-28 05:04:27 +00:00
|
|
|
margin: int
|
2023-01-01 19:16:56 +00:00
|
|
|
vmargin: int
|
2022-12-28 05:04:27 +00:00
|
|
|
extension: str
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
|
|
|
class FormatParameters(BaseModel):
|
|
|
|
linkcolor: str
|
|
|
|
tocdepth: int
|
|
|
|
pdfengine: str
|
|
|
|
fontsize: int
|
|
|
|
paperwidth: int
|
|
|
|
paperheight: int
|
2023-02-12 01:35:32 +00:00
|
|
|
ratio: int
|
2023-01-22 00:12:08 +00:00
|
|
|
margin: int
|
|
|
|
vmargin: int
|
|
|
|
extension: str
|
|
|
|
|
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
class Styles(BaseModel):
|
|
|
|
styles: List[str]
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
class Formats(BaseModel):
|
|
|
|
formats: List[str]
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
class App(BaseModel):
|
|
|
|
app: str
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2023-01-01 19:16:56 +00:00
|
|
|
def convert_pdf(filename, filetype, output_path, resolution=300):
|
2022-12-28 05:04:27 +00:00
|
|
|
""" Convert a PDF into images.
|
|
|
|
|
|
|
|
All the pages will give a single png file with format:
|
|
|
|
{pdf_filename}-{page_number}.png
|
|
|
|
|
|
|
|
The function removes the alpha channel from the image and
|
|
|
|
replace it with a white background.
|
|
|
|
"""
|
|
|
|
all_pages = Image(filename=filename, resolution=resolution)
|
|
|
|
for i, page in enumerate(all_pages.sequence):
|
|
|
|
with Image(page) as img:
|
2023-01-01 19:16:56 +00:00
|
|
|
img.format = filetype
|
2023-01-05 03:18:29 +00:00
|
|
|
img.background_color = Color('white')
|
|
|
|
img.alpha_channel = 'remove'
|
2022-12-28 05:04:27 +00:00
|
|
|
|
|
|
|
image_filename = os.path.splitext(os.path.basename(filename))[0]
|
2023-01-01 19:16:56 +00:00
|
|
|
image_filename = f'{image_filename}-{i}.{filetype}'
|
2022-12-28 05:04:27 +00:00
|
|
|
image_filename = os.path.join(output_path, image_filename)
|
|
|
|
|
|
|
|
img.save(filename=image_filename)
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2022-12-28 05:04:27 +00:00
|
|
|
app = FastAPI()
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
@app.get("/")
|
|
|
|
async def get_root():
|
|
|
|
app = App(app='fabriquedoc')
|
|
|
|
return app
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
|
|
|
# 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))]
|
|
|
|
|
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
@app.get("/styles/")
|
|
|
|
async def get_styles():
|
2023-01-22 00:12:08 +00:00
|
|
|
styles = Styles(styles=list_dir("./styles"))
|
2023-01-05 03:18:29 +00:00
|
|
|
return styles
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2023-01-05 03:18:29 +00:00
|
|
|
@app.get("/formats/{style}/")
|
|
|
|
async def get_formats(style: str):
|
2023-01-22 00:12:08 +00:00
|
|
|
formats = Formats(formats=list_dir(f"./styles/{style}/"))
|
2023-01-05 03:18:29 +00:00
|
|
|
return formats
|
2022-12-28 05:04:27 +00:00
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
|
2022-12-28 05:04:27 +00:00
|
|
|
@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'
|
2023-02-12 03:52:39 +00:00
|
|
|
datef = datetime.datetime.now().strftime("%Y-%m-%d")
|
2023-01-22 00:12:08 +00:00
|
|
|
os.makedirs("out", exist_ok=True)
|
2023-01-05 03:18:29 +00:00
|
|
|
output_file = f"./out/{specs.style}-{specs.format}-{datef}-output.pdf"
|
2023-02-12 03:52:39 +00:00
|
|
|
filters = ['latex-emoji.lua', 'centered.lua']
|
2022-12-28 05:04:27 +00:00
|
|
|
pdoc_args = [
|
|
|
|
f'--include-in-header={header_file}',
|
|
|
|
f'--include-after-body={cover_file}',
|
|
|
|
'--listings',
|
|
|
|
'--dpi=300',
|
|
|
|
f'--toc-depth={specs.tocdepth}',
|
|
|
|
f'--pdf-engine={specs.pdfengine}',
|
2023-01-22 00:12:08 +00:00
|
|
|
'-V', f'linkcolor={specs.linkcolor}',
|
|
|
|
'-V', f'fontsize={specs.fontsize}pt',
|
2023-02-12 01:35:32 +00:00
|
|
|
'-V', f'geometry:paperwidth={round(specs.paperwidth * specs.ratio / 100,-1) / 300}in',
|
2023-02-12 03:52:39 +00:00
|
|
|
'-V', f'geometry:paperheight={round(specs.paperheight * specs.ratio / 100,-1) / 300}in',
|
2023-02-12 01:35:32 +00:00
|
|
|
'-V', f'geometry:left={specs.margin / 300}in',
|
|
|
|
'-V', f'geometry:right={specs.margin / 300}in',
|
|
|
|
'-V', f'geometry:top={specs.vmargin / 300}in',
|
|
|
|
'-V', f'geometry:bottom={specs.vmargin / 300}in'
|
2022-12-28 05:04:27 +00:00
|
|
|
]
|
|
|
|
try:
|
|
|
|
logging.info("Dossier courant = " + os.getcwd())
|
|
|
|
|
|
|
|
result = pypandoc.convert_text(source=specs.content,
|
|
|
|
to='pdf',
|
2023-02-12 03:04:35 +00:00
|
|
|
format='markdown+implicit_figures+smart+emoji',
|
2022-12-28 05:04:27 +00:00
|
|
|
encoding='utf-8',
|
|
|
|
extra_args=pdoc_args,
|
|
|
|
filters=filters,
|
|
|
|
cworkdir=os.getcwd(),
|
|
|
|
outputfile=output_file
|
|
|
|
)
|
|
|
|
|
|
|
|
except RuntimeError as rerr:
|
|
|
|
logging.exception(rerr)
|
|
|
|
except OSError as oerr:
|
|
|
|
logging.exception(oerr)
|
2023-01-22 00:12:08 +00:00
|
|
|
if specs.extension in ["png", "jpg"]:
|
2023-02-12 03:04:35 +00:00
|
|
|
zip_filename = os.path.join("out", os.path.splitext(os.path.basename(output_file))[0])
|
|
|
|
png_output_dir = "./out/png_output"
|
2022-12-28 05:04:27 +00:00
|
|
|
if not os.path.exists(png_output_dir):
|
|
|
|
os.mkdir(png_output_dir)
|
|
|
|
try:
|
2023-02-12 03:04:35 +00:00
|
|
|
convert_pdf(output_file,
|
|
|
|
specs.extension,
|
|
|
|
png_output_dir,
|
|
|
|
resolution=300)
|
|
|
|
shutil.make_archive(base_name=zip_filename,
|
|
|
|
format='zip',
|
|
|
|
root_dir=png_output_dir)
|
2022-12-28 05:04:27 +00:00
|
|
|
shutil.rmtree(png_output_dir)
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception(e)
|
2023-01-22 00:12:08 +00:00
|
|
|
return FileResponse(zip_filename + ".zip")
|
|
|
|
elif specs.extension == "pdf":
|
2022-12-28 05:04:27 +00:00
|
|
|
return FileResponse(output_file)
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
|
2022-12-28 05:04:27 +00:00
|
|
|
client = TestClient(app)
|
|
|
|
|
|
|
|
|
2023-01-22 00:12:08 +00:00
|
|
|
def test_getroot():
|
|
|
|
response = client.get("/")
|
2022-12-28 05:04:27 +00:00
|
|
|
assert response.status_code == 200
|