66# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
77
88import base64
9+ import io
10+ import PIL .PdfImagePlugin
11+ from PIL import Image
912
1013from odoo import _ , fields , models
1114from 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 )
0 commit comments