android 4.4打印功能

Android 4.4goole提供了打印的统一接口,通过使用Android 的api,加上各个厂商提供的打印插件,实现起来相对比较容易些,因为之前在网上下载了一些打印app,连上打印机后打印不出来,有的就是按照提示下载驱动,同样无法打印,所以,最后选择的是Android api+厂家的的打印插件,

目前Android提供的比较容易的是打印图片和webview,代码相对比较简单

private void printPicture(Bitmap bitmap) {
        PrintHelper photoPrinter = new PrintHelper(this);
        photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);

        photoPrinter.printBitmap("droids.jpg - test print", bitmap);
    }


打印webview设置自定义字体,首先将字体库放到assert子目录下:

InputStream in = new FileInputStream(new File(path));
    //    InputStream    in = getAssets().open("beihairen.doc");
            int len = in.available();
            byte[] buff = new byte[len];
            in.read(buff);
            String html = new String(buff, "UTF-8");

            html = html.replace("@fontPath0", "../font/fs_GBK.TTF");//设置webview中的字体为自定义的字体
            html = html.replace("@fontPath1", "../font/fzgfs.TTF");
            String baseurl = "file:///android_asset/html/";
            mwebView.loadDataWithBaseURL(baseurl, html, "text/html", "UTF-8",
                    null);
            mwebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    createWebPrintJob(mwebView);
                }
            });

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    private void createWebPrintJob(WebView webView) {
    
        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
        
        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView
                .createPrintDocumentAdapter();
        // Create a print job with name and adapter instance
        String jobName = getString(R.string.app_name) + " Document";
        PrintJob printJob = printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());
    }

如果需要自定义字体html页面设置的style:

<style>
    .text0 { font-family: simsun; line-height:30.0pt;font-size:16.0pt;line-height:26.0pt}
    @font-face {
       font-family: simsun;
       src:url(@fontPath0);
    }
    .text1 { font-family: fangsong; line-height:30.0pt;font-size:16.0pt;}
    @font-face {
       font-family: fangsong;
       src:url(../font/fangsong.ttf);
    }
  .text3 {
    list-style: outside none none; border-bottom: 1px solid rgb(15, 15, 15);
    font-family: fangsong; line-height:30.0pt;font-size:14.0pt;
  }
  .text4 {
    font-family: fangsong; line-height:30.0pt;font-size:14.0pt;
  }  
    .text2 { font-family: fangzheng; line-height:30.0pt;text-align:center;font-size:22.0pt;}
    @font-face {
       font-family: fangzheng;
       src:url(../font/fzgfs.TTF);
    }
  li{
        list-style: outside none none; border-bottom: 1px solid rgb(15, 15, 15);font-size:14.0pt;
    font-family: fangsong; line-height:30.0pt;font-size:16.0pt;}
    }
</style>

打印pdf文件

打印pdf文档自定义adapter

PrintDocumentAdapter pda = new PrintDocumentAdapter() {

        @Override
        public void onLayout(PrintAttributes oldAttributes,
                PrintAttributes newAttributes,
                CancellationSignal cancellationSignal,
                LayoutResultCallback callback, Bundle extras) {
            if (cancellationSignal.isCanceled()) {
                callback.onLayoutCancelled();
                return;
            }
            PrintDocumentInfo info = new PrintDocumentInfo.Builder(
                    "printer_qr_code.pdf")
                    .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                    .setPageCount(1).build();//setPageCount(1)不设置打印所有页
            callback.onLayoutFinished(info, true);

        }

        @Override
        public void onWrite(PageRange[] pages,
                ParcelFileDescriptor destination,
                CancellationSignal cancellationSignal,
                WriteResultCallback callback) {
            // TODO Auto-generated method stub
            InputStream input = null;
            OutputStream output = null;

            try {

                input = getAssets().open("beihairen.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();
                }
            }
        }

    };

参考:http://blog.csdn.net/sahadev_/article/details/51313384

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值