It is easy enough to create a separate class for the Py2 version.

This commit is contained in:
Stephan Richter 2017-05-22 12:41:52 -04:00 committed by Johannes 'josch' Schauer
parent 719928f5f5
commit 06560cd0d4

View file

@ -29,6 +29,8 @@ from enum import Enum
from io import BytesIO from io import BytesIO
import logging import logging
PY3 = sys.version_info[0] >= 3
__version__ = "0.2.3" __version__ = "0.2.3"
default_dpi = 96.0 default_dpi = 96.0
papersizes = { papersizes = {
@ -269,17 +271,27 @@ class MyPdfWriter():
self.addobj(page) self.addobj(page)
class MyPdfString(): if PY3:
@classmethod class MyPdfString():
def encode(cls, string): @classmethod
try: def encode(cls, string):
string = string.encode('ascii') try:
except UnicodeEncodeError: string = string.encode('ascii')
string = b"\xfe\xff"+string.encode("utf-16-be") except UnicodeEncodeError:
string = string.replace(b'\\', b'\\\\') string = b"\xfe\xff"+string.encode("utf-16-be")
string = string.replace(b'(', b'\\(') string = string.replace(b'\\', b'\\\\')
string = string.replace(b')', b'\\)') string = string.replace(b'(', b'\\(')
return b'(' + string + b')' string = string.replace(b')', b'\\)')
return b'(' + string + b')'
else:
class MyPdfString(object):
@classmethod
def encode(cls, string):
# This mimics exactely to what pdfrw does.
string = string.replace(b'\\', b'\\\\')
string = string.replace(b'(', b'\\(')
string = string.replace(b')', b'\\)')
return b'(' + string + b')'
class pdfdoc(object): class pdfdoc(object):