Save PDFs to a remote server on Android

If you already have a document saved locally and you want to commit it to a remote server once a user is done editing it, you can achieve this by uploading the contents of the document. Below is an example that uses OkHttp(opens in a new tab). We chose to use it because it’s a common dependency most projects already have, but you’re by no means required to use it if you have a different networking setup already in place:

val dataProvider = activity.requirePdfFragment().document?.documentSource?.dataProvider ?: return
val data = dataProvider.read(dataProvider.size, 0)
val requestBody = data.toRequestBody("application/pdf".toMediaType())
val request = Request.Builder().url("http://127.0.0.1:12345").post(requestBody).build()
val client = OkHttpClient()
val response = client.newCall(request).execute()