Улучшена генерация PDF отчета
- Добавлен OverlayCanvas для отрисовки штампа поверх контента - Изменено позиционирование штампа - Добавлены необходимые импорты для работы с Canvas
This commit is contained in:
parent
98b0e7ac26
commit
e52f7ea5d9
48
app.py
48
app.py
@ -20,7 +20,9 @@ from reportlab.lib import colors
|
||||
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer, Image
|
||||
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||||
from reportlab.lib.units import inch, mm
|
||||
from reportlab.pdfgen.canvas import Canvas
|
||||
from reportlab.lib.enums import TA_CENTER, TA_LEFT, TA_RIGHT
|
||||
from reportlab.platypus import Frame
|
||||
import sys
|
||||
import logging
|
||||
|
||||
@ -150,6 +152,34 @@ dictConfig({
|
||||
# Создаем логгер для приложения
|
||||
logger = logging.getLogger("app")
|
||||
|
||||
class OverlayCanvas(Canvas):
|
||||
def __init__(self, *args, **kwargs):
|
||||
Canvas.__init__(self, *args, **kwargs)
|
||||
self._saved_page_states = []
|
||||
|
||||
def showPage(self):
|
||||
self._saved_page_states.append(dict(self.__dict__))
|
||||
self._startPage()
|
||||
|
||||
def save(self):
|
||||
for state in self._saved_page_states:
|
||||
self.__dict__.update(state)
|
||||
self.draw_overlay() # теперь рисуем поверх
|
||||
Canvas.showPage(self)
|
||||
Canvas.save(self)
|
||||
|
||||
def draw_overlay(self):
|
||||
stamp_path = os.path.join(app_path, 'static', 'stamp256.png')
|
||||
if os.path.exists(stamp_path):
|
||||
self.drawImage(
|
||||
stamp_path,
|
||||
x=400, # настроить по ширине
|
||||
y=400, # настроить по высоте
|
||||
width=100,
|
||||
height=100,
|
||||
mask='auto'
|
||||
)
|
||||
|
||||
@app.after_request
|
||||
def after_request(response):
|
||||
if request.cookies.get('user_id', None) == None:
|
||||
@ -1107,6 +1137,19 @@ def generate_pdf_report(vin):
|
||||
|
||||
elements.append(Spacer(1, 15*mm))
|
||||
|
||||
# Добавляем штамп поверх таблицы
|
||||
# stamp_path = os.path.join(app_path, 'static', 'stamp256.png')
|
||||
# if os.path.exists(stamp_path):
|
||||
# stamp_img = Image(stamp_path, width=100, height=100)
|
||||
# stamp_img.hAlign = 'RIGHT'
|
||||
# stamp_img.vAlign = 'TOP'
|
||||
# # Размещаем штамп поверх таблицы
|
||||
# elements.append(stamp_img)
|
||||
|
||||
# elements.append(Spacer(1, 15*mm))
|
||||
|
||||
|
||||
|
||||
# Дисклеймер
|
||||
disclaimer_text = """
|
||||
Disclaimer: This report provides information about salvage or junk vehicles; damage from hail, flood or fire;
|
||||
@ -1120,10 +1163,11 @@ def generate_pdf_report(vin):
|
||||
elements.append(Spacer(1, 15*mm))
|
||||
elements.append(Paragraph(f"© {current_year} SALVAGEDB.COM - All Rights Reserved", styles['SalvageDBFooter']))
|
||||
elements.append(Paragraph("Visit SALVAGEDB.COM for more information about vehicle history", styles['SalvageDBFooter']))
|
||||
elements.append(Paragraph("This report is provided as is without any guarantees or warranty.", styles['Disclaimer']))
|
||||
elements.append(Paragraph('"This report is provided as is without any guarantees or warranty."', styles['Disclaimer']))
|
||||
|
||||
# Строим PDF документ
|
||||
doc.build(elements)
|
||||
# doc.build(elements, onFirstPage=add_stamp_overlay, onLaterPages=add_stamp_overlay)
|
||||
doc.build(elements, canvasmaker=OverlayCanvas)
|
||||
|
||||
# Получаем содержимое буфера
|
||||
pdf_data = buffer.getvalue()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user