Writing to StringIO in Python 3.4.1

637 views
Skip to first unread message

contracode

unread,
Sep 11, 2014, 5:13:18 PM9/11/14
to reportl...@googlegroups.com
Hello!

I'm working with code that implements an in-memory file-like object. I've been working through several examples from Stackoverflow and a few Python/Django development blogs that give the following example, or something similar to it, using Python 2.7 and PyPDF.

from pyPdf import PdfFileWriter, PdfFileReader
import StringIO
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter

buffer
= StringIO.StringIO() # create a new PDF with Reportlab c = canvas.Canvas(packet, pagesize=letter) c.drawString(100,100, "Hello world") c.save() #move to the beginning of the StringIO buffer buffer.seek(0) new_pdf = PdfFileReader(buffer)

My implementation requires me to use PyPDF2 and Python 3.4.x. Given similar code, I receive the exception:

TypeError: string argument expected, got 'bytes'

Using:

from io import StringIO
from reportlab.pdfgen import canvas

buffer = StringIO()
c = canvas.Canvas(buffer)
c.drawString(100, 100, "Hello world")
c.save()


Python 3.4.1 on Windows 7 64-bit, Reportlab 3.1.8 installed via pip.

Can anyone help me get through the Canvas.save() call? What am I doing wrong here?

Esben Rugbjerg

unread,
Mar 27, 2018, 7:32:10 AM3/27/18
to reportlab-users
Hi,

I have exact the same problem. Did you found a solution ?

BR
Esben
Reply all
Reply to author
Forward
0 new messages