NativeScript中PDF打印的Android实现

标签 android nativescript

在我的 NativeScript-Vue 应用程序中,我需要将 PDF 文档打印到蓝牙打印机,并接收回调,无论打印成功还是取消。插件nativescript-printer在 iOS 上可以完美处理它,但在 Android 上它不会返回回调 (the feature is not implemented) 。该插件使用类 PrintHelper ,它有一个回调,在成功和取消时都会被调用,不带参数和返回。

看来唯一的解决办法就是通过类PrintManager来实现打印。一些来源:

这就是我尝试过的。 onWriteonLayout 可以工作,但是 onStartonFinish (这是我的目标)永远不会被调用。

import * as application from "tns-core-modules/application";

function printPdf(pdfFilePath) { // path: "/data/user/0/com.myapp.test/cache/pdf/document1.pdf"
    let printManager = application.android.foregroundActivity.getSystemService(android.content.Context.PRINT_SERVICE);
    let jobName = "PrintPdf";
    let PrintPDFAdapter = android.print.PrintDocumentAdapter.extend({
        onStart() {
            console.log("on start);
        },

        onWrite(pages, destination, cancellationSignal, callback) {
            let input;
            let output;
            try {
                input = new java.io.FileInputStream(new java.io.File(pdfFilePath));
                output = new java.io.FileOutputStream(destination.getFileDescriptor());

                let buf = new Array.create("byte", 1024);
                let bytesRead;
                while ((bytesRead = input.read(buf)) > 0) {
                    output.write(buf, 0, bytesRead);
                }

                callback.onWriteFinished(new android.print.PageRange(0, 0));
            } catch (e){
                console.error(e);
            } finally {
                try {
                    input.close();
                    output.close();
                } catch (e) {
                    console.error(e);
                }
            }
        },

        onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras){
            try {
                if (cancellationSignal.isCanceled()) {
                    callback.onLayoutCancelled();
                    return;
                }

                let pdi = new android.print.PrintDocumentInfo.Builder("print_output.pdf").setContentType(android.print.PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

                callback.onLayoutFinished(pdi, true);
            } catch (e) {
                console.error(e);
            }
        },

        onFinish() {
            console.log("on finish");
        }
    });

    let pda = new PrintPDFAdapter();

    printManager.print(jobName, pda, null);
}

最佳答案

printManager.print() 返回一个 PrintJob 对象,该对象公开了当前的打印状态。这不太好,但这是我的解决方法:

function printPDF(pdfFilePath) {

    // above code

    let printJob = printManager.print(jobName, pda, null);

    let onFinish = function(status) {
        resolve(status);
        clearInterval(interval);
    }
    let interval = setInterval(() => {
        let state = printJob.getInfo().getState();
        console.log(state);
        if (state === 6) onFinish("print failed");
        if (state === 7) onFinish("print cancelled");
        if (state === 5) onFinish("print completed");
    }, 500);
}

如果 PrintJob 状态卡在排队或阻塞状态,我可能会在时间间隔上实现超时。

关于NativeScript中PDF打印的Android实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61099335/

相关文章:

html - Nativescript ScrollView 被 gridview 裁剪

android - 解决 4.3 及以下版本的 Android webview 问题

android - NoSuchMethodError使用Kotlin与新的Android数据绑定(bind)

带有自定义布局的 Android 对话框将组件压缩在一起

android - 如何在 Android Studio 中停止跟踪被忽略的文件

javascript - pageLoaded 函数未触发

java - SharedPreferences 不保存 boolean 值

javascript - 在 Android NativeScript 中发出不安全的 http 请求

javascript - 如何在 NativeScript 中更改 StackLayout 容器的背景颜色?

angular - 如何在 chrome 浏览器中调试 nativescript 应用程序并在 chrome 中查看输出