Skip to content

Commit 84485b9

Browse files
jans23sudhir-erpharbor
authored andcommitted
shipping label in PDF format
1 parent 25b6e48 commit 84485b9

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-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: 29 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,20 @@ 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 with proper 6x4" page size
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+
# Set resolution to 236 DPI (standard for 6x4" UPS labels)
175+
im.save(label_result, 'PDF', resolution=236.0)
176+
return base64.b64encode(label_result.getvalue()).decode('utf-8')
177+
161178
def _prepare_ups_label_attachment(self, picking, values):
162179
return {
163180
"name": values["name"],
@@ -171,17 +188,25 @@ def _create_ups_label(self, picking, labels):
171188
val_list = []
172189
for label in labels:
173190
format_code = label["format_code"].upper()
191+
label_data = label["datas"]
192+
file_extension = format_code
193+
194+
# Convert GIF to PDF only when PDF format is explicitly selected
195+
if self.ups_file_format == "PDF" and format_code == "GIF":
196+
label_data = self._convert_gif_to_pdf(label_data)
197+
file_extension = "PDF"
198+
174199
attachment_name = "%s-%s.%s" % (
175200
label["tracking_ref"],
176-
format_code,
177-
format_code,
201+
self.ups_file_format if self.ups_file_format == "PDF" else format_code,
202+
file_extension.lower(),
178203
)
179204
val_list.append(
180205
self._prepare_ups_label_attachment(
181206
picking,
182207
{
183208
"name": attachment_name,
184-
"datas": label["datas"],
209+
"datas": label_data,
185210
},
186211
)
187212
)

delivery_ups_oca/models/ups_request.py

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

269269
def _label_data(self):
270-
res = {"LabelImageFormat": {"Code": self.file_format}}
270+
# When PDF is selected, request GIF from UPS API since UPS doesn't support PDF natively
271+
api_format = "GIF" if self.file_format == "PDF" else self.file_format
272+
res = {"LabelImageFormat": {"Code": api_format}}
271273
# According to documentation, we need to specify sizes in some formats
272-
if self.file_format != "GIF":
274+
if api_format != "GIF":
273275
res["LabelStockSize"] = {"Height": "6", "Width": "4"}
274276
return res
275277

0 commit comments

Comments
 (0)