question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

string argument expected, got 'bytes'

See original GitHub issue

When I try to create a PDF, I get the error “string argument expected, got 'bytes”.

I am using the djangoproject demo on Python 3.5 and am running the pre of xhtml2pdf as instructed in the readme.

The error happens when the following line is reached, which is line 35

        pdf = pisa.CreatePDF(
            StringIO(request.POST["data"]),
            result
            )

More specifically, it seems that the “result” variable on line 37 is not actually a String variable, but rather a Bytes (or so seen by the code). When I run a type() on “result” it returns <class '_io.StringIO'> which seems that it should be correct but is still erroring out.

Django is also highlighting line 183 of the document.py file as to where this error is happening which contains the following:

context.dest.write(data)  # TODO: context.dest is a tempfile as well... 

Is there any workaround that has been found for this or even a known reason as to why it’s happening?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:5
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

27reactions
ghostcommented, May 23, 2016

I’ve solved the issue. Changing line 13 to the following seems to fix the issue:

from io import StringIO, BytesIO

and changing line 34 to the following:

result = BytesIO()
2reactions
ianspricecommented, Dec 28, 2016

Any further thoughts on this? It is blocking for me and the fix above isn’t helping. Here’s a traceback (inline css in template)-

Internal Server Error: /bc/draw/830__ball__CS____+__1138/
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/bc/views.py", line 627, in drawing
pisa.CreatePDF(html, dest=file1, link_callback=callbacky)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/document.py", line 97, in pisaDocument
encoding, context=context, xml_output=xml_output)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/document.py", line 59, in pisaStory
pisaParser(src, context, default_css, xhtml, encoding, xml_output)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/parser.py", line 702, in pisaParser
context.parseCSS()
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/context.py", line 522, in parseCSS
self.css = self.cssParser.parse(self.cssText)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/cssParser.py", line 443, in parse
src, stylesheet = self._parseStylesheet(src)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/cssParser.py", line 531, in _parseStylesheet
src, stylesheetImports = self._parseAtImports(src)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/cssParser.py", line 615, in _parseAtImports
stylesheet = self.cssBuilder.atImport(import_, mediums, self)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/css.py", line 909, in atImport
return cssParser.parseExternal(import_)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/context.py", line 404, in parseExternal
result = self.parse(cssFile.getData())
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/cssParser.py", line 443, in parse
src, stylesheet = self._parseStylesheet(src)
File "/app/.heroku/python/lib/python3.5/site-packages/xhtml2pdf/w3c/cssParser.py", line 523, in _parseStylesheet
src = self.re_comment.sub('', src)
TypeError: cannot use a string pattern on a bytes-like object
Read more comments on GitHub >

github_iconTop Results From Across the Web

string argument expected, got 'bytes' in buffer.write
io.StringIO is for unicode text, its counterpart for bytes is io.BytesIO . As your undelying file is a binary jpg, you really should...
Read more >
TypeError: string argument expected, got 'bytes' #552 - GitHub
Hi, document.save() receives a file path or stream, right? What am I doing wrong? This a flask application.... `@app.route('/doc') def ...
Read more >
(SOLVED) string argument expected, got 'bytes'
Re: TypeError: string argument expected, got 'bytes' ... StringIO() expects write() to send text strings, to send byte strings use io.
Read more >
quopri.decode(): string argument expected, got 'bytes'
3. On protocol. A protocol: write_bytes() and write_string() methods that would have been raising type error in case of text_file.write_bytes() ...
Read more >
python3 PIL提示TypeError: string argument expected, got 'bytes'
python3 PIL提示TypeError: string argument expected, got 'bytes',用BytesIO替代StringIO即可解决 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found