繁体   English   中英

Android打印框架-静默打印自定义pdf到预定打印机

[英]Android print framework - Silent print custom pdf to predetermined printer

我正在为Android开发POS应用程序。 我需要做的是单击按钮时打印账单。 打印需要在不与应用程序进行任何交互的情况下静默地发生在预定的打印机上。 将发现打印机的IP地址已在代码中设置。 连接应该发生在真正的wifi或蓝牙(可能是wifi)上。

到目前为止,我尝试使用的是android打印框架(在Android 4.4(API级别19)中),但它似乎被卡在gui上(同样,我不希望任何gui来打印,它只需要发生即可)。 我找不到设置打印机IP真实代码的选项。

我遵循了一个简单的教程来创建我现在拥有的代码。 这是它的链接: http : //www.techotopia.com/index.php/An_Android_Custom_Document_Printing_Tutorial

因此,回顾一下,我需要我的自定义Android POS应用程序才能将帐单(或对于初学者来说只是什么)打印到我在代码中设置的预定打印机。

看来Rongta pos打印机具有android sdk。 我猜想其他制造商也必须为他们的打印机配备一个。

抱歉,您无法在Android中静默打印文档。 AOSP问题跟踪器上有一个未解决的问题。 https://code.google.com/p/android/issues/detail?id=160908

通过调用打印适配器生命周期方法,可以将printint转换为PDF。 但是,由于回调是非公共抽象类,并且如果提供null,则系统将引发segfault,因此您需要使用DexMaker来实现它们。 我已经为这样的webView适配器实现了:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    printAdapter = webView.createPrintDocumentAdapter();
}

@Override
protected Void doInBackground(Void... voids) {

    File file = new File(pdfPath);
    if (file.exists()) {
        file.delete();
    }
    try {
        file.createNewFile();

        // get file descriptor
        descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);

        // create print attributes
        PrintAttributes attributes = new PrintAttributes.Builder()
                .setMediaSize(PrintAttributes.MediaSize.ISO_A4)
                .setResolution(new PrintAttributes.Resolution("id", PRINT_SERVICE, 300, 300))
                .setColorMode(PrintAttributes.COLOR_MODE_COLOR)
                .setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0))
                .build();
        ranges = new PageRange[]{new PageRange(1, numberPages)};

        // dexmaker cache folder
        cacheFolder =  new File(context.getFilesDir() +"/etemp/");

        printAdapter.onStart();

        printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {
            @Override
            public Object invoke(Object o, Method method, Object[] objects) throws Throwable {

                if (method.getName().equals("onLayoutFinished")) {
                    onLayoutSuccess();
                } else {
                    Log.e(TAG, "Layout failed");
                    pdfCallback.onPdfFailed();
                }
                return null;
            }
        }, cacheFolder), new Bundle());
    } catch (IOException e) {
        e.printStackTrace();
        Log.e(TAG, e != null ? e.getMessage() : "PrintPdfTask unknown error");
    }
    return null;
}

private void onLayoutSuccess() throws IOException {
    PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            if (method.getName().equals("onWriteFinished")) {
                pdfCallback.onPdfCreated();
            } else {
                Log.e(TAG, "Layout failed");
                pdfCallback.onPdfFailed();
            }
            return null;
        }
    }, cacheFolder);
    printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);
}


/**
 * Implementation of non public abstract class LayoutResultCallback obtained via DexMaker
 * @param invocationHandler
 * @param dexCacheDir
 * @return LayoutResultCallback
 * @throws IOException
 */
public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler,
                                                                                File dexCacheDir) throws IOException {
    return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)
            .dexCache(dexCacheDir)
            .handler(invocationHandler)
            .build();
}

/**
 * Implementation of non public abstract class WriteResultCallback obtained via DexMaker
 * @param invocationHandler
 * @param dexCacheDir
 * @return LayoutResultCallback
 * @throws IOException
 */
public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler,
                                                                              File dexCacheDir) throws IOException {
    return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)
            .dexCache(dexCacheDir)
            .handler(invocationHandler)
            .build();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM