@@ -77,30 +77,33 @@ def url_logout(request):
7777
7878
7979class LogMixin ():
80+ logging .disable (logging .NOTSET )
81+ logger = logging .getLogger (__name__ )
82+ logger .setLevel (logging .DEBUG )
8083
8184 def logi (self , * args ):
8285 msg = f'ℹ{ self .__class__ .__name__ } .{ sys ._getframe ().f_back .f_code .co_name } '
8386 for arg in args :
8487 msg += f'::{ arg } '
85- logging .info (msg )
88+ self . logger .info (msg )
8689
8790 def logw (self , * args ):
8891 msg = f'⚠{ self .__class__ .__name__ } .{ sys ._getframe ().f_back .f_code .co_name } '
8992 for arg in args :
9093 msg += f'::{ arg } '
91- logging .warning (msg )
94+ self . logger .warning (msg )
9295
9396 def logd (self , * args ):
9497 msg = f'‼{ self .__class__ .__name__ } .{ sys ._getframe ().f_back .f_code .co_name } '
9598 for arg in args :
9699 msg += f'::{ arg } '
97- logging .debug (msg )
100+ self . logger .debug (msg )
98101
99102 def loge (self , err , * args ):
100103 msg = f'🆘{ self .__class__ .__name__ } .{ err .__traceback__ .tb_frame .f_code .co_name } ::{ err } ::LINE={ err .__traceback__ .tb_lineno } '
101104 for arg in args :
102105 msg += f'::{ arg } '
103- logging .error (msg )
106+ self . logger .error (msg )
104107
105108
106109class ProductView (DetailView , LogMixin ):
@@ -383,8 +386,30 @@ def get(self, request, pk=None):
383386 locale .setlocale (locale .LC_ALL , '' )
384387 lcl = locale .getlocale (locale .LC_MESSAGES )
385388 lcode = lcl [0 ].split ('_' )[0 ] if lcl and lcl [0 ] else 'en'
386- html_content = f'<!DOCTYPE html><html lang="{ lcode } "><head><meta charset="utf-8"><title>{ obj } </title></head><body>{ body } </body></html>'
387- return HttpResponse (html_content )
389+ pdf_engine = request .GET .get ('pdf' , '' )
390+ if not pdf_engine :
391+ response = HttpResponse (f'<!DOCTYPE html><html lang="{ lcode } "><head><meta charset="utf-8"><title>{ obj } </title></head><body>{ body } </body></html>' )
392+ else :
393+ response = HttpResponse (body )
394+ #sudo apt install texlive-xetex, wkhtmltopdf, pandoc
395+ #pandoc --quiet (Suppress warning messages)
396+ from subprocess import Popen , PIPE , STDOUT
397+ try :
398+ #pandoc = Popen(['pandoc', '--from=html', '--to=pdf', '--pdf-engine=xelatex', '--pdf-engine-opt=-recorder', '-V "mainfont:Times New Roman" -V "monofont:Times New Roman Mono"'], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
399+ pandoc = Popen (['pandoc' , '--from=html' , '--to=pdf' , f'--pdf-engine={ pdf_engine } ' , f'-V lang={ lcode } ' , '-V mainfont="Times New Roman"' , '-V sansfont="DejaVu Sans"' ], stdin = PIPE , stdout = PIPE , stderr = STDOUT )
400+ except Exception as e :
401+ self .logw (e , 'PANDOC-HTML-TO-PDF' )
402+ response .status_code = 400
403+ response .content = f'<!DOCTYPE html>{ e } ;'
404+ else :
405+ response ['Content-Type' ] = 'application/pdf'
406+ response ['Content-Disposition' ] = f'attachment; filename="sales_receipt_{ obj .id } .pdf"'
407+ response .content = pandoc .communicate (input = response .content )
408+ pos = response .content .find (b'%PDF' )
409+ if pos :
410+ response ['pdf-errors' ] = response .content [:pos ].replace (b'\n ' , b'|' ).replace (b'\r ' , b'' )
411+ response .content = response .content [pos :response .content .find (b'%%EOF' )+ 5 ]
412+ return response
388413
389414
390415class CustomersView (PaginatedView ):
0 commit comments