I don’t know how to do this for MuPDF, but you can combine several PDF files on Android with the latest Apache PdfBox Release , (Not final yet ... RC3 ...)
Just add this dependency to your build.gradle:
compile 'org.apache.pdfbox:pdfbox:2.0.0-RC3'
And in the asynchronous task do the following:
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 an SD card, you can open FileInputStream.
I download PDF files from the Internet as follows:
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 OkHttpClient, add this to your build.gradle:
compile 'com.squareup.okhttp:okhttp:2.7.2'
Note: This does not work with encrypted files. To combine encrypted files, you must first decrypt all the individual files and then encrypt the combined pdf.