1

I'm retrieving a PDF in the form of a byte array from the internet. Once I recieve it, I've been able to convert it to a File using this method:

File dir = Environment.getExternalStorageDirectory();

    File assist = new File(Environment.getExternalStorageDirectory() + "/Sample.pdf");
    try {
        InputStream fis = new FileInputStream(assist);

        long length = assist.length();
        if (length > Integer.MAX_VALUE) {
            Log.e("Print error", "Too large");
        }
        byte[] bytes = new byte[(int) length];
        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length && (numRead = fis.read(bytes, offset, bytes.length - offset)) >= 0) {
            offset += numRead;
        }

        return new File(dir, "mydemo.pdf");

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

Now that I have the file, what do I do? On the official dev site: https://developer.android.com/training/printing/custom-docs.html it doesn't give specific information for this, doesn't the Print Manager have a method to handle this for us when I give it a File? Or do I have to manually create an adapter? If so, how would I get the page count (which I read is obligatory) and what would I do in onWrite()?

1

1 Answer 1

2

This works for me to print it directly from its URL

    PrintDocumentAdapter pda = new PrintDocumentAdapter() {

    @Override
    public void onWrite(PageRange[] pages, final ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback) {

        !!THIS MUST BE RUN ASYNC!!

        InputStream input = null;
        OutputStream output = null;

        try {
            input = new URL(YOUR URL HERE).openStream();
            output = new FileOutputStream(destination.getFileDescriptor());

            byte[] buf = new byte[1024];
            int bytesRead;

            while ((bytesRead = input.read(buf)) > 0) {
                output.write(buf, 0, bytesRead);
            }

            callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

        } catch (FileNotFoundException ee) {
            //TODO Handle Exception
        } catch (Exception e) {
            //TODO Handle Exception
        } finally {
            try {
                input.close();
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {

        if (cancellationSignal.isCanceled()) {
            callback.onLayoutCancelled();
            return;
        }
        PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("NAME OF DOCUMENT").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
        callback.onLayoutFinished(pdi, true);
    }
};

PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
printManager.print("JOB NAME", pda, null);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.