简体   繁体   English

如何从Android设备打印PDF,图像和HTML文件到wifi上的打印机?

[英]How to print PDF, image and HTML documents from android device to a printer on wifi?

I need to implement document printing functionality in my android app. 我需要在我的Android应用程序中实现文档打印功能。 I was able to print image file using the android printing framework code mentioned below : 我能够使用下面提到的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);
}

I went through the existing threads discussing the same question, but none of them help much. 我经历了讨论同一个问题的现有主题,但没有一个帮助太多。

The issue I am facing is with other document types like PDF and HLML. 我面临的问题是其他文档类型,如PDF和HLML。 It would be helpful if someone could give some insight on the same. 如果有人可以提供相同的见解会很有帮助。

you can check this code 你可以查看这段代码

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);  
    }

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

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