Sunday, April 28, 2019

Re: Generate PDF using reportlab and write to s3 bucket without saving locally

Figured it out: streams have this builtin position attr which bytesIO uses to keep track of to know where to insert data. Reportlab simpledoctemplate writes to the stream object, but when it completes pdf generation at doc.build(story), it doesn't reset the stream position back to the beginning of the file. as a result the s3 put_object method writes only the portion of the file from where the position is at currently, through to the end, resulting in corrupt file. all that was neeeded was to reset the stream position just before writing to s3 using stream.seek(0)

On Monday, 22 April 2019 21:18:14 UTC+10, Danny Blaker wrote:
I'm trying to generate a PDF using reportlab and write to s3 bucket without saving locally

I know I'm missing something simple:

# create a stream
stream = io.BytesIO()

# generate PDF
doc = SimpleDocTemplate(stream, pagesize=letter,
                        rightMargin=72, leftMargin=72,
                        topMargin=72, bottomMargin=18)
Story = []

styles = getSampleStyleSheet()

Story.append(Paragraph('<font size=11>This is a PDF</font>', styles["Normal"]))

doc.build(Story)

# get buffer
pdf_buffer = stream.getbuffer()

filename = "new.pdf"
bucket_name = 'insert_bucket_name'
object_name = bucket_name

# here is where I get stuck - how should be passing the pdf_buffer to s3?

# how you typically write to s3 :
# Method 1

s3 = boto3.client('s3')
with open(filename, "rb") as f:
    s3.upload_fileobj(f, bucket_name, object_name)

# Method 2
s3.Bucket(bucket_name).put_object(Key=filename, Body=file)

Any ideas most appreciated!

Thanks!

here are some related resources:


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c7dab075-5edb-41ac-932b-090d8738a7e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment