feat: product barcode pdf resize
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
from abc import abstractmethod
|
||||
|
||||
from fastapi import UploadFile
|
||||
from typing import BinaryIO
|
||||
|
||||
|
||||
class BaseImagesUploader:
|
||||
@@ -18,5 +17,5 @@ class BaseImagesUploader:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
async def upload(self, upload_file: UploadFile) -> str:
|
||||
pass
|
||||
async def upload(self, file: BinaryIO, filename: str) -> str:
|
||||
pass
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
from pathlib import Path
|
||||
from typing import BinaryIO
|
||||
from uuid import uuid4
|
||||
|
||||
from aioshutil import copyfileobj
|
||||
from fastapi import UploadFile
|
||||
|
||||
from barcodes.images_uploader.base import BaseImagesUploader
|
||||
from barcodes.pdf import PDFGenerator
|
||||
from constants import APP_PATH, API_ROOT
|
||||
|
||||
|
||||
@@ -29,10 +28,10 @@ class BarcodeImagesUploader(BaseImagesUploader):
|
||||
if file_location.exists():
|
||||
file_location.unlink()
|
||||
|
||||
async def upload(self, upload_file: UploadFile) -> str:
|
||||
filename = str(uuid4()) + '.' + upload_file.filename.split('.')[-1]
|
||||
async def upload(self, file: BinaryIO, filename: str) -> str:
|
||||
filename = str(uuid4()) + '.' + filename.split('.')[-1]
|
||||
file_location = self.storage_path / filename
|
||||
with open(file_location, 'wb') as buffer:
|
||||
await copyfileobj(upload_file.file, buffer)
|
||||
await copyfileobj(file, buffer)
|
||||
|
||||
return filename
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
from io import BytesIO
|
||||
from typing import BinaryIO
|
||||
|
||||
from fpdf import FPDF
|
||||
import fitz
|
||||
import pdfrw
|
||||
from pdfrw import PdfReader
|
||||
from pdfrw.objects.pdfdict import PdfDict
|
||||
from fpdf import FPDF
|
||||
|
||||
|
||||
class PdfMaker:
|
||||
@@ -42,27 +40,44 @@ class PdfMaker:
|
||||
return result_io
|
||||
|
||||
@staticmethod
|
||||
def check_is_correct_aspects_ratio(file: BinaryIO):
|
||||
pdf = PdfReader(file)
|
||||
allowed_aspects_ratio = 1.45
|
||||
page: PdfDict
|
||||
def _get_target_rect(page: fitz.Page, target_ratio: float) -> fitz.Rect:
|
||||
original_width, original_height = page.rect.width, page.rect.height
|
||||
|
||||
try:
|
||||
page: PdfDict = pdf.getPage(0)
|
||||
except IndexError:
|
||||
raise Exception("Ошибка. В документе нет страниц.")
|
||||
if original_width / original_height > target_ratio:
|
||||
# Image is wider than target aspect ratio
|
||||
new_width = original_width
|
||||
new_height = int(original_width / target_ratio)
|
||||
else:
|
||||
# Image is taller than target aspect ratio
|
||||
new_height = original_height
|
||||
new_width = int(new_height * target_ratio)
|
||||
|
||||
try:
|
||||
pdf.getPage(1)
|
||||
raise Exception("Ошибка. В документе должна быть только одна страница.")
|
||||
except IndexError:
|
||||
pass
|
||||
return fitz.Rect(0, 0, new_width, new_height)
|
||||
|
||||
media_box = page.MediaBox
|
||||
width = float(media_box[2]) - float(media_box[0])
|
||||
height = float(media_box[3]) - float(media_box[1])
|
||||
@staticmethod
|
||||
def resize_pdf_with_reportlab(input_pdf_bytesio: BytesIO) -> BytesIO:
|
||||
output_pdf = BytesIO()
|
||||
|
||||
aspect_ratio = width / height
|
||||
if abs(aspect_ratio - allowed_aspects_ratio) > 0.01:
|
||||
raise Exception("Ошибка. Страница документа должна быть размером 58х40.")
|
||||
file.seek(0)
|
||||
pdf_document = fitz.open(stream=input_pdf_bytesio.getvalue(), filetype="pdf")
|
||||
|
||||
if len(pdf_document) != 1:
|
||||
raise Exception("Ошибка. В документе должна быть одна страница.")
|
||||
|
||||
page = pdf_document[0]
|
||||
target_ratio = 29 / 20
|
||||
actual_ratio = page.rect.width / page.rect.height
|
||||
|
||||
if abs(actual_ratio - target_ratio) < 0.1:
|
||||
return input_pdf_bytesio
|
||||
|
||||
rect = PdfMaker._get_target_rect(page, target_ratio)
|
||||
page.set_mediabox(rect)
|
||||
page.set_cropbox(rect)
|
||||
page.set_bleedbox(rect)
|
||||
page.set_trimbox(rect)
|
||||
|
||||
pdf_document.save(output_pdf)
|
||||
pdf_document.close()
|
||||
|
||||
output_pdf.seek(0)
|
||||
return output_pdf
|
||||
|
||||
@@ -32,4 +32,5 @@ reportlab
|
||||
weasyprint
|
||||
number_to_string
|
||||
pdfrw
|
||||
fpdf
|
||||
fpdf
|
||||
PyMuPDF
|
||||
@@ -1,3 +1,4 @@
|
||||
from io import BytesIO
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException, UploadFile
|
||||
@@ -278,12 +279,13 @@ class ProductService(BaseService):
|
||||
|
||||
async def upload_barcode_image(self, product_id: int, upload_file: UploadFile) -> ProductUploadBarcodeImageResponse:
|
||||
try:
|
||||
PdfMaker.check_is_correct_aspects_ratio(upload_file.file)
|
||||
await self.get_model_by_id(product_id)
|
||||
|
||||
uploader = BarcodeImagesUploader()
|
||||
await self.delete_model_barcode_image(uploader, product_id)
|
||||
filename = await uploader.upload(upload_file)
|
||||
|
||||
file = PdfMaker.resize_pdf_with_reportlab(BytesIO(upload_file.file.read()))
|
||||
filename = await uploader.upload(file, upload_file.filename)
|
||||
barcode_image_url = uploader.get_url(filename)
|
||||
|
||||
product_barcode_image = ProductBarcodeImage(
|
||||
|
||||
Reference in New Issue
Block a user