【问题标题】:Print PDF created using iTextG via. printer connected on ethernet?打印通过 iTextG 创建的 PDF。打印机连接在以太网上?
【发布时间】:2017-11-22 06:58:50
【问题描述】:

我使用 iTextG 库创建了一个 pdf。现在我想通过以太网打印它。我应该如何实现这一目标?使用以下行,我可以调用默认打印服务,但它不显示我的打印机连接到以太网。在我的电脑上安装驱动程序后,我可以使用谷歌云打印服务打印 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

【问题讨论】:

  • 您的问题与 iText 无关。标记已删除。

标签: android pdf printing


【解决方案1】:

您的设备应与打印机位于同一网络上。另外,您需要确保您使用的打印机是Mopria-certified:“集成打印支持 兼容所有经 Mopria 认证的打印机,这些打印机占全球销售的打印机的 97%。”

【讨论】:

    猜你喜欢
    • 2012-04-27
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2014-08-25
    • 2011-09-12
    • 1970-01-01
    • 2014-07-22
    相关资源
    最近更新 更多