javaandroidpdfprintingprintdocument

Codes to print PDF not working at the last step


I am writing an app that can create Maths worksheet with answer pages in PDF format. I want to add a print function to allow printing the worksheet directly from the app.

In the PrintDocumentAdapter, the first half of the codes check the color of a certain pixel of the page to determine whether it is an answer page and reduce the number of pages to print accordingly. It then constructs a printingPDF document and sends it to print using 's writeTo() method in the onWrite().

Everything works fine until the last part. It reads the PDF correctly, checks the number of answer pages correctly, and sends a correct PDF with question pages only to the printing page. If I choose "Save as PDF" in the printing page, it saves the pages to a new PDF file with no problem. However, if I try to print with my printer that is connected to my phone with USB by clicking the print button, the printing page simply disappears and goes back to the main page of my app. (It is supposed to past those few pages to the printer app.) I tried several different android printer driver apps and the result was the same.

private void printWS() throws IOException {
    findViewById(R.id.defaultTextView).setVisibility(View.INVISIBLE);
    if (!loadedFile) {
        File file = new File(getApplicationContext().getCacheDir(), getString(R.string.defaultFileName));
        parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
    } else {
        parcelFileDescriptor = getContentResolver().openFileDescriptor(pdfUri, "r");
    }
    // Start printing
    PrintManager printManager = (PrintManager) this.getSystemService(PRINT_SERVICE);
    String jobName = getString(R.string.app_name);
    PrintAttributes printAttributes = new PrintAttributes.Builder().setMediaSize(PrintAttributes.MediaSize.ISO_A4).build();
    printManager.print(jobName, new PrintDocumentAdapter() {


        int finalTotal;


        @Override
        public void onStart() {
            if (parcelFileDescriptor != null) {
                try {
                    pdfRenderer = new PdfRenderer(parcelFileDescriptor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            int tempTotal = pdfRenderer.getPageCount();
            Bitmap[] tempBitmap = new Bitmap[tempTotal];
            finalTotal = tempTotal;
            for (int pageNum = 0; pageNum < tempTotal; pageNum++) {
                PdfRenderer.Page tempPage = pdfRenderer.openPage(pageNum);
                tempBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
                tempPage.render(tempBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
                if (tempBitmap[pageNum].getPixel(0, 0) == Color.GRAY) {
                    finalTotal--;
                }
                tempPage.close();
            }
        }


        PrintedPdfDocument printingPDF;


        @Override
        public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle bundle) {
            printingPDF = new PrintedPdfDocument(getApplicationContext(), newAttributes);
            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }
            if (finalTotal > 0) {
                PrintDocumentInfo info = new PrintDocumentInfo
                        .Builder("Worksheet")
                        .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                        .setPageCount(finalTotal)
                        .build();
                callback.onLayoutFinished(info, true);
            } else {
                callback.onLayoutFailed("Page count calculation failed.");
            }
        }


        @Override
        public void onWrite(PageRange[] pageRanges, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            Bitmap[] loadedBitmap = new Bitmap[finalTotal];
            for (int pageNum = 0; pageNum < finalTotal; pageNum++) {
                PdfDocument.PageInfo printingPageInfo = new PdfDocument.PageInfo.Builder(WS_WIDTH, WS_HEIGHT, pageNum).create();
                PdfDocument.Page printingPage = printingPDF.startPage(printingPageInfo);
                if (cancellationSignal.isCanceled()) {
                    callback.onWriteCancelled();
                    printingPDF.close();
                    printingPDF = null;
                    return;
                }
                PdfRenderer.Page loadedPage = pdfRenderer.openPage(pageNum);
                loadedBitmap[pageNum] = Bitmap.createBitmap(WS_WIDTH, WS_HEIGHT, Bitmap.Config.ARGB_8888);
                loadedPage.render(loadedBitmap[pageNum], null, null, PdfRenderer.Page.RENDER_MODE_FOR_PRINT);
                Canvas canvas = printingPage.getCanvas();
                Paint paint = new Paint();
                canvas.drawBitmap(loadedBitmap[pageNum], 0, 0, paint);
                printingPDF.finishPage(printingPage);
                loadedPage.close();
            }
            try {
                printingPDF.writeTo(new FileOutputStream(destination.getFileDescriptor()));
            } catch (IOException e) {
                callback.onWriteFailed(e.toString());
                return;
            } finally {
                printingPDF.close();
                printingPDF = null;
            }
            PageRange[] writtenPages = new PageRange[]{PageRange.ALL_PAGES};
            callback.onWriteFinished(writtenPages);
        }


        @Override
        public void onFinish() {
            try {
                pdfRenderer.close();
                parcelFileDescriptor.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, printAttributes);
}

Can someone take a look at my codes and tell me what is going wrong? Please help! Thanks!


Solution

  • Nevermind. It turned out the codes are ok. What I needed to do was go to the settings of my phone and change the "Display pop-up windows while running in the background" of the printer driver app to "allow", then it worked perfectly!

    I will keep the question and answer here in case someone encounters the same problem.