Skip to content

Commit 300ef5f

Browse files
jans23sudhir-erpharbor
authored andcommitted
shipping label in PDF format
1 parent f5a58d8 commit 300ef5f

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

delivery_ups_oca/__manifest__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
"delivery_state",
2020
"stock_picking_declared_value",
2121
],
22+
"external_dependencies": {
23+
"python": ["Pillow"],
24+
},
2225
"data": [
2326
"security/ir.model.access.csv",
2427
"data/product_packaging_data.xml",

delivery_ups_oca/models/delivery_carrier.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
77

88
import base64
9+
import io
10+
import PIL.PdfImagePlugin
11+
from PIL import Image
912

1013
from odoo import _, fields, models
1114
from odoo.exceptions import UserError
@@ -23,7 +26,7 @@ class DeliveryCarrier(models.Model):
2326
},
2427
)
2528
ups_file_format = fields.Selection(
26-
selection=[("GIF", "GIF"), ("ZPL", "ZPL"), ("EPL", "EPL"), ("SPL", "SPL")],
29+
selection=[("PDF", "PDF"), ("GIF", "GIF"), ("ZPL", "ZPL"), ("EPL", "EPL"), ("SPL", "SPL")],
2730
default="GIF",
2831
string="File format",
2932
)
@@ -158,6 +161,19 @@ def ups_create_shipping(self, picking):
158161
def ups_send_shipping(self, pickings):
159162
return [self.ups_create_shipping(p) for p in pickings]
160163

164+
def _convert_gif_to_pdf(self, gif_data):
165+
"""Convert GIF image data to PDF format
166+
:param gif_data: base64 encoded GIF data
167+
:returns: base64 encoded PDF data
168+
"""
169+
# Decode base64 GIF data
170+
img_decoded = base64.b64decode(gif_data)
171+
image_string = io.BytesIO(img_decoded)
172+
im = Image.open(image_string)
173+
label_result = io.BytesIO()
174+
im.save(label_result, 'PDF')
175+
return base64.b64encode(label_result.getvalue()).decode('utf-8')
176+
161177
def _prepare_ups_label_attachment(self, picking, values):
162178
return {
163179
"name": values["name"],
@@ -171,17 +187,25 @@ def _create_ups_label(self, picking, labels):
171187
val_list = []
172188
for label in labels:
173189
format_code = label["format_code"].upper()
190+
label_data = label["datas"]
191+
file_extension = format_code
192+
193+
# Convert GIF to PDF only when PDF format is explicitly selected
194+
if self.ups_file_format == "PDF" and format_code == "GIF":
195+
label_data = self._convert_gif_to_pdf(label_data)
196+
file_extension = "PDF"
197+
174198
attachment_name = "%s-%s.%s" % (
175199
label["tracking_ref"],
176-
format_code,
177-
format_code,
200+
self.ups_file_format if self.ups_file_format == "PDF" else format_code,
201+
file_extension.lower(),
178202
)
179203
val_list.append(
180204
self._prepare_ups_label_attachment(
181205
picking,
182206
{
183207
"name": attachment_name,
184-
"datas": label["datas"],
208+
"datas": label_data,
185209
},
186210
)
187211
)

delivery_ups_oca/models/ups_request.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,11 @@ def _partner_to_shipping_data(self, partner, **kwargs):
232232
return result
233233

234234
def _label_data(self):
235-
res = {"LabelImageFormat": {"Code": self.file_format}}
235+
# When PDF is selected, request GIF from UPS API since UPS doesn't support PDF natively
236+
api_format = "GIF" if self.file_format == "PDF" else self.file_format
237+
res = {"LabelImageFormat": {"Code": api_format}}
236238
# According to documentation, we need to specify sizes in some formats
237-
if self.file_format != "GIF":
239+
if api_format != "GIF":
238240
res["LabelStockSize"] = {"Height": "6", "Width": "4"}
239241
return res
240242

0 commit comments

Comments
 (0)