use BytesIO instead of StringIO for pdf generation