diff --git a/poetry.lock b/poetry.lock index 44ddad9..6a935b6 100644 --- a/poetry.lock +++ b/poetry.lock @@ -309,6 +309,14 @@ optional = false python-versions = ">=3.5" version = "2.6.1" +[[package]] +category = "main" +description = "PDF toolkit" +name = "pypdf2" +optional = false +python-versions = "*" +version = "1.26.0" + [[package]] category = "main" description = "Pure Python module to hyphenate text" @@ -412,7 +420,7 @@ python-versions = "*" version = "0.5.1" [metadata] -content-hash = "f551392acf27b79f04c5fab68df548257f3a538875f9b4bf501e4633a3761860" +content-hash = "56be8de37de7b07fbe5ca98c9ceea5ca22dbd2ddb2053e252bc41baf6293c373" python-versions = "^3.8" [metadata.files] @@ -561,6 +569,9 @@ pygments = [ {file = "Pygments-2.6.1-py3-none-any.whl", hash = "sha256:ff7a40b4860b727ab48fad6360eb351cc1b33cbf9b15a0f689ca5353e9463324"}, {file = "Pygments-2.6.1.tar.gz", hash = "sha256:647344a061c249a3b74e230c739f434d7ea4d8b1d5f3721bc0f3558049b38f44"}, ] +pypdf2 = [ + {file = "PyPDF2-1.26.0.tar.gz", hash = "sha256:e28f902f2f0a1603ea95ebe21dff311ef09be3d0f0ef29a3e44a932729564385"}, +] pyphen = [ {file = "Pyphen-0.9.5-py2.py3-none-any.whl", hash = "sha256:e172faf10992c8c9d369bdc83e36dbcf1121f4ed0d881f1a0b521935aee583b5"}, {file = "Pyphen-0.9.5.tar.gz", hash = "sha256:3b633a50873156d777e1f1075ba4d8e96a6ad0a3ca42aa3ea9a6259f93f18921"}, diff --git a/pyproject.toml b/pyproject.toml index c78fb08..ecee974 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,6 +9,7 @@ python = "^3.8" django = "^3.0.4" django-model-utils = "^4.0.0" weasyprint = "^51" +pypdf2 = "^1.26.0" [tool.poetry.dev-dependencies] ipython = "^7.13.0" diff --git a/sdbs_pile/pile/views.py b/sdbs_pile/pile/views.py index a94a013..789d4e4 100644 --- a/sdbs_pile/pile/views.py +++ b/sdbs_pile/pile/views.py @@ -1,8 +1,10 @@ # Create your views here. import io +import logging from operator import itemgetter import weasyprint +from PyPDF2 import PdfFileWriter, PdfFileReader from django.contrib.syndication.views import Feed from django.core.exceptions import ObjectDoesNotExist from django.http import Http404, FileResponse @@ -106,11 +108,27 @@ class DocumentWithLabel(BasePileViewMixin): label_html = render_to_string("label.html", {'document': document}) - stream = io.BytesIO() - weasyprint.HTML(base_url=request.build_absolute_uri(), string=label_html).write_pdf(stream) - stream.seek(0) + label_stream = io.BytesIO() + weasyprint.HTML(base_url=request.build_absolute_uri(), string=label_html).write_pdf(label_stream) + label_stream.seek(0) - return FileResponse(stream, + final_stream = label_stream + + if document.file: + try: + with open(document.file.path, 'rb') as document_fp: + writer = PdfFileWriter() + for reader in map(PdfFileReader, (label_stream, document_fp)): + for n in range(reader.getNumPages()): + writer.addPage(reader.getPage(n)) + final_stream = io.BytesIO() + writer.write(final_stream) + final_stream.seek(0) + except Exception as exc: + logging.exception(exc) + final_stream = label_stream + + return FileResponse(final_stream, filename=f"pile_{document.id}__{slugify(document.title)}.pdf", content_type="application/pdf")