Removes: export as pdf from fastapi
This commit is contained in:
parent
7aeed5f68a
commit
13f21d6b29
5 changed files with 1 additions and 127 deletions
|
|
@ -6,7 +6,7 @@ import { startFastApiServer, startNextJsServer } from "./utils/servers";
|
|||
import { ChildProcessByStdio } from "child_process";
|
||||
import { baseDir, fastapiDir, isDev, localhost, nextjsDir, tempDir, userConfigPath, userDataDir } from "./utils/constants";
|
||||
import { setupIpcHandlers } from "./ipc";
|
||||
import url from "url";
|
||||
|
||||
|
||||
var win: BrowserWindow | undefined;
|
||||
var fastApiProcess: ChildProcessByStdio<any, any, any> | undefined;
|
||||
|
|
@ -34,7 +34,6 @@ async function startServers(fastApiPort: number, nextjsPort: number) {
|
|||
{
|
||||
DEBUG: isDev ? "True" : "False",
|
||||
LLM: process.env.LLM,
|
||||
LIBREOFFICE: process.env.LIBREOFFICE,
|
||||
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
|
||||
GOOGLE_API_KEY: process.env.GOOGLE_API_KEY,
|
||||
APP_DATA_DIRECTORY: userDataDir,
|
||||
|
|
|
|||
1
app/types/index.d.ts
vendored
1
app/types/index.d.ts
vendored
|
|
@ -1,7 +1,6 @@
|
|||
interface FastApiEnv {
|
||||
DEBUG?: string,
|
||||
LLM?: string,
|
||||
LIBREOFFICE?: string,
|
||||
OPENAI_API_KEY?: string,
|
||||
GOOGLE_API_KEY?: string,
|
||||
APP_DATA_DIRECTORY?: string,
|
||||
|
|
|
|||
|
|
@ -1,70 +0,0 @@
|
|||
import os
|
||||
import uuid
|
||||
|
||||
from api.models import LogMetadata
|
||||
from api.routers.presentation.mixins.fetch_presentation_assets import (
|
||||
FetchPresentationAssetsMixin,
|
||||
)
|
||||
from api.routers.presentation.models import (
|
||||
ExportAsRequest,
|
||||
PresentationAndPath,
|
||||
)
|
||||
from api.services.database import get_sql_session
|
||||
from api.services.instances import temp_file_service
|
||||
from api.services.logging import LoggingService
|
||||
from api.sql_models import PresentationSqlModel
|
||||
from api.utils import get_presentation_dir
|
||||
from image_processor.image_from_pptx import get_pdf_from_pptx
|
||||
from ppt_generator.pptx_presentation_creator import PptxPresentationCreator
|
||||
|
||||
|
||||
class ExportAsPDFHandler(FetchPresentationAssetsMixin):
|
||||
|
||||
def __init__(self, data: ExportAsRequest):
|
||||
self.data = data
|
||||
|
||||
self.session = str(uuid.uuid4())
|
||||
self.temp_dir = temp_file_service.create_temp_dir(self.session)
|
||||
|
||||
self.presentation_dir = get_presentation_dir(self.data.presentation_id)
|
||||
|
||||
def __del__(self):
|
||||
temp_file_service.cleanup_temp_dir(self.temp_dir)
|
||||
|
||||
async def post(self, logging_service: LoggingService, log_metadata: LogMetadata):
|
||||
logging_service.logger.info(
|
||||
logging_service.message(self.data.model_dump(mode="json")),
|
||||
extra=log_metadata.model_dump(),
|
||||
)
|
||||
|
||||
await self.fetch_presentation_assets()
|
||||
|
||||
with get_sql_session() as sql_session:
|
||||
presentation = sql_session.get(
|
||||
PresentationSqlModel, self.data.presentation_id
|
||||
)
|
||||
|
||||
ppt_path = os.path.join(
|
||||
self.presentation_dir,
|
||||
f"""{presentation.title.replace('/', '_').replace(' ', '_').replace('"', "'")}.pptx""",
|
||||
)
|
||||
|
||||
ppt_creator = PptxPresentationCreator(self.data.pptx_model, self.temp_dir)
|
||||
ppt_creator.create_ppt()
|
||||
ppt_creator.save(ppt_path)
|
||||
|
||||
print(ppt_path)
|
||||
|
||||
pdf_path = get_pdf_from_pptx(ppt_path, self.presentation_dir)
|
||||
|
||||
response = PresentationAndPath(
|
||||
presentation_id=self.data.presentation_id,
|
||||
path=pdf_path,
|
||||
)
|
||||
|
||||
logging_service.logger.info(
|
||||
logging_service.message(response.model_dump()),
|
||||
extra=log_metadata.model_dump(),
|
||||
)
|
||||
|
||||
return response
|
||||
|
|
@ -12,7 +12,6 @@ from api.routers.presentation.handlers.delete_presentation import (
|
|||
)
|
||||
from api.routers.presentation.handlers.delete_slide import DeleteSlideHandler
|
||||
from api.routers.presentation.handlers.edit import PresentationEditHandler
|
||||
from api.routers.presentation.handlers.export_as_pdf import ExportAsPDFHandler
|
||||
from api.routers.presentation.handlers.export_as_pptx import ExportAsPptxHandler
|
||||
from api.routers.presentation.handlers.generate_data import (
|
||||
PresentationGenerateDataHandler,
|
||||
|
|
@ -60,7 +59,6 @@ from api.routers.presentation.models import (
|
|||
PresentationAndPaths,
|
||||
PresentationAndSlides,
|
||||
GenerateTitleRequest,
|
||||
PresentationAndUrl,
|
||||
PresentationAndUrls,
|
||||
PresentationGenerateRequest,
|
||||
SearchIconRequest,
|
||||
|
|
@ -301,19 +299,6 @@ async def export_as_pptx(data: ExportAsRequest):
|
|||
)
|
||||
|
||||
|
||||
@presentation_router.post(
|
||||
"/presentation/export_as_pdf", response_model=PresentationAndPath
|
||||
)
|
||||
async def export_as_pdf(data: ExportAsRequest):
|
||||
request_utils = RequestUtils("/ppt/presentation/export_as_pdf")
|
||||
logging_service, log_metadata = await request_utils.initialize_logger(
|
||||
presentation_id=data.presentation_id,
|
||||
)
|
||||
return await handle_errors(
|
||||
ExportAsPDFHandler(data).post, logging_service, log_metadata
|
||||
)
|
||||
|
||||
|
||||
@presentation_router.delete("/delete", status_code=204)
|
||||
async def delete_presentation(presentation_id: str):
|
||||
request_utils = RequestUtils("/ppt/delete")
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
import io
|
||||
import os
|
||||
import subprocess
|
||||
from typing import List
|
||||
|
||||
from image_processor.utils import get_page_images_from_pdf
|
||||
|
||||
|
||||
def get_pdf_from_pptx(pptx_path: str, temp_dir: str) -> str:
|
||||
base_name = os.path.splitext(os.path.basename(pptx_path))[0]
|
||||
pdf_filename = f"{base_name}.pdf"
|
||||
|
||||
pdf_path = os.path.join(temp_dir, pdf_filename)
|
||||
|
||||
retry_count = 0
|
||||
while not os.path.exists(pdf_path):
|
||||
result = subprocess.run(
|
||||
f'{os.getenv("LIBREOFFICE")} --convert-to pdf "{pptx_path}" --outdir "{temp_dir}"',
|
||||
shell=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
print("LibreOffice stdout:", result.stdout)
|
||||
print("LibreOffice stderr:", result.stderr)
|
||||
|
||||
retry_count += 1
|
||||
if retry_count > 3:
|
||||
raise Exception("Failed to convert PPTX to PDF")
|
||||
|
||||
pdf_path = os.path.join(temp_dir, pdf_filename)
|
||||
|
||||
return pdf_path
|
||||
|
||||
|
||||
def get_images_from_pptx(pptx_path: str) -> List[str]:
|
||||
temp_dir = os.path.dirname(pptx_path)
|
||||
pdf_path = get_pdf_from_pptx(pptx_path, temp_dir)
|
||||
|
||||
return get_page_images_from_pdf(pdf_path, temp_dir)
|
||||
Loading…
Add table
Reference in a new issue