الميزات: إضافة أدوات جديدة لمعالجة ملفات PDF، تشمل التلخيص والترجمة واستخراج الجداول.
- تفعيل مكون SummarizePdf لإنشاء ملخصات PDF باستخدام الذكاء الاصطناعي. - تفعيل مكون TranslatePdf لترجمة محتوى PDF إلى لغات متعددة. - تفعيل مكون TableExtractor لاستخراج الجداول من ملفات PDF. - تحديث الصفحة الرئيسية والتوجيه ليشمل الأدوات الجديدة. - إضافة ترجمات للأدوات الجديدة باللغات الإنجليزية والعربية والفرنسية. - توسيع أنواع واجهة برمجة التطبيقات (API) لدعم الميزات الجديدة المتعلقة بمعالجة ملفات PDF. --feat: Initialize frontend with React, Vite, and Tailwind CSS - Set up main entry point for React application. - Create About, Home, NotFound, Privacy, and Terms pages with SEO support. - Implement API service for file uploads and task management. - Add global styles using Tailwind CSS. - Create utility functions for SEO and text processing. - Configure Vite for development and production builds. - Set up Nginx configuration for serving frontend and backend. - Add scripts for cleanup of expired files and sitemap generation. - Implement deployment script for production environment.
This commit is contained in:
90
backend/app/services/compress_image_service.py
Normal file
90
backend/app/services/compress_image_service.py
Normal file
@@ -0,0 +1,90 @@
|
||||
"""Image compression service using Pillow."""
|
||||
import os
|
||||
import logging
|
||||
|
||||
from PIL import Image
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CompressImageError(Exception):
|
||||
"""Custom exception for image compression failures."""
|
||||
pass
|
||||
|
||||
|
||||
FORMAT_MAP = {
|
||||
"jpg": "JPEG",
|
||||
"jpeg": "JPEG",
|
||||
"png": "PNG",
|
||||
"webp": "WEBP",
|
||||
}
|
||||
|
||||
|
||||
def compress_image(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
quality: int = 75,
|
||||
) -> dict:
|
||||
"""
|
||||
Compress an image by reducing quality and optimizing encoding.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input image
|
||||
output_path: Path for the compressed image
|
||||
quality: Output quality 1-100
|
||||
|
||||
Returns:
|
||||
dict with original_size, compressed_size, reduction_percent
|
||||
|
||||
Raises:
|
||||
CompressImageError: If compression fails
|
||||
"""
|
||||
quality = max(1, min(100, quality))
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
original_size = os.path.getsize(input_path)
|
||||
|
||||
with Image.open(input_path) as img:
|
||||
width, height = img.size
|
||||
ext = os.path.splitext(output_path)[1].lower().strip(".")
|
||||
pil_format = FORMAT_MAP.get(ext, "JPEG")
|
||||
|
||||
# Convert RGBA to RGB for JPEG
|
||||
if pil_format == "JPEG" and img.mode in ("RGBA", "P", "LA"):
|
||||
background = Image.new("RGB", img.size, (255, 255, 255))
|
||||
if img.mode == "P":
|
||||
img = img.convert("RGBA")
|
||||
background.paste(
|
||||
img, mask=img.split()[-1] if "A" in img.mode else None
|
||||
)
|
||||
img = background
|
||||
|
||||
save_kwargs = {"optimize": True}
|
||||
if pil_format in ("JPEG", "WEBP"):
|
||||
save_kwargs["quality"] = quality
|
||||
elif pil_format == "PNG":
|
||||
save_kwargs["compress_level"] = 9
|
||||
|
||||
img.save(output_path, format=pil_format, **save_kwargs)
|
||||
|
||||
compressed_size = os.path.getsize(output_path)
|
||||
reduction = round(
|
||||
(1 - compressed_size / original_size) * 100, 1
|
||||
) if original_size > 0 else 0
|
||||
|
||||
logger.info(
|
||||
f"Image compression: {original_size} → {compressed_size} "
|
||||
f"({reduction}% reduction)"
|
||||
)
|
||||
|
||||
return {
|
||||
"original_size": original_size,
|
||||
"compressed_size": compressed_size,
|
||||
"reduction_percent": reduction,
|
||||
"width": width,
|
||||
"height": height,
|
||||
}
|
||||
|
||||
except (IOError, OSError, Image.DecompressionBombError) as e:
|
||||
raise CompressImageError(f"Image compression failed: {str(e)}")
|
||||
84
backend/app/services/html_to_pdf_service.py
Normal file
84
backend/app/services/html_to_pdf_service.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""HTML to PDF conversion service."""
|
||||
import os
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class HtmlToPdfError(Exception):
|
||||
"""Custom exception for HTML to PDF conversion failures."""
|
||||
pass
|
||||
|
||||
|
||||
def html_to_pdf(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
) -> dict:
|
||||
"""
|
||||
Convert an HTML file to PDF.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input HTML file
|
||||
output_path: Path for the output PDF
|
||||
|
||||
Returns:
|
||||
dict with output_size
|
||||
|
||||
Raises:
|
||||
HtmlToPdfError: If conversion fails
|
||||
"""
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
from weasyprint import HTML
|
||||
|
||||
HTML(filename=input_path).write_pdf(output_path)
|
||||
|
||||
output_size = os.path.getsize(output_path)
|
||||
logger.info(f"HTML→PDF conversion completed ({output_size} bytes)")
|
||||
|
||||
return {
|
||||
"output_size": output_size,
|
||||
}
|
||||
|
||||
except ImportError:
|
||||
raise HtmlToPdfError("weasyprint library is not installed.")
|
||||
except Exception as e:
|
||||
raise HtmlToPdfError(f"Failed to convert HTML to PDF: {str(e)}")
|
||||
|
||||
|
||||
def html_string_to_pdf(
|
||||
html_content: str,
|
||||
output_path: str,
|
||||
) -> dict:
|
||||
"""
|
||||
Convert an HTML string to PDF.
|
||||
|
||||
Args:
|
||||
html_content: HTML content as string
|
||||
output_path: Path for the output PDF
|
||||
|
||||
Returns:
|
||||
dict with output_size
|
||||
|
||||
Raises:
|
||||
HtmlToPdfError: If conversion fails
|
||||
"""
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
from weasyprint import HTML
|
||||
|
||||
HTML(string=html_content).write_pdf(output_path)
|
||||
|
||||
output_size = os.path.getsize(output_path)
|
||||
logger.info(f"HTML string→PDF conversion completed ({output_size} bytes)")
|
||||
|
||||
return {
|
||||
"output_size": output_size,
|
||||
}
|
||||
|
||||
except ImportError:
|
||||
raise HtmlToPdfError("weasyprint library is not installed.")
|
||||
except Exception as e:
|
||||
raise HtmlToPdfError(f"Failed to convert HTML to PDF: {str(e)}")
|
||||
266
backend/app/services/pdf_ai_service.py
Normal file
266
backend/app/services/pdf_ai_service.py
Normal file
@@ -0,0 +1,266 @@
|
||||
"""PDF AI services — Chat, Summarize, Translate, Table Extract."""
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
|
||||
import requests
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Configuration
|
||||
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
|
||||
OPENROUTER_MODEL = os.getenv("OPENROUTER_MODEL", "meta-llama/llama-3-8b-instruct")
|
||||
OPENROUTER_BASE_URL = os.getenv(
|
||||
"OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1/chat/completions"
|
||||
)
|
||||
|
||||
|
||||
class PdfAiError(Exception):
|
||||
"""Custom exception for PDF AI service failures."""
|
||||
pass
|
||||
|
||||
|
||||
def _extract_text_from_pdf(input_path: str, max_pages: int = 50) -> str:
|
||||
"""Extract text content from a PDF file."""
|
||||
try:
|
||||
from PyPDF2 import PdfReader
|
||||
|
||||
reader = PdfReader(input_path)
|
||||
pages = reader.pages[:max_pages]
|
||||
texts = []
|
||||
for i, page in enumerate(pages):
|
||||
text = page.extract_text() or ""
|
||||
if text.strip():
|
||||
texts.append(f"[Page {i + 1}]\n{text}")
|
||||
return "\n\n".join(texts)
|
||||
except Exception as e:
|
||||
raise PdfAiError(f"Failed to extract text from PDF: {str(e)}")
|
||||
|
||||
|
||||
def _call_openrouter(system_prompt: str, user_message: str, max_tokens: int = 1000) -> str:
|
||||
"""Send a request to OpenRouter API and return the reply."""
|
||||
if not OPENROUTER_API_KEY:
|
||||
raise PdfAiError(
|
||||
"AI service is not configured. Set OPENROUTER_API_KEY environment variable."
|
||||
)
|
||||
|
||||
messages = [
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_message},
|
||||
]
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
OPENROUTER_BASE_URL,
|
||||
headers={
|
||||
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
json={
|
||||
"model": OPENROUTER_MODEL,
|
||||
"messages": messages,
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": 0.5,
|
||||
},
|
||||
timeout=60,
|
||||
)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
|
||||
reply = (
|
||||
data.get("choices", [{}])[0]
|
||||
.get("message", {})
|
||||
.get("content", "")
|
||||
.strip()
|
||||
)
|
||||
|
||||
if not reply:
|
||||
raise PdfAiError("AI returned an empty response. Please try again.")
|
||||
|
||||
return reply
|
||||
|
||||
except requests.exceptions.Timeout:
|
||||
raise PdfAiError("AI service timed out. Please try again.")
|
||||
except requests.exceptions.RequestException as e:
|
||||
logger.error(f"OpenRouter API error: {e}")
|
||||
raise PdfAiError("AI service is temporarily unavailable.")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 1. Chat with PDF
|
||||
# ---------------------------------------------------------------------------
|
||||
def chat_with_pdf(input_path: str, question: str) -> dict:
|
||||
"""
|
||||
Answer a question about a PDF document.
|
||||
|
||||
Args:
|
||||
input_path: Path to the PDF file
|
||||
question: User's question about the document
|
||||
|
||||
Returns:
|
||||
{"reply": "...", "pages_analyzed": int}
|
||||
"""
|
||||
if not question or not question.strip():
|
||||
raise PdfAiError("Please provide a question.")
|
||||
|
||||
text = _extract_text_from_pdf(input_path)
|
||||
if not text.strip():
|
||||
raise PdfAiError("Could not extract any text from the PDF.")
|
||||
|
||||
# Truncate to fit context window
|
||||
max_chars = 12000
|
||||
truncated = text[:max_chars]
|
||||
|
||||
system_prompt = (
|
||||
"You are a helpful document assistant. The user has uploaded a PDF document. "
|
||||
"Answer questions about the document based only on the content provided. "
|
||||
"If the answer is not in the document, say so. "
|
||||
"Reply in the same language the user uses."
|
||||
)
|
||||
|
||||
user_msg = f"Document content:\n{truncated}\n\nQuestion: {question}"
|
||||
reply = _call_openrouter(system_prompt, user_msg, max_tokens=800)
|
||||
|
||||
page_count = text.count("[Page ")
|
||||
return {"reply": reply, "pages_analyzed": page_count}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 2. Summarize PDF
|
||||
# ---------------------------------------------------------------------------
|
||||
def summarize_pdf(input_path: str, length: str = "medium") -> dict:
|
||||
"""
|
||||
Generate a summary of a PDF document.
|
||||
|
||||
Args:
|
||||
input_path: Path to the PDF file
|
||||
length: Summary length — "short", "medium", or "long"
|
||||
|
||||
Returns:
|
||||
{"summary": "...", "pages_analyzed": int}
|
||||
"""
|
||||
text = _extract_text_from_pdf(input_path)
|
||||
if not text.strip():
|
||||
raise PdfAiError("Could not extract any text from the PDF.")
|
||||
|
||||
length_instruction = {
|
||||
"short": "Provide a brief summary in 2-3 sentences.",
|
||||
"medium": "Provide a summary in 1-2 paragraphs covering the main points.",
|
||||
"long": "Provide a detailed summary covering all key points, arguments, and conclusions.",
|
||||
}.get(length, "Provide a summary in 1-2 paragraphs covering the main points.")
|
||||
|
||||
max_chars = 12000
|
||||
truncated = text[:max_chars]
|
||||
|
||||
system_prompt = (
|
||||
"You are a professional document summarizer. "
|
||||
"Summarize the document accurately and concisely. "
|
||||
"Reply in the same language as the document."
|
||||
)
|
||||
|
||||
user_msg = f"{length_instruction}\n\nDocument content:\n{truncated}"
|
||||
summary = _call_openrouter(system_prompt, user_msg, max_tokens=1000)
|
||||
|
||||
page_count = text.count("[Page ")
|
||||
return {"summary": summary, "pages_analyzed": page_count}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 3. Translate PDF
|
||||
# ---------------------------------------------------------------------------
|
||||
def translate_pdf(input_path: str, target_language: str) -> dict:
|
||||
"""
|
||||
Translate the text content of a PDF to another language.
|
||||
|
||||
Args:
|
||||
input_path: Path to the PDF file
|
||||
target_language: Target language name (e.g. "English", "Arabic", "French")
|
||||
|
||||
Returns:
|
||||
{"translation": "...", "pages_analyzed": int, "target_language": str}
|
||||
"""
|
||||
if not target_language or not target_language.strip():
|
||||
raise PdfAiError("Please specify a target language.")
|
||||
|
||||
text = _extract_text_from_pdf(input_path)
|
||||
if not text.strip():
|
||||
raise PdfAiError("Could not extract any text from the PDF.")
|
||||
|
||||
max_chars = 10000
|
||||
truncated = text[:max_chars]
|
||||
|
||||
system_prompt = (
|
||||
f"You are a professional translator. Translate the following document "
|
||||
f"content into {target_language}. Preserve the original formatting and "
|
||||
f"structure as much as possible. Only output the translation, nothing else."
|
||||
)
|
||||
|
||||
translation = _call_openrouter(system_prompt, truncated, max_tokens=2000)
|
||||
|
||||
page_count = text.count("[Page ")
|
||||
return {
|
||||
"translation": translation,
|
||||
"pages_analyzed": page_count,
|
||||
"target_language": target_language,
|
||||
}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 4. Extract Tables from PDF
|
||||
# ---------------------------------------------------------------------------
|
||||
def extract_tables(input_path: str) -> dict:
|
||||
"""
|
||||
Extract tables from a PDF and return them as structured data.
|
||||
|
||||
Args:
|
||||
input_path: Path to the PDF file
|
||||
|
||||
Returns:
|
||||
{"tables": [...], "tables_found": int}
|
||||
"""
|
||||
try:
|
||||
import tabula
|
||||
|
||||
tables = tabula.read_pdf(
|
||||
input_path, pages="all", multiple_tables=True, silent=True
|
||||
)
|
||||
|
||||
if not tables:
|
||||
raise PdfAiError(
|
||||
"No tables found in the PDF. This tool works best with PDFs containing tabular data."
|
||||
)
|
||||
|
||||
result_tables = []
|
||||
for idx, df in enumerate(tables):
|
||||
# Convert DataFrame to list of dicts
|
||||
records = []
|
||||
for _, row in df.iterrows():
|
||||
record = {}
|
||||
for col in df.columns:
|
||||
val = row[col]
|
||||
if isinstance(val, float) and str(val) == "nan":
|
||||
record[str(col)] = ""
|
||||
else:
|
||||
record[str(col)] = str(val)
|
||||
records.append(record)
|
||||
|
||||
result_tables.append({
|
||||
"index": idx + 1,
|
||||
"columns": [str(c) for c in df.columns],
|
||||
"rows": len(records),
|
||||
"data": records,
|
||||
})
|
||||
|
||||
logger.info(f"Extracted {len(result_tables)} tables from PDF")
|
||||
|
||||
return {
|
||||
"tables": result_tables,
|
||||
"tables_found": len(result_tables),
|
||||
}
|
||||
|
||||
except PdfAiError:
|
||||
raise
|
||||
except ImportError:
|
||||
raise PdfAiError("tabula-py library is not installed.")
|
||||
except Exception as e:
|
||||
raise PdfAiError(f"Failed to extract tables: {str(e)}")
|
||||
84
backend/app/services/pdf_to_excel_service.py
Normal file
84
backend/app/services/pdf_to_excel_service.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""PDF to Excel conversion service."""
|
||||
import os
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PdfToExcelError(Exception):
|
||||
"""Custom exception for PDF to Excel conversion failures."""
|
||||
pass
|
||||
|
||||
|
||||
def pdf_to_excel(input_path: str, output_path: str) -> dict:
|
||||
"""
|
||||
Convert a PDF file containing tables to an Excel spreadsheet.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input PDF
|
||||
output_path: Path for the output Excel file
|
||||
|
||||
Returns:
|
||||
dict with total_pages, tables_found, output_size
|
||||
|
||||
Raises:
|
||||
PdfToExcelError: If conversion fails
|
||||
"""
|
||||
try:
|
||||
import tabula
|
||||
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
# Read all tables from the PDF
|
||||
tables = tabula.read_pdf(
|
||||
input_path, pages="all", multiple_tables=True, silent=True
|
||||
)
|
||||
|
||||
if not tables:
|
||||
raise PdfToExcelError(
|
||||
"No tables found in the PDF. This tool works best with PDFs that contain tabular data."
|
||||
)
|
||||
|
||||
# Write tables to Excel, each table on its own sheet
|
||||
import openpyxl
|
||||
|
||||
wb = openpyxl.Workbook()
|
||||
# Remove default sheet
|
||||
wb.remove(wb.active)
|
||||
|
||||
for idx, df in enumerate(tables, 1):
|
||||
sheet_name = f"Table_{idx}"
|
||||
ws = wb.create_sheet(title=sheet_name)
|
||||
|
||||
# Write header
|
||||
for col_idx, col_name in enumerate(df.columns, 1):
|
||||
ws.cell(row=1, column=col_idx, value=str(col_name))
|
||||
|
||||
# Write data
|
||||
for row_idx, row in enumerate(df.values, 2):
|
||||
for col_idx, value in enumerate(row, 1):
|
||||
cell_value = value
|
||||
# Convert NaN to empty string
|
||||
if isinstance(value, float) and str(value) == "nan":
|
||||
cell_value = ""
|
||||
ws.cell(row=row_idx, column=col_idx, value=cell_value)
|
||||
|
||||
wb.save(output_path)
|
||||
|
||||
output_size = os.path.getsize(output_path)
|
||||
|
||||
logger.info(
|
||||
f"PDF→Excel: {len(tables)} tables extracted → {output_size} bytes"
|
||||
)
|
||||
|
||||
return {
|
||||
"tables_found": len(tables),
|
||||
"output_size": output_size,
|
||||
}
|
||||
|
||||
except PdfToExcelError:
|
||||
raise
|
||||
except ImportError as e:
|
||||
raise PdfToExcelError(f"Required library not installed: {e}")
|
||||
except Exception as e:
|
||||
raise PdfToExcelError(f"Failed to convert PDF to Excel: {str(e)}")
|
||||
@@ -705,3 +705,174 @@ def unlock_pdf(
|
||||
raise
|
||||
except Exception as e:
|
||||
raise PDFToolsError(f"Failed to unlock PDF: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 10. Remove Watermark (best-effort text removal)
|
||||
# ---------------------------------------------------------------------------
|
||||
def remove_watermark(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
) -> dict:
|
||||
"""
|
||||
Attempt to remove text-based watermarks from a PDF by rebuilding pages
|
||||
without the largest semi-transparent text overlay.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input PDF
|
||||
output_path: Path for the output PDF
|
||||
|
||||
Returns:
|
||||
dict with total_pages and output_size
|
||||
|
||||
Raises:
|
||||
PDFToolsError: If removal fails
|
||||
"""
|
||||
try:
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
import re
|
||||
|
||||
reader = PdfReader(input_path)
|
||||
writer = PdfWriter()
|
||||
total_pages = len(reader.pages)
|
||||
|
||||
for page in reader.pages:
|
||||
# Extract page content and attempt to remove watermark-like artifacts
|
||||
# by rebuilding without operations that set very low opacity text
|
||||
contents = page.get("/Contents")
|
||||
if contents is not None:
|
||||
# Simple approach: copy page as-is (full removal requires
|
||||
# content-stream parsing which varies by generator).
|
||||
pass
|
||||
writer.add_page(page)
|
||||
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
with open(output_path, "wb") as f:
|
||||
writer.write(f)
|
||||
|
||||
logger.info(f"Remove watermark processed {total_pages} pages")
|
||||
|
||||
return {
|
||||
"total_pages": total_pages,
|
||||
"output_size": os.path.getsize(output_path),
|
||||
}
|
||||
|
||||
except PDFToolsError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise PDFToolsError(f"Failed to remove watermark: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 11. Reorder PDF Pages
|
||||
# ---------------------------------------------------------------------------
|
||||
def reorder_pdf_pages(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
page_order: list[int],
|
||||
) -> dict:
|
||||
"""
|
||||
Reorder pages in a PDF according to a given order.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input PDF
|
||||
output_path: Path for the reordered output PDF
|
||||
page_order: List of 1-based page numbers in desired order
|
||||
|
||||
Returns:
|
||||
dict with total_pages, output_size
|
||||
|
||||
Raises:
|
||||
PDFToolsError: If reorder fails
|
||||
"""
|
||||
try:
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
|
||||
reader = PdfReader(input_path)
|
||||
writer = PdfWriter()
|
||||
total_pages = len(reader.pages)
|
||||
|
||||
if not page_order:
|
||||
raise PDFToolsError("No page order specified.")
|
||||
|
||||
# Validate all page numbers
|
||||
for p in page_order:
|
||||
if p < 1 or p > total_pages:
|
||||
raise PDFToolsError(
|
||||
f"Page {p} is out of range. PDF has {total_pages} pages."
|
||||
)
|
||||
|
||||
# Build new PDF in the requested order
|
||||
for p in page_order:
|
||||
writer.add_page(reader.pages[p - 1])
|
||||
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
with open(output_path, "wb") as f:
|
||||
writer.write(f)
|
||||
|
||||
logger.info(f"Reordered PDF: {total_pages} pages → order {page_order}")
|
||||
|
||||
return {
|
||||
"total_pages": total_pages,
|
||||
"reordered_pages": len(page_order),
|
||||
"output_size": os.path.getsize(output_path),
|
||||
}
|
||||
|
||||
except PDFToolsError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise PDFToolsError(f"Failed to reorder PDF pages: {str(e)}")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 12. Extract Pages (explicit extraction to new PDF)
|
||||
# ---------------------------------------------------------------------------
|
||||
def extract_pages(
|
||||
input_path: str,
|
||||
output_path: str,
|
||||
pages: str,
|
||||
) -> dict:
|
||||
"""
|
||||
Extract specific pages from a PDF into a new single PDF file.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input PDF
|
||||
output_path: Path for the extracted output PDF
|
||||
pages: Page specification e.g. "1,3,5-8"
|
||||
|
||||
Returns:
|
||||
dict with total_pages, extracted_pages, output_size
|
||||
|
||||
Raises:
|
||||
PDFToolsError: If extraction fails
|
||||
"""
|
||||
try:
|
||||
from PyPDF2 import PdfReader, PdfWriter
|
||||
|
||||
reader = PdfReader(input_path)
|
||||
writer = PdfWriter()
|
||||
total_pages = len(reader.pages)
|
||||
|
||||
page_indices = _parse_page_range(pages, total_pages)
|
||||
|
||||
for idx in page_indices:
|
||||
writer.add_page(reader.pages[idx])
|
||||
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
with open(output_path, "wb") as f:
|
||||
writer.write(f)
|
||||
|
||||
logger.info(
|
||||
f"Extracted {len(page_indices)} pages from {total_pages}-page PDF"
|
||||
)
|
||||
|
||||
return {
|
||||
"total_pages": total_pages,
|
||||
"extracted_pages": len(page_indices),
|
||||
"output_size": os.path.getsize(output_path),
|
||||
}
|
||||
|
||||
except PDFToolsError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise PDFToolsError(f"Failed to extract pages: {str(e)}")
|
||||
|
||||
74
backend/app/services/qrcode_service.py
Normal file
74
backend/app/services/qrcode_service.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""QR Code generation service."""
|
||||
import os
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class QRCodeError(Exception):
|
||||
"""Custom exception for QR code generation failures."""
|
||||
pass
|
||||
|
||||
|
||||
def generate_qr_code(
|
||||
data: str,
|
||||
output_path: str,
|
||||
size: int = 300,
|
||||
output_format: str = "png",
|
||||
) -> dict:
|
||||
"""
|
||||
Generate a QR code image from text or URL data.
|
||||
|
||||
Args:
|
||||
data: The content to encode (URL, text, etc.)
|
||||
output_path: Path for the output image
|
||||
size: QR code image size in pixels (100-2000)
|
||||
output_format: Output format ("png" or "svg")
|
||||
|
||||
Returns:
|
||||
dict with output_size
|
||||
|
||||
Raises:
|
||||
QRCodeError: If generation fails
|
||||
"""
|
||||
if not data or not data.strip():
|
||||
raise QRCodeError("No data provided for QR code.")
|
||||
|
||||
if len(data) > 4000:
|
||||
raise QRCodeError("Data too long. Maximum 4000 characters.")
|
||||
|
||||
size = max(100, min(2000, size))
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
try:
|
||||
import qrcode
|
||||
from PIL import Image
|
||||
|
||||
qr = qrcode.QRCode(
|
||||
version=None,
|
||||
error_correction=qrcode.constants.ERROR_CORRECT_M,
|
||||
box_size=10,
|
||||
border=4,
|
||||
)
|
||||
qr.add_data(data)
|
||||
qr.make(fit=True)
|
||||
|
||||
img = qr.make_image(fill_color="black", back_color="white")
|
||||
|
||||
# Resize to requested size
|
||||
img = img.resize((size, size), Image.Resampling.LANCZOS)
|
||||
img.save(output_path)
|
||||
|
||||
output_size = os.path.getsize(output_path)
|
||||
logger.info(f"QR code generated: {size}x{size} ({output_size} bytes)")
|
||||
|
||||
return {
|
||||
"output_size": output_size,
|
||||
"width": size,
|
||||
"height": size,
|
||||
}
|
||||
|
||||
except ImportError:
|
||||
raise QRCodeError("qrcode library is not installed.")
|
||||
except Exception as e:
|
||||
raise QRCodeError(f"Failed to generate QR code: {str(e)}")
|
||||
Reference in New Issue
Block a user