--- a/eric6/QScintilla/Exporters/ExporterPDF.py Tue Oct 13 19:02:26 2020 +0200 +++ b/eric6/QScintilla/Exporters/ExporterPDF.py Wed Oct 14 17:50:39 2020 +0200 @@ -543,74 +543,73 @@ try: with E5OverrideCursor(): # save file in win ansi using cp1250 - f = open(filename, "w", encoding="cp1250", - errors="backslashreplace") - - # initialise PDF rendering - ot = PDFObjectTracker(f) - self.pr.oT = ot - self.pr.startPDF() - - # do here all the writing - lengthDoc = self.editor.length() - - if lengthDoc == 0: - self.pr.nextLine() # enable zero length docs - else: - pos = 0 - column = 0 - utf8 = self.editor.isUtf8() - utf8Ch = b"" - utf8Len = 0 + with open(filename, "w", encoding="cp1250", + errors="backslashreplace") as f: + + # initialise PDF rendering + ot = PDFObjectTracker(f) + self.pr.oT = ot + self.pr.startPDF() - while pos < lengthDoc: - ch = self.editor.byteAt(pos) - style = self.editor.styleAt(pos) + # do here all the writing + lengthDoc = self.editor.length() + + if lengthDoc == 0: + self.pr.nextLine() # enable zero length docs + else: + pos = 0 + column = 0 + utf8 = self.editor.isUtf8() + utf8Ch = b"" + utf8Len = 0 - if ch == b'\t': - # expand tabs - ts = tabSize - (column % tabSize) - column += ts - self.pr.add(' ' * ts, style) - elif ch == b'\r' or ch == b'\n': - if ( - ch == b'\r' and - self.editor.byteAt(pos + 1) == b'\n' - ): - pos += 1 - # close and begin a newline... - self.pr.nextLine() - column = 0 - else: - # write the character normally... - if ord(ch) > 127 and utf8: - utf8Ch += ch - if utf8Len == 0: - if (utf8Ch[0] & 0xF0) == 0xF0: - utf8Len = 4 - elif (utf8Ch[0] & 0xE0) == 0xE0: - utf8Len = 3 - elif (utf8Ch[0] & 0xC0) == 0xC0: - utf8Len = 2 - column -= 1 - # will be incremented again later - elif len(utf8Ch) == utf8Len: - ch = utf8Ch.decode('utf8') - self.pr.add(ch, style) - utf8Ch = b"" - utf8Len = 0 + while pos < lengthDoc: + ch = self.editor.byteAt(pos) + style = self.editor.styleAt(pos) + + if ch == b'\t': + # expand tabs + ts = tabSize - (column % tabSize) + column += ts + self.pr.add(' ' * ts, style) + elif ch == b'\r' or ch == b'\n': + if ( + ch == b'\r' and + self.editor.byteAt(pos + 1) == b'\n' + ): + pos += 1 + # close and begin a newline... + self.pr.nextLine() + column = 0 + else: + # write the character normally... + if ord(ch) > 127 and utf8: + utf8Ch += ch + if utf8Len == 0: + if (utf8Ch[0] & 0xF0) == 0xF0: + utf8Len = 4 + elif (utf8Ch[0] & 0xE0) == 0xE0: + utf8Len = 3 + elif (utf8Ch[0] & 0xC0) == 0xC0: + utf8Len = 2 + column -= 1 + # will be incremented again later + elif len(utf8Ch) == utf8Len: + ch = utf8Ch.decode('utf8') + self.pr.add(ch, style) + utf8Ch = b"" + utf8Len = 0 + else: + column -= 1 + # will be incremented again later else: - column -= 1 - # will be incremented again later - else: - self.pr.add(ch.decode(), style) - column += 1 - - pos += 1 - - # write required stuff and close the PDF file - self.pr.endPDF() - f.close() + self.pr.add(ch.decode(), style) + column += 1 + + pos += 1 + + # write required stuff and close the PDF file + self.pr.endPDF() except IOError as err: E5MessageBox.critical( self.editor,