django - HTML to PDF in Indian languages with pisa - django

I'm converting a HTML file into PDF in Django using Pisa. It is working when the content is only in English. But here the content will be in English and five other Indian languages(Tamil, Hindi, Telugu, Malayalam, and Kannada). I have given my code below.
views.py
def render_to_pdf1(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('UTF-8')), result)
return result.getvalue()
def print_pdf(request):
message = Message.objects.get(id = 1)
html_table_string = ''
html_table_string += '%s' % message.english
html_table_string += '%s' % message.tamil
html_table_string += '%s' % message.hindi
html_table_string += '%s' % message.telugu
html_table_string += '%s' % message.kannada
html_table_string += '%s' % message.malayalam
fileread = str(settings.TEMPLATE_DIRS[0])+str('/base_file.html')
fr = open(fileread, "r").read()
fr = fr.replace('message_content', html_table_string)
result = StringIO.StringIO()
filewrite = str(settings.TEMPLATE_DIRS[0]) + str('/temp_file.html')
empty = ""
fw = open(filewrite, 'w')
fw.write(empty)
fw.write(fr)
fw.close()
pdf_contents = render_to_pdf1('temp_file.html',result)
file_to_be_saved = ContentFile(pdf_contents)
name = (str(request.user.email) + ".pdf").replace("#", '')
pdf = Pdf.objects.create(name = name, user = request.user, created_by = request.user)
pdf.name.save(name ,file_to_be_saved)
file_path = Pdf.objects.get(user = request.user).name
pdf_file = str(file_path).split("media")[1]
return HttpResponseRedirect('/site_media' + pdf_file)
Here what I'm doing is:
Having a base template base_file.html.
Getting the message object by ID (ID will be dynamically supplied).
Then replacing the message_content with current content.
Writing it in a file temp_file.html.
Converting temp_file.html into a PDF file.
The converted PDF will be containing the message in English, Tamil, Hindi, Telugu, Kannada, and Malayalam. But I couldn't write the other language contents in the HTML file and couldn't convert it. The error I'm getting is 'ascii' codec can't encode characters in position 1066-1075: ordinal not in range(128) and occurs in the line fw.write(fr).
So how can I achieve this? I want to print the PDF file with content in all these languages.

Related

\u0000 cannot be converted to text in django/postgreSQl

i have a project with django .on the host when i want to upload an image sometime error occurred(problem with specific images)! the below show how i resize uploaded images:
def save_files_to_media(request, is_public=False, klass=None, conversation=None):
from apps.file.models import File
fs = FileSystemStorage()
file_items = {}
for data_item in request.data:
file_match = re.search('^fileToUpload\[(\d+)\]$', data_item)
if file_match and file_match.groups():
item_index = file_match.groups()[0]
if item_index not in file_items:
file_items[item_index] = {}
file_items[item_index]['file_to_upload'] = request.data[data_item]
else:
optimize_match = re.search('^optimizeType\[(\d+)\]$', data_item)
if optimize_match and optimize_match.groups():
item_index = optimize_match.groups()[0]
if item_index not in file_items:
file_items[item_index] = {}
file_items[item_index]['optimize_type'] = request.data[data_item]
files = []
for file_item_key in file_items:
input_file = file_items[file_item_key]['file_to_upload']
# TODO: checking validation. if input_file.name is not exist
optimize_type = file_items[file_item_key].get('optimize_type')
file_uuid = str(uuid4())
if is_public:
orig_filename, file_ext = splitext(basename(input_file.name))
directory_name = join(settings.MEDIA_ROOT, file_uuid)
filename = file_uuid + file_ext
else:
directory_name = join(settings.MEDIA_ROOT, file_uuid)
mkdir(directory_name)
filename = input_file.name
filepath = join(directory_name, filename)
fs.save(filepath, input_file)
is_optimized = False
if optimize_type == 'image':
is_success, filepath = image_optimizer(filepath)
filename = basename(filepath)
is_optimized = is_success
file_obj = File(
orig_name=filename,
uuid=file_uuid,
md5sum=get_md5sum(filepath),
filesize=get_filesize(filepath),
meta=get_meta_info(filepath),
is_optimized=is_optimized,
creator=request.user
)
if is_public:
file_obj.is_public = True
else:
file_obj.klass = klass
file_obj.conversation = conversation
file_obj.save()
files.append(file_obj)
return files
here is the error i got with some images:
unsupported Unicode escape sequence
LINE 1: ..., 'ada90ead20f7994837dced344266cc51', 145216, '', '{"FileTyp...
^
DETAIL: \u0000 cannot be converted to text.
CONTEXT: JSON data, line 1: ...ecTimeDigitized": 506779, "MakerNoteUnknownText":
its funny that in my local but not in host. for more information i must tell you guys my postgreSQL version is 11.3 and host postgreSQl is 9.5.17 . where you think is problem? as error it's seems for postgreSQL. thank you

How do I limit pyqt4 clipboard just only print text or image path?

def tt(self):
cb=QApplication.clipboard()
data=cb.mimeData()
#if data.hasImage():
#for path in data.urls():
#print path
if data.hasText():
tex =unicode (data.text())
print tex
if tex != "":
r = QtCore.QStringList([])
for ct in tex:
py = slug(ct, style=pypinyin.TONE, errors='ignore')
if py != '':
w = ct + '(' + py + ')'
else:
w = ct
r.append(w)
str = r.join("")
self.ui.textEdit.setText(QtCore.QString(str))
I use python2.7 and pyqt4 to make something like Chinese characters to Pinyin. So when I copy string, it's fine, the job ding very well. but when I copy image, I just want only print its path . but tex still work, slug() will go error. how do I limit it.
You can use QMimeData.hasUrls() and QMimeData.urls(). The latter returns a list of QUrl objects (which are also used for file-paths):
if data.hasUrls() or data.hasImage():
for url in data.urls():
filepath = unicode(url.toLocalFile())
print(filepath)
elif data.hasText():
tex =unicode (data.text())
...
EDIT:
Here is a test script to get clipboard information:
import sys
from PyQt4 import QtCore, QtGui
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.button = QtGui.QPushButton('Get Clipboard Info', self)
self.button.clicked.connect(self.handleButton)
self.edit = QtGui.QTextEdit(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.edit)
layout.addWidget(self.button)
def handleButton(self):
cb = QtGui.QApplication.clipboard()
data = cb.mimeData()
output = []
if data.hasImage():
image = QtGui.QImage(data.imageData())
output.append('Image: size %s' % image.byteCount())
elif data.hasUrls():
output.append('Urls: count %s' % len(data.urls()))
for url in data.urls():
filepath = unicode(url.toLocalFile())
output.append(' %s' % filepath)
elif data.hasText():
output.append('Text: length %s' % len(data.text()))
output.append('')
output.append('Formats: count %s' % len(data.formats()))
for fmt in data.formats():
output.append(' %s' % fmt)
self.edit.setText('\n'.join(output))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.setGeometry(600, 50, 300, 400)
window.show()
sys.exit(app.exec_())

Regular expression to find function definitions in Python file

I have a python project with function definitions written in CamelCase style.
I'm trying to write a script to convert them to snake_case style.
CaseFormatter class
import re
class CaseFormatter:
def __init__(self, file_directory):
self.first_cap_re = re.compile('(.)([A-Z][a-z]+)')
self.all_cap_re = re.compile('([a-z0-9])([A-Z])')
self.functions_dict = {}
self.file_directory = file_directory
self.file = open(file_directory, "r", encoding="UTF-8")
self.file_content = self.file.read()
self.names_dictionary = {}
def convert_camel_case_to_snake_case(self, name):
"""this function convert a camel case name to a snake case name """
s1 = self.first_cap_re.sub(r'\1_\2', name)
self.names_dictionary[name] = self.all_cap_re.sub(r'\1_\2', s1).lower()
def find_camel_case_functions(self):
"""this function finds camel case functions in a file """
s1 = re.findall("^\s*def (\S+)\s*\(\s*\S+\s*(?:,\s*\S+)*\):$", self.file_content)
return s1
def replace_functions(self):
# file_content = open(self.file_directory, "r", encoding="UTF-8").read()
self.file_content = self.file_content.replace(";", "")
for key, val in self.names_dictionary.items():
self.file_content = self.file_content.replace(key, val)
# print(self.file_content)
self.file = open(self.file_directory, "w", encoding="UTF-8")
print(self.file_content)
self.file.write(self.file_content)
testing the CaseFormatter class
import os
from CaseFormatter import *
walk_dir = 'some dirctory'
print('walk_dir = ' + walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
for root, subDirs, files in os.walk(walk_dir):
print('--\nroot = ' + root)
for filename in files:
file_path = os.path.join(root, filename)
if filename.endswith('.py') and filename != "__init__.py":
print('\t- file %s (full path: %s)' % (filename, file_path))
case_formatter = CaseFormatter(file_path)
# print(case_formatter.find_camel_case_functions())
for function_name in case_formatter.find_camel_case_functions():
case_formatter.convert_camel_case_to_snake_case(function_name)
print(case_formatter.names_dictionary)
case_formatter.replace_functions()
I found the RegEx to find function definitions here.
When I tried it on my project it gave me no results, the RegEx didn't work as I think.
As an example of one of the files in the project:
class UnvoweledPattern(object):
String = ''
Rules = []
IDs = []
def __init__(self, string, rules, ids):
self.String = string
self.Rules = rules
self.IDs = ids
pass
def GetRootsStringsAndRules(self, string):
if (string == None):
string = self.String
rootStrings = []
rootRules = []
for j in range(len(self.Rules)):
rootRule = ''
rootString = ''
for k in range(len(self.Rules[j])):
rootRule += self.Rules[j][k]
if self.Rules[j][k].isdigit():
rootString += string[int(self.Rules[j][k]) - 1]
else:
rootString += self.Rules[j][k]
rootStrings.append(rootString)
rootRules.append(rootRule)
return [rootStrings, rootRules]

Unicode and UTF-8 in python to generate csv file

I'm trying to write some variableas in a csv file.
Documentation (https://docs.python.org/2/library/csv.html) say the all input must be UTF-8 or ASCII, but I don't know how to set the encode ( I already tried .decode('utf-8'))
part of the code:
def get_events():
global SEVERITY, SEVERITY
get_host()
for HID,HNAME in zip(HOSTID,HOSTNAME):
EVENT = zapi.event.get(time_from=DATE_FROM,
time_till=DATE_TILL,
output='extend',
source='0',
hostids=HID,
select_acknowledges='extend',
)
for t in EVENT:
TRIGGERID = t['objectid']
TRIGGER = zapi.trigger.get(output='extend',
triggerids=TRIGGERID,
expandDescription='true',
)
for T in TRIGGER:
TRIGGER_D = T['description'].encode('utf-8')
SEVERITY = T['priority']
if int(SEVERITY) < 3 or TRIGGER_D.decode('utf-8') == 'Zabbix agent on %s is unreachable for 8 minutes' % HNAME or TRIGGER_D.decode('utf-8') == '%s jmx is not reachable' % HNAME:
continue
NS = t['ns']
HOUR = t['clock']
if t.get('value') == "0":
STATUS = "OK"
elif t.get('value')== "1":
STATUS = "PROBLEM"
else:
STATUS = "UNKNOWN"
if t['acknowledged'] == "1":
LISTA_DIC = t['acknowledges'][0]
USER = LISTA_DIC['alias']
MESSAGE = LISTA_DIC["message"]
else:
MESSAGE = ""
USER = ""
with open(FILE_OUT, 'wb') as FILE:
FILE = csv.writer(FILE,delimiter=';')
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D, SEVERITY,MESSAGE,USER)
I'm get the error
“UnicodeDecodeError: 'ascii' codec can't decode byte”
Another thing: How add a newline in
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D, SEVERITY,MESSAGE,USER))
..MESSAGE,USER "\n")) doesn't work
Can solve the problem change the encode to latin-1
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D.encode('latin-1'), SEVERITY,MESSAGE.encode('latin-1'),USER)

django served file returned empty on windows, not on mac

def _tarFiles(filepaths):
print "create tar file from all files in file list and save to temp working dir. returns tarfile path "
try:
savePathDir = settings.TAR_FILE_TARGET_DIRECTORY
if not os.path.exists(savePathDir):
os.makedirs(savePathDir)
tarredfiles = tarfile.open(settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar',mode='w')
for f in filepaths:
tarredfiles.add(f)
tarredfiles.close()
return ("Ok", settings.TAR_FILE_TARGET_DIRECTORY + '/' + 'responsefiles.tar')
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
def sendFiles(files):
try:
result, tarfilename = _tarFiles(files)
if result == 'Ok':
try:
print tarfilename
wrapper = FileWrapper(file(tarfilename))
response = HttpResponse(wrapper, content_type='application/x-tar') #zip,avi,png,jpeg, etc...
response['Content-Disposition'] = 'attachment; filename=' + tarfilename#tarredfiles.name #eg. myfile.zip
response['Content-Length'] = str(os.path.getsize(tarfilename))
return ("Ok",response)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " + e.message, None)
else:
return (result,None)
except Exception as e:
return ("Error in "+ inspect.stack()[0][3] + " " +e.message,None)
tarfilename is the complete path to the file.
The content-length looks right (comparing actual file to getsize).
Works on a mac running runserver. Returns a partial file on windows running runserver. Or completely empty file if I step through on windows.
The target directory and filename generated is "tarred_files/responsefiles.tar"
the file size is 90K and the os.path.getsize returned is 92160
What am I doing that would cause an empty file to be downloaded?
For windows, you need to add "rb" to file.
like so:
wrapper = FileWrapper(file(tarfilename,'rb'))
Also, Content-Length should use a integer, not a string
like so:
response['Content-Length'] = os.path.getsize(tarfilename)