打印使用iTextG via创建的PDF。打印机连接在以太网上?

时间:2017-11-22 06:58:50

标签: android pdf printing

我使用iTextG库创建了一个pdf。现在我想通过以太网打印它。我该怎么做到这一点?使用以下行,我可以调用默认打印服务,但它不会显示我的打印机连接到以太网。在我的PC上安装驱动程序后,我可以使用Google Cloud Print服务打印pdf。任何人都可以帮我吗?

目前我正在使用以下代码:

public class MyPrintHelper {

    private Context context;

    public MyPrintHelper(Context context) {
        this.context = context;
    }

    public void print() {
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
        // Set job name, which will be displayed in the print queue
        String jobName = context.getString(R.string.app_name) + " Document";
        // Start a print job, passing in a PrintDocumentAdapter implementation
        // to handle the generation of a print document
        printManager.print(jobName, new MyPrintDocumentAdapter(), null);
    }

    private class MyPrintDocumentAdapter extends PrintDocumentAdapter {

        @Override
        public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
            InputStream input = null;
            OutputStream output = null;

            try {

                input = new FileInputStream(new File(context.getExternalCacheDir(), PDF_FILE_NAME));
                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 (IOException e) {
                e.printStackTrace();
            } 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 file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();

            callback.onLayoutFinished(pdi, true);
        }

    }
}

要使用上面的帮助程序类,我将其称为:

MyPrintHelper helper = new MyPrintHelper(getActivity());
helper.print();

我正在使用以下打印机:

https://www.amazon.in/Heyday-Thermal-Receipt-Printer-GP-U80300I/dp/B011BWM95E

1 个答案:

答案 0 :(得分:0)

您的设备应与打印机位于同一网络中。此外,您需要确保您使用的打印机为Mopria-certified:"集成打印支持 与所有经Mopria认证的打印机兼容,占全球销售量的97%。"