Printing WebView contents on a Bluetooth printer - android

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.

Related

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 Printing PDF document

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

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).

Webview to PDF ignores job name

I am trying to print an HTML-page from a webview on KitKat (4.4.4) basically using the sample code provided in Google's API documentation. Accordingly, I set a print job name as follows:
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
My code runs fine and the page is printed as intended. Even if the user chooses the option "save as PDF" in the Android standard printing dialog, the codes renders a nice PDF and the user can choose a file name. At this point I would expect Android to use the string stored in the jobName field as file name. Instead it always uses webview as file name, although my code does not contain webview as string.
Is there a way to set another default name for storing a PDF file that is generated in my application?
Thanks for your hints...
Update:
I spent some additional time investigating this issue and I found that the WebView uses AwPrintDocumentAdapter as print adapter if the adapter is created by calling createPrintDocumentAdapter() as proposed in the API documentation. That class then calls
PrintDocumentInfo.Builder("webview") which seems to be the reason why the PDF always is named "webview". Some research reveals the following code snippet in AwPrintDocumentAdapter:
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle metadata) {
mAttributes = newAttributes;
// TODO(sgurun) pass a meaningful string once b/10705082 is resolved
PrintDocumentInfo documentInfo = new PrintDocumentInfo
.Builder("webview")
.build();
// TODO(sgurun) once componentization is done, do layout changes and
// generate PDF here, set the page range information to documentinfo
// and call onLayoutFinished with true/false depending on whether
// layout actually changed.
callback.onLayoutFinished(documentInfo, true);
}
Hence, this seems to be the root cause for my problem - at least if that code made its way to my Nexus 4 test device... Finally, the best way to deal with this naming issue seems to be a custom print adapter implementation.
Are there any other solutions that do not require a custom print adapter (which should contain code for calculating the number of pages etc...)?

Categories

Resources