android.print.pdf.PrintedPdfDocument Java Examples

The following examples show how to use android.print.pdf.PrintedPdfDocument. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IdentityPrintDocumentAdapter.java    From secure-quick-reliable-login with MIT License 6 votes vote down vote up
@Override
public void onLayout(
        PrintAttributes oldAttributes,
        PrintAttributes newAttributes,
        CancellationSignal cancellationSignal,
        LayoutResultCallback callback,
        Bundle metadata
) {
    mPdfDocument = new PrintedPdfDocument(activity, newAttributes);
    if (cancellationSignal.isCanceled() ) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo info = new PrintDocumentInfo
            .Builder("Identity.pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1)
            .build();
    callback.onLayoutFinished(info, true);
}
 
Example #2
Source File: RescueCodePrintDocumentAdapter.java    From secure-quick-reliable-login with MIT License 6 votes vote down vote up
@Override
public void onLayout(
        PrintAttributes oldAttributes,
        PrintAttributes newAttributes,
        CancellationSignal cancellationSignal,
        LayoutResultCallback callback,
        Bundle metadata
) {
    mPdfDocument = new PrintedPdfDocument(activity, newAttributes);
    if (cancellationSignal.isCanceled() ) {
        callback.onLayoutCancelled();
        return;
    }

    PrintDocumentInfo info = new PrintDocumentInfo
            .Builder("RescueCode.pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1)
            .build();
    callback.onLayoutFinished(info, true);
}
 
Example #3
Source File: PdfPrinter.java    From ViewPrinter with Apache License 2.0 4 votes vote down vote up
/**
 * Prints the current view to a PDF file, in the given directory and with the given
 * filename. If the file exists, it will be deleted.
 *
 * @param printId an (optional) identifier for the process
 * @param directory a directory where the file will be saved
 * @param filename the output file name
 */
@Override
public void print(final String printId, @NonNull final File directory, @NonNull String filename) {
    Context context = mDocument.getContext();
    if (!checkPermission(context, directory)) return;
    if (!checkPreview(printId, directory, filename)) return;
    if (!filename.toLowerCase().endsWith(".pdf")) filename += ".pdf";
    final File file = new File(directory, filename);
    if (!checkFile(printId, file)) return;

    if (mDocument.getPageCount() == 0) return;
    DocumentPage firstPage = mDocument.getPageAt(0);
    PrintSize size = mDocument.getPrintSize();

    // Create doc
    PrintAttributes attrs = new PrintAttributes.Builder()
            .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
            .setMediaSize(size.toMediaSize(firstPage))
            .setMinMargins(PrintAttributes.Margins.NO_MARGINS)
            .build();
    final PrintedPdfDocument doc = new PrintedPdfDocument(context, attrs);

    // Print page
    // Page canvas is passed in PostScript points. In order not to break View drawing,
    // we must scale that up back to pixels.
    dispatchOnPrePrint(mDocument);
    for (int i = 0; i < mDocument.getPageCount(); i++) {
        PdfDocument.Page page = doc.startPage(i);
        Canvas canvas = page.getCanvas();
        float pixelsToPoints = PrintSize.PIXELS_TO_INCHES(context) * PrintSize.INCHES_TO_POINTS;
        canvas.scale(pixelsToPoints, pixelsToPoints, 0, 0);

        DocumentPage view = mDocument.getPageAt(i);
        Drawable background = null;
        if (!mPrintBackground) {
            background = view.getBackground();
            view.setBackground(null);
        }
        view.draw(canvas);
        if (!mPrintBackground) {
            view.setBackground(background);
        }
        doc.finishPage(page);
    }
    dispatchOnPostPrint(mDocument);

    // I am not sure if the above would work with any view. Some views might be checking for
    // canvas.getWidth() or canvas.tryGetHeight(), which now are not consistent. If errors show up,
    // we might have to draw on a separate canvas and then scale the bitmap.
    // This has other drawbacks: the original pdf canvas, for example, takes text as text and
    // makes it selectable in the final PDF.

    // Print in a separate thread.
    // Since we're API 19 we can use try with resources.
    final Handler ui = new Handler();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(file))) {
                doc.writeTo(stream);
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onPrint(printId, file);
                    }
                });

            } catch (final IOException e) {
                ui.post(new Runnable() {
                    @Override
                    public void run() {
                        mCallback.onPrintFailed(printId, new RuntimeException("Invalid file: " + file, e));
                    }
                });
            }
        }
    }, TAG + "Worker").start();
}