I don't know how to do this for MuPDF, but you can combine multiple PDF Files on Android with with the latest Apache PdfBox Release. (At the moment still not final... RC3...)
Just add this dependency to your build.gradle:
compile 'org.apache.pdfbox:pdfbox:2.0.0-RC3'
And in an async task do this:
private File downloadAndCombinePDFs(String urlToPdf1, String urlToPdf2, String urlToPdf3 ) throws IOException {
PDFMergerUtility ut = new PDFMergerUtility();
ut.addSource(NetworkUtils.downloadFile(urlToPdf1, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf2, 20));
ut.addSource(NetworkUtils.downloadFile(urlToPdf3, 20));
final File file = new File(getContext().getExternalCacheDir(), System.currentTimeMillis() + ".pdf");
final FileOutputStream fileOutputStream = new FileOutputStream(file);
try {
ut.setDestinationStream(fileOutputStream);
ut.mergeDocuments(MemoryUsageSetting.setupTempFileOnly());
} finally {
fileOutputStream.close();
}
return file;
}
Here NetworkUtils.downloadFile() should return an InputStream.
If you have them on your sdcard you can open a FileInputStream.
I download the PDFs from the internet like this:
public static InputStream downloadFileThrowing(String url, int timeOutInSeconds) throws IOException {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(timeOutInSeconds, TimeUnit.SECONDS);
client.setReadTimeout(timeOutInSeconds, TimeUnit.SECONDS);
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Download not successful.response:" + response);
else
return response.body().byteStream();
}
To use the OkHttpClient add this to your build.gradle:
compile 'com.squareup.okhttp:okhttp:2.7.2'
Note:
This works not with encrypted files. To combine encrypted files you should first decrypt all single files and later encrypt the merged pdf.