How to Print PDF using Android 4.4 Printing framework
24,219
After spend some hours on google i found the solution.
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
PrintDocumentAdapter pda = new PrintDocumentAdapter(){
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(file to print);
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;
}
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
![Dinesh T A](https://i.stack.imgur.com/r2fHi.jpg?s=256&g=1)
Comments
-
Dinesh T A almost 2 years
How to print already downloaded PDF using Android 4.4 printing framework?
I viewed the developer documentation. but no luck. Any example would be helpful
-
abhishek over 9 yearsWhat kind of printers are supported ??
-
Dinesh T A over 9 years
-
Access Denied over 9 yearsThanks man, you saved me tons of time :)
-
dd619 about 9 yearsvery useful..just remove int pages = computePageCount(newAttributes); from code.
-
Nandakishore Shetty over 8 years@Dinesh Anandan, do you any idea about USB printer using android.stackoverflow.com/questions/30498746/…
-
Mohsin almost 8 yearsThanks, perfect solution
-
Smit Davda about 7 yearsThanks for the perfect answer, do you have any idea of how to print an image from an image url or a local device image?
-
Amit about 7 years@Dinesh Do you have any Idea how to print any office document like xls, doc, docx through this?
-
Ravish Sharma almost 7 yearshi guys what is pda in printmanager.print(jobname,pda,null) please help me
-
Dinesh T A almost 7 years@RavishSharma It's PrintDocumentAdapter use the code like printmanager.print(jobname, new PrintDocumentAdapter(){ } ,null)
-
Pink Jazz about 6 yearsThis does not respect the orientation of PDFs, with the preview of landscape PDFs being rotated sideways.
-
Fisher over 5 yearsThis works fine for a .pdf file, but for a .txt file it doesn't show the layout and no way to print. Do you know how to modify to print a .txt file? Thanks!
-
Girish over 4 yearsStill, File saving as 0 bytes.
-
SinaMN75 over 4 yearsit will open android's dialog for choosing printers and other settings, is there anyway to prevent that, make it print directly without any popups?
-
Saad Bilal over 3 yearsAwesome man !!!!
-
Venkat almost 3 yearsHi...in this code -
input = new FileInputStream(file to print);
- what I need to print in the place of file to print... Can anyone plz help me..