Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: a solution for multi label printing #3537

Closed
wants to merge 1 commit into from
Closed
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
49 changes: 24 additions & 25 deletions InvenTree/label/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""API functionality for the 'label' app"""

import datetime
from django.conf import settings
from django.core.exceptions import FieldError, ValidationError
from django.http import HttpResponse, JsonResponse
Expand All @@ -18,7 +19,7 @@
from plugin.registry import registry
from stock.models import StockItem, StockLocation

from .models import PartLabel, StockItemLabel, StockLocationLabel
from .models import PartLabel, StockItemLabel, StockLocationLabel, WeasyprintLabelMixin
from .serializers import (PartLabelSerializer, StockItemLabelSerializer,
StockLocationLabelSerializer)

Expand Down Expand Up @@ -107,10 +108,7 @@ def print(self, request, items_to_print):
label_names.append(label_name)
label_instances.append(label)

if debug_mode:
outputs.append(label.render_as_string(request))
else:
outputs.append(label.render(request))
outputs.append(label.render_as_string(request))

if not label_name.endswith(".pdf"):
label_name += ".pdf"
Expand Down Expand Up @@ -154,28 +152,29 @@ def print(self, request, items_to_print):
return HttpResponse(html)

else:
"""Concatenate all rendered pages into a single PDF object, and return the resulting document!"""

pages = []

if len(outputs) > 1:
# If more than one output is generated, merge them into a single file
for output in outputs:
doc = output.get_document()
for page in doc.pages:
pages.append(page)

pdf = outputs[0].get_document().copy(pages).write_pdf()
else:
pdf = outputs[0].get_document().write_pdf()

inline = common.models.InvenTreeUserSetting.get_setting('LABEL_INLINE', user=request.user)
label = self.get_object()
context = {}

context['base_url'] = common.models.InvenTreeSetting.get_setting('INVENTREE_BASE_URL')
context['date'] = datetime.datetime.now().date()
context['datetime'] = datetime.datetime.now()
context['request'] = request
context['user'] = request.user
context['width'] = label.width
context['height'] = label.height
context['outputs'] = outputs

wp = WeasyprintLabelMixin(
request,
"label/labels.html",
base_url=request.build_absolute_uri("/"),
presentational_hints=True,
filename="labels.pdf",
)

return InvenTree.helpers.DownloadFile(
pdf,
label_name,
content_type='application/pdf',
inline=inline
return wp.render_to_response(
context=context,
)


Expand Down
41 changes: 41 additions & 0 deletions InvenTree/label/templates/label/labels.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% load report %}
{% load barcode %}

<head>
<style>
@page {
size: {{ width }}mm {{ height }}mm;
{% block margin %}
margin: 0mm;
{% endblock %}
}

body {
font-family: Arial, Helvetica, sans-serif;
margin: 0mm;
color: #000;
background-color: #FFF;
}

img {
display: inline-block;
image-rendering: pixelated;
}

.thelabel {
break-after: always;
}

{% block style %}
{% endblock %}

</style>
</head>

<body>
{% for output in outputs %}
<div class="thelabel">
{{ output }}
</div>
{% endfor %}
</body>