Android Printing PDF document - android

I am trying to print pdf document that i just have created in my application.
I used
How to Print PDF using Android 4.4 Printing framework
above link
but there is error in :
PrintManager printManager = (PrintManager)this.getSystemService(mContext.PRINT_SERVICE);
this line
says this error
Please see this image for error
And after this i tried to extend my activity with service then it says that , ondestroy clashes with service ondestroy.
so after that i created a service which extends PrintService there also same error occurs as in image.
Added service in manifest also and added the permission too. Please help i need to print the pdf file.

Try this :
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);

Related

Why is there an error loading a PDF using Intent on Android 7

I have a TextView with an onClick event handler to download an open a PDF file
I have the following code in the response handler.
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(pdfUrl)))
I can see the PDF loading but before it can be read it returns to the app with the error below.
Cannot open PDF
<filename>.pdf cannot be opened
Device properties
ril.model_id: QB13532959
ro.board.platform: exynos5
ro.boot.em.model: SM-J327T1
ro.build.version.sdk: 24
ro.product.brand: samsung
ro.product.manufacturer: samsung
ro.product.model: SM-J327T1
ro.product.name: j3popeltemtr
Thanks to user #CommonsWare I double-checked assumptions and realized there was an error with the PDF URL. The problem has been resolved.

Printing WebView contents on a Bluetooth printer

I'm using following code from developer site to print web-view to Bluetooth printer from android app.
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) +
" Print Test";
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
But It loads webview and ask to save as PDF, instead of showing Bluetooth printers available. Even if we search for printers it is searching for long time with no result.
So please suggest me someway to the dynamic HTML content or webview to Bluetooth printer.
The above code works if you have a supported plugin service available in the playstore. Unfortunately the printer(Intermec 6820 series) doesn't have any. So I ended up formatting the text using android String.format and printing it out.

Save pdf without showing the user the print dialogue screen

I'm using Xamarin.android and I followed this
I Want to save a print to a specific path without showing dialogue screen to user.
How can I say the path I want to save without asking user ?
I have that code:
myWebView = new Android.Webkit.WebView(this.Context);
var printManager = (Android.Print.PrintManager)Forms.Context.GetSystemService(Android.Content.Context.PrintService);
var text = new ContractHTML() { Model = new Model.Model() { img = null } };
myWebView.LoadDataWithBaseURL("file:///android_asset/", text.GenerateString(), "text/html", "UTF-8", null);
string fileName = "MyPrint_" + Guid.NewGuid().ToString() + ".pdf";
var printAdapter = myWebView.CreatePrintDocumentAdapter(fileName);
Android.Print.PrintJob printJob = printManager.Print("MyPrintJob", printAdapter, null);
This code is working and generate a pdf, but it's asking user where to save it, I want to save it in some path.
Is it possible ?
Save pdf without showing the user the print dialogue screen
AFAIK you can not implement this feature in Android.
The user needs to be able to choose some configuration, such as choosing what printer to print on. So when you use printManager.Print() method a print dialog will show up. You could find that In android PrintManager Source code, PrintManager class is final(In C# it's sealed), we are not allowed to override this method to prevent the dialog.
When you execute printManager.Print("MyPrintJob", printAdapter, null) method from an Activity, it starts PrintJob also it will bringing up the system print UI. This is did by Android printing framework, we cannot silently print by using the platform API.
There is an open issue on AOSP issue tracker : https://code.google.com/p/android/issues/detail?id=160908.
After searching far and wide, I used this GitHub project to solve this problem. I was able to silently print the document without the user's knowledge.
It seems like the developer was able to add some classes to the android.print package to access private members, making this possible.
https://github.com/UttamPanchasara/PDF-Generator?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=7355

how android use BlueTooth printer print the WebView content

how to print the HTML using a bluetooth printer with android?Can I print the String="< html > < body >You scored 192 points." into html page?
There's a quick how-to on the Android Developer site about printing the contents of a WebView:
from https://developer.android.com/training/printing/html-docs.html
// Get a PrintManager instance
PrintManager printManager = (PrintManager) getActivity()
.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());
You don't even have to display the WebView first - in fact they recommend that you use a dedicated invisible WebView for printing.
In order to print to a Bluetooth printer though, you'll need to have the print service app for that printer installed, or use a manufacturer-specific SDK - either of which might not exist :(

Android HTML code printing before KitKat (before api 18)

I'm looking for an easy way of making this printing HTML code fragment compatible backwards with older Android versions:
#TargetApi(Build.VERSION_CODES.KITKAT)
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());
// Save the job object for later status checking
mPrintJob = printJob;
}
This works from Android 19 (Kit-Kat), but i need some previous versions to work also.
I don't need to download and print a webpage from the internet, i already have the HTML code as a String, which i show in a webview (as yo can see in the code).
Is there an easy way of printing HTML code before Android 19?
Thanks in advance!
Prior to Kitkat there is no Android API to support printing. In the 2016 Google I/O printing training, the developers mentioned that prior to Kitkat your only way to print is to "throw it over the wall". That is, send an intent and hope that the user has the suitable application to print. Look at this question for examples on apps that provide printing (HP ePrint and Google Cloud Print).

Categories

Resources