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. 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 =, 0)
val requestBody = data.toRequestBody("application/pdf".toMediaType())
val request = Request.Builder().url("").post(requestBody).build()
val client = OkHttpClient()
val response = client.newCall(request).execute()
final PdfDocument document = activity.requirePdfFragment().getDocument();
if (document == null) return;
final DataProvider dataProvider = document.getDocumentSource().getDataProvider();
if (dataProvider == null) return;

final byte[] data =, 0);
final RequestBody requestBody = RequestBody.create(data, MediaType.parse("application/pdf"));
final Request request = new Request.Builder().url("").post(requestBody).build();
final OkHttpClient client = new OkHttpClient();
final Response response = client.newCall(request).execute();