diff --git a/gramps/plugins/lib/libhtml.py b/gramps/plugins/lib/libhtml.py
index 9690a55fe..c232d1f6b 100644
--- a/gramps/plugins/lib/libhtml.py
+++ b/gramps/plugins/lib/libhtml.py
@@ -202,7 +202,7 @@ class Html(list):
if title is not None:
head += (Html('title', title, inline=True, indent=True))
if html5:
- head += Html('meta', charset="utf-8", indent=True)
+ head += Html('meta', charset=encoding, indent=True)
else:
meta1 = 'http-equiv="content-type" content="text/html;charset=%s"'
meta2 = 'http-equiv="Content-Style-Type" content="text/css"'
diff --git a/gramps/plugins/webreport/narrativeweb.py b/gramps/plugins/webreport/narrativeweb.py
index 3db65999a..8608b318f 100644
--- a/gramps/plugins/webreport/narrativeweb.py
+++ b/gramps/plugins/webreport/narrativeweb.py
@@ -82,12 +82,13 @@ import time, datetime
import locale
import shutil
import io
+import codecs
import tarfile
import tempfile
if sys.version_info[0] < 3:
from cStringIO import StringIO
else:
- from io import StringIO
+ from io import StringIO, BytesIO, TextIOWrapper, BufferedWriter
from textwrap import TextWrapper
from unicodedata import normalize
from collections import defaultdict
@@ -7828,9 +7829,14 @@ class NavWebReport(Report):
else:
self.cur_fname = fname + ext
if self.archive:
- string_io = StringIO()
- of = io.open(fname, "w", encoding = self.encoding,
- errors = 'xmlcharrefreplace')
+ if sys.version_info[0] < 3:
+ string_io = StringIO()
+ of = codecs.EncodedFile(string_io, 'utf-8', self.encoding,
+ 'xmlcharrefreplace')
+ else:
+ string_io = BytesIO()
+ of = TextIOWrapper(string_io, encoding=self.encoding,
+ errors='xmlcharrefreplace')
else:
string_io = None
if subdir:
@@ -7838,8 +7844,22 @@ class NavWebReport(Report):
if not os.path.isdir(subdir):
os.makedirs(subdir)
fname = os.path.join(self.html_dir, self.cur_fname)
- of = io.open(fname, "w", encoding = self.encoding,
- errors = 'xmlcharrefreplace')
+ if sys.version_info[0] < 3:
+ # In python 2.x, the data written by of.write() is genarally of
+ # type 'str' (i.e. 8-bit strings), except for cases where (at
+ # least) one of the objects being converted by a '%' operator is
+ # unicode (e.g. the "Generated by" line or the _META3 line), in
+ # which case the data being written is of type 'unicode' (See
+ # http://docs.python.org/2/library/stdtypes.html#string-
+ # formatting). The data written to the file is encoded according
+ # to self.encoding
+ of = codecs.EncodedFile(open(fname, 'w'), 'utf-8',
+ self.encoding, 'xmlcharrefreplace')
+ else:
+ # In python 3, the data that is written by of.write() is always
+ # of type 'str' (i.e. unicode text).
+ of = open(fname, 'w', encoding=self.encoding,
+ errors='xmlcharrefreplace')
return (of, string_io)
def close_file(self, of, string_io):
@@ -7848,6 +7868,8 @@ class NavWebReport(Report):
"""
if self.archive:
+ if sys.version_info[0] >= 3:
+ of.flush()
tarinfo = tarfile.TarInfo(self.cur_fname)
tarinfo.size = len(string_io.getvalue())
tarinfo.mtime = time.time()