android


How to print PDF, image and HTML documents from android device to a printer on wifi?


I need to implement document printing functionality in my android app.
I was able to print image file using the android printing framework code mentioned below :
private void doPhotoPrint(Bitmap bigbitmap)
{
PrintHelper photoPrinter = new PrintHelper(MainActivity.this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("droids.jpg - test print", bigbitmap);
}
I went through the existing threads discussing the same question, but none of them help much.
The issue I am facing is with other document types like PDF and HLML.
It would be helpful if someone could give some insight on the same.
you can check this code
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
PrintDocumentAdapter pda = new PrintDocumentAdapter() {
#Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
InputStream input = null;
OutputStream output = null;
try {
AssetManager assetManager = getAssets();
File file = new File(getFilesDir(), "fact_sheet.pdf");
input = assetManager.open("fact_sheet.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();
}
}
}
#Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
// int pages = computePageCount(newAttributes);
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
printManager.print(jobName, pda, null);
}

Related Links

writeToParcel() not called in orientation changes in Android
android custom List Adapter not displaying items
android - universal image loader how to apply displayer when image failed to load?
cordova - android app crashes when receive one push notification
sdk platform tools component is missing when i copied the android sdk path
Libgdx: ScreenUtils.getFrameBufferPixmap returning blank black Pixmap on Android
Custom ListView to handle EditText and TextView passed from another Intent
GoogleCloudMessaging.getInstance(Unknown Source) NullPointer
How to change Toolbar home icon color
Get a Signed Request from Facebook Android SDK
How to access a file from network in Android?
the number of cpus of the virtual device has been forced to be 1
Test the web site on mobile using appium
How to pass value to flag enum parameter in Soap service (ASMX service) From android app
Android - Using a custom image for GoogleMap overlay
How to switch between Activities using the same Fragment?

Categories

HOME
grok
twitter-bootstrap-3
cobalt
ecmascript-6
jax-rs
angular-formly
angular2-material
pdfbox
opendj
entity-framework-core
spring-session
jax-ws
mvc5
frontend
barcode
teechart
ipmitool
angular-dart
scrolltop
backpack-for-laravel
ember-cli
ios10.2
exploit
data.stackexchange.com
opentk
custom-post-type
fable-f#
entity-relationship-model
inversion-of-control
spotipy
bringtofront
aws-codecommit
mobx
dsx-desktop
installshield-le
openwhisk
ideascript
password-protection
comparison-operators
linqpad
chronicle-map
contract
stylecop
jquery-ui-draggable
hammer.js
context-switch
appdomain
modelsim
git-flow
seq
django-1.10
testrail
cudd
pure-css
microstation
vorpal.js
jclouds
appscale
libav
rasterize
lazybones
toolkit
sony-lifelog-api
viola-jones
graphics2d
osc
bcache
exceldatareader
sql-scripts
formview
human-computer-interface
universal-image-loader
mdichild
live555
mysql-error-1044
cmmi
kaleidoscope
abstract-data-type
extreme-programming
jquery-blockui
boost-msm
veracity
dajaxice
subproject
failing-tests
ikimagebrowserview
user-tracking
multiplatform
squishit
web-application-project
tui
sifr3
software-lifecycle
appliance

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App