Working on pdf creation
This commit is contained in:
@@ -4,8 +4,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
import magic # python-magic
|
import magic # python-magic
|
||||||
|
|
||||||
from tasks.common.pdf_converter import TextToPdfConverter, ImageToPdfConverter, WordToPdfConverter
|
|
||||||
|
|
||||||
|
|
||||||
class UnsupportedFileTypeError(Exception):
|
class UnsupportedFileTypeError(Exception):
|
||||||
"""Exception raised when a file type is not supported."""
|
"""Exception raised when a file type is not supported."""
|
||||||
@@ -74,29 +72,3 @@ def compress_pdf(input_pdf: str, output_pdf: str, quality: str = "ebook") -> Non
|
|||||||
raise RuntimeError(f"Ghostscript failed with return code {result.returncode}")
|
raise RuntimeError(f"Ghostscript failed with return code {result.returncode}")
|
||||||
|
|
||||||
|
|
||||||
def convert_to_pdf(filepath: str, output_dir: str = ".") -> str:
|
|
||||||
"""
|
|
||||||
Convert any supported file to PDF.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
filepath (str): Path to the input file.
|
|
||||||
output_dir (str): Directory to save the output PDF.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str: Path to the generated PDF.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
UnsupportedFileTypeError: If the input file type is not supported.
|
|
||||||
"""
|
|
||||||
file_type = detect_file_type(filepath)
|
|
||||||
|
|
||||||
if file_type == "text":
|
|
||||||
converter = TextToPdfConverter(filepath, output_dir=output_dir)
|
|
||||||
elif file_type == "image":
|
|
||||||
converter = ImageToPdfConverter(filepath, output_dir=output_dir)
|
|
||||||
elif file_type == "word":
|
|
||||||
converter = WordToPdfConverter(filepath, output_dir=output_dir)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unsupported file type: {file_type}")
|
|
||||||
|
|
||||||
return converter.convert()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user