Skip to content

Commit 9c27001

Browse files
committed
Force decode input string to unicode on python 2
Fixes #42
1 parent afa1efc commit 9c27001

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

pdfkit/source.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# -*- coding: utf-8 -*-
22
import os
33
import io
4+
try:
5+
# Python 2.x and 3.x support for checking string types
6+
assert basestring
7+
assert unicode
8+
except NameError:
9+
basestring = str
10+
unicode = str
11+
412

513

614
class Source(object):
@@ -38,4 +46,12 @@ def isFileObj(self):
3846
return hasattr(self.source, 'read')
3947

4048
def to_s(self):
41-
return self.source
49+
# String should be in unicode(python2)/str(python3) type since we will
50+
# later encode it to utf-8 bytes array to pipe into subprocess
51+
# With some charachters on python 2 it sets to str type (bytes) which is wrong
52+
# and cant later encode properly, this is a workaround for this.
53+
# See issue #42
54+
if isinstance(self.source, unicode):
55+
return self.source
56+
else:
57+
return unicode(self.source, 'utf-8')

0 commit comments

Comments
 (0)