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