【问题标题】:How to print PDF, image and HTML documents from android device to a printer on wifi?如何将 PDF、图像和 HTML 文档从 android 设备打印到 wifi 上的打印机?
【发布时间】:2017-03-11 01:24:34
【问题描述】:

我需要在我的 android 应用中实现文档打印功能。 我能够使用下面提到的 android 打印框架代码打印图像文件:

private void doPhotoPrint(Bitmap bigbitmap) 
{
    PrintHelper photoPrinter = new PrintHelper(MainActivity.this);
    photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
    photoPrinter.printBitmap("droids.jpg - test print", bigbitmap);
}

我浏览了讨论相同问题的现有线程,但没有一个有太大帮助。

我面临的问题是其他文档类型,如 PDF 和 HLML。 如果有人能就此提供一些见解,那将很有帮助。

【问题讨论】:

    标签: android html pdf printing


    【解决方案1】:

    您可以检查此代码

    if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
            String jobName = this.getString(R.string.app_name) + " Document";
    
    
    
            PrintDocumentAdapter pda = new PrintDocumentAdapter() {
    
                @Override
                public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
                    InputStream input = null;
                    OutputStream output = null;
    
                    try {
                        AssetManager assetManager = getAssets();
                        File file = new File(getFilesDir(), "fact_sheet.pdf");                 
                        input = assetManager.open("fact_sheet.pdf");                        
                        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 (FileNotFoundException ee) {
                        //Catch exception
                    } catch (Exception e) {
                        //Catch exception
                    } 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;
                    }
    
                    // int pages = computePageCount(newAttributes);
    
                    PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
    
                    callback.onLayoutFinished(pdi, true);
                }
            };
          printManager.print(jobName, pda, null);  
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-03
      • 1970-01-01
      • 2013-01-20
      • 2019-08-15
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多