summaryrefslogtreecommitdiffstats
path: root/pydyf/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'pydyf/__init__.py')
-rwxr-xr-xpydyf/__init__.py10
1 files changed, 8 insertions, 2 deletions
diff --git a/pydyf/__init__.py b/pydyf/__init__.py
index 4ad0bde..05dccf6 100755
--- a/pydyf/__init__.py
+++ b/pydyf/__init__.py
@@ -3,10 +3,11 @@ A low-level PDF generator.
"""
+import re
import zlib
from codecs import BOM_UTF16_BE
-VERSION = __version__ = '0.1.1'
+VERSION = __version__ = '0.1.2'
def _to_bytes(item):
@@ -375,7 +376,12 @@ class String(Object):
@property
def data(self):
try:
- return b'(' + _to_bytes(self.string) + b')'
+ # "A literal string is written as an arbitrary number of characters
+ # enclosed in parentheses. Any characters may appear in a string
+ # except unbalanced parentheses and the backslash, which must be
+ # treated specially."
+ escaped = re.sub(rb'([\\\(\)])', rb'\\\1', _to_bytes(self.string))
+ return b'(' + escaped + b')'
except UnicodeEncodeError:
encoded = BOM_UTF16_BE + str(self.string).encode('utf-16-be')
return b'<' + encoded.hex().encode() + b'>'