utils.py: add makeFileName and use in pdfdoc.py
authorrobin
Mon, 29 Apr 2013 11:19:28 +0100
changeset 3687 1fead6079288
parent 3686 0ef2cb9578d7
child 3688 e0be99f793d4
child 3720 7a059dde5bf5
utils.py: add makeFileName and use in pdfdoc.py
src/reportlab/lib/utils.py
src/reportlab/pdfbase/pdfdoc.py
tests/test_pdfbase_encodings.py
--- a/src/reportlab/lib/utils.py	Mon Apr 22 17:48:33 2013 +0100
+++ b/src/reportlab/lib/utils.py	Mon Apr 29 11:19:28 2013 +0100
@@ -1190,3 +1190,10 @@
         for k,v in kwds.iteritems():
             setattr(self,k,v)
         return self
+
+def makeFileName(s):
+    '''force filename strings to unicode so python can handle encoding stuff'''
+    assert isinstance(s,basestring),"filename is %r should be str or unicode" % s
+    if isinstance(s,str):
+        s = s.decode('utf8')
+    return s
--- a/src/reportlab/pdfbase/pdfdoc.py	Mon Apr 22 17:48:33 2013 +0100
+++ b/src/reportlab/pdfbase/pdfdoc.py	Mon Apr 29 11:19:28 2013 +0100
@@ -18,7 +18,7 @@
 from reportlab.pdfbase import pdfutils
 from reportlab.pdfbase.pdfutils import LINEEND # this constant needed in both
 from reportlab import rl_config
-from reportlab.lib.utils import import_zlib, open_for_read, fp_str, _digester
+from reportlab.lib.utils import import_zlib, open_for_read, fp_str, _digester, makeFileName
 from reportlab.pdfbase import pdfmetrics
 try:
     from hashlib import md5
@@ -228,10 +228,10 @@
         if hasattr(getattr(filename, "write",None),'__call__'):
             myfile = 0
             f = filename
-            filename = utf8str(getattr(filename,'name',''))
+            filename = makeFileName(getattr(filename,'name',''))
         else :
             myfile = 1
-            filename = utf8str(filename)
+            filename = makeFileName(filename)
             f = open(filename, "wb")
         f.write(self.GetPDFData(canvas))
         if myfile:
--- a/tests/test_pdfbase_encodings.py	Mon Apr 22 17:48:33 2013 +0100
+++ b/tests/test_pdfbase_encodings.py	Mon Apr 29 11:19:28 2013 +0100
@@ -83,6 +83,14 @@
         self.assertNear(pdfmetrics.stringWidth(testUTF8, 'Vera', 10),279.809570313)
         self.assertNear(pdfmetrics.stringWidth(testUni, 'Vera', 10),279.809570313)
 
+    def testUtf8FileName(self):
+        fn=outputfile('test_pdfbase_utf8_filename').decode('utf8')
+        fn += u'_portr\xe4t.pdf'
+        print repr(fn)
+        c = Canvas(fn)
+        c.drawString(100,700, u'Filename='+fn)
+        c.save()
+
     def testUtf8Canvas(self):
         """Verify canvas declared as utf8 autoconverts.