[reportlab-users] Writing to stdout

113 views
Skip to first unread message

Rob Allen

unread,
Feb 28, 2021, 12:56:57 PM2/28/21
to reportl...@reportlab.com
Hi,

I’m trying to write to stdout and it’s not working.

I’m using Python 3.8.6 with a clean virtualenv into which I ran `pip install reportlab`.


$ python --version
Python 3.8.6

$ pip freeze
Pillow==8.1.0
reportlab==3.5.59


This is my test application, based on page 11 of the manual where I changed the filename to sys.stdout:

$ cat test.py
import sys
from reportlab.pdfgen import canvas

def hello(c):
c.drawString(100, 100, "Hello World")

c = canvas.Canvas(sys.stdout)
hello(c)
c.showPage()
c.save()


This is what happens when I run it:

$ python test.py
Traceback (most recent call last):
File "test.py", line 10, in <module>
c.save()
File "/Users/rob/.pyenv/versions/py386/lib/python3.8/site-packages/reportlab/pdfgen/canvas.py", line 1277, in save
self._doc.SaveToFile(self._filename, self)
File "/Users/rob/.pyenv/versions/py386/lib/python3.8/site-packages/reportlab/pdfbase/pdfdoc.py", line 222, in SaveToFile
f.write(data)
TypeError: write() argument must be str, not bytes


Am I specifying the stdout stream incorrectly or missing something else?


Regards,

Rob


--
Development thoughts at https://akrabat.com
Daily Jotter for macOS at https://dailyjotter.com
rst2pdf: https://rst2pdf.org


_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
https://pairlist2.pair.net/mailman/listinfo/reportlab-users

Christoph Zwerschke

unread,
Feb 28, 2021, 1:40:42 PM2/28/21
to reportl...@lists2.reportlab.com
On 28.02.2021 18:55, Rob Allen wrote:
> This is what happens when I run it:
>
> ...
> TypeError: write() argument must be str, not bytes


This is probably because stdout is expecting text (native strings), but
ReportLab sends bytes.

It may work when replacing sys.stdout with something like
TextIOWrapper(sys.stdout.buffer, encoding='utf8').

-- Christoph

Tim Roberts

unread,
Feb 28, 2021, 2:52:00 PM2/28/21
to reportlab-users, reportl...@reportlab.com
On Feb 28, 2021, at 9:55 AM, Rob Allen <r...@akrabat.com> wrote:
>
> This is my test application, based on page 11 of the manual where I changed the filename to sys.stdout:
>
> $ cat test.py
> import sys
> from reportlab.pdfgen import canvas
>
> def hello(c):
> c.drawString(100, 100, "Hello World")
>
> c = canvas.Canvas(sys.stdout)
> hello(c)
> c.showPage()
> c.save()

sys.stdout.buffer has a “write” member, You should be able to say

c = canvas.Canvas(sys.stdout.buffer)

Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Robin Becker

unread,
Feb 28, 2021, 2:59:05 PM2/28/21
to reportlab-users, Christoph Zwerschke, r...@akrabat.com
I think this is almost the right answer Christof, but since PDF is not a textual file format I think the correct way to
overcome the python3 encode everything is to use the buffer directly eg

sys.stdout.buffer is a binary file

see https://docs.python.org/3.9/glossary.html#term-binary-file


so rob you should use

c = canvas.Canvas(sys.stdout.buffer)
--
Robin Becker

Christoph Zwerschke

unread,
Feb 28, 2021, 3:25:28 PM2/28/21
to reportlab-users
On 28.02.2021 20:58, Robin Becker wrote:
> I think this is almost the right answer Christof, but since PDF is not
> a textual file format I think the correct way to overcome the python3
> encode everything is to use the buffer directly eg

Ah, yes, you're right. This should not be interpreted as being utf-8.

Rob Allen

unread,
Mar 1, 2021, 4:05:15 AM3/1/21
to reportl...@reportlab.com
On 28 Feb 2021, at 19:58, Robin Becker <ro...@reportlab.com> wrote:
>
> so rob you should use
>
> c = canvas.Canvas(sys.stdout.buffer)
>

Thank you. This worked perfectly.

Regards,

Rob
Reply all
Reply to author
Forward
0 new messages