also try concatenating local pdf files with labels
This commit is contained in:
parent
ca91572ab4
commit
1367f05e00
3 changed files with 35 additions and 5 deletions
13
poetry.lock
generated
13
poetry.lock
generated
|
@ -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"},
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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")
|
||||
|
||||
|
|
Loading…
Reference in a new issue