Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
FROM python:3.9

RUN echo "deb http://deb.debian.org/debian bookworm main contrib" | tee /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y --no-install-recommends libreoffice-writer
RUN apt-get install -y libreoffice-java-common
RUN apt-get install -y ttf-mscorefonts-installer

WORKDIR /app

COPY requirements.txt .
Expand Down
38 changes: 34 additions & 4 deletions backend/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Вьюсеты v1 API."""
import io
import os
import subprocess
import tempfile
from pathlib import Path

import aspose.words as aw
from django.contrib.auth import get_user_model
Expand Down Expand Up @@ -56,7 +59,7 @@ class CategoryViewSet(viewsets.ModelViewSet):
permissions_classes = (AllowAny,)


def send_file(filestream, filename: str):
def send_file(filestream, filename: str, as_attachment: bool = True):
"""Функция подготовки открытого двоичного файла к отправке."""
response = FileResponse(
streaming_content=filestream,
Expand Down Expand Up @@ -223,8 +226,7 @@ def history_documents(self, request):
url_path=r"download_document",
)
def download_document(self, request, pk=None):
"""Скачивание готового документа"""

"""Скачивание готового документа."""
document = get_object_or_404(Document, id=pk)
context = dict()
for docfield in document.document_fields.all():
Expand All @@ -242,9 +244,37 @@ def download_document(self, request, pk=None):

@action(
detail=True,
permission_classes=[IsOwner],
permissions_classes=[IsAuthenticated],
url_path="download_pdf",
)
def download_pdf(self, request, pk=None):
"""Генерация и выдача на скачивание pdf-файла."""
with tempfile.NamedTemporaryFile() as output:
outfile = Path(output.name).resolve()
outfile.write_bytes(
b''.join(self.download_document(request, pk).streaming_content)
)
subprocess.run([
"soffice",
"--headless",
"--invisible",
"--nologo",
"--convert-to",
"pdf",
"--outdir",
outfile.parent,
outfile.absolute(),
])
newfile = outfile.with_suffix(".pdf")
buffer = io.BytesIO(newfile.read_bytes())
response = send_file(buffer, newfile.name)
return response

@action(
detail=True,
permission_classes=[IsOwner],
)
def download_pdf_aspose(self, request, pk=None):
"""Скачивание pdf-файла."""
document = get_object_or_404(Document, id=pk, owner=request.user)
docx_stream = io.BytesIO(
Expand Down
1 change: 1 addition & 0 deletions backend/commands/app.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash

python manage.py makemigrations

python manage.py migrate
Expand Down