55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
|
from django.core.mail import EmailMessage
|
||
|
from django.template.loader import render_to_string
|
||
|
|
||
|
import subprocess
|
||
|
import os
|
||
|
|
||
|
import subprocess
|
||
|
import os
|
||
|
|
||
|
|
||
|
def generate_invoice_image(html_content, output_path):
|
||
|
# Создание временного HTML файла
|
||
|
temp_html = "temp_invoice.html"
|
||
|
with open(temp_html, "w", encoding='utf-8') as f:
|
||
|
f.write(html_content)
|
||
|
|
||
|
# Полный путь к исполняемому файлу wkhtmltoimage
|
||
|
wkhtmltoimage_path = r"C:\Users\fhjj3\djangoProject1\orders\wkhtmltopdf\bin\wkhtmltoimage.exe"
|
||
|
|
||
|
# Команда для преобразования HTML в изображение
|
||
|
command = [wkhtmltoimage_path, temp_html, output_path]
|
||
|
|
||
|
# Выполнение команды
|
||
|
subprocess.call(command)
|
||
|
|
||
|
# Удаление временного файла
|
||
|
os.remove(temp_html)
|
||
|
|
||
|
|
||
|
def send_invoice_via_email(email_to, order):
|
||
|
# Генерация HTML для чека
|
||
|
order_items = order.items.all()
|
||
|
context = {'order': order, 'order_items': order_items}
|
||
|
html_content = render_to_string('orders/order/pdf.html', context)
|
||
|
|
||
|
# Путь, где будет сохранено изображение чека
|
||
|
image_path = "invoice_image.png"
|
||
|
|
||
|
# Создание изображения из HTML
|
||
|
generate_invoice_image(html_content, image_path)
|
||
|
|
||
|
# Создание и отправка письма
|
||
|
email = EmailMessage(
|
||
|
subject=f'Ваш заказ №{order.id}',
|
||
|
body="Детали вашего заказа во вложении. Для оплаты используйте ссылку: [ссылка на оплату]",
|
||
|
from_email='from@example.com',
|
||
|
to=[email_to]
|
||
|
)
|
||
|
|
||
|
# Прикрепление изображения к письму
|
||
|
email.attach_file(image_path)
|
||
|
|
||
|
# Отправка
|
||
|
email.send()
|