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

How to check the broadcast receiver is running or not in android?
Android Add image to text (in text View)?
linebreak received from server
Android's Media Scanner: How do I remove files?
Android db loading chat for chat application
Android output error. TextView won't display output
Android fill array with ImageViews
Android collapse an element?
How to use JSON in PhoneGap [closed]
Android SDK r 15 error: Cannot build project using Ant
Nullpointerexception when findingViewById
Move Android source into case-sensitive image
Android: Accessing images from assets/drawable folders
Pinch Zoom on Two Views in Single Viewgroup android
Eclipse can see resources which do not exist
List Items not displaying in this Android ListView

Categories

HOME
scikit-learn
jpa
linkedin
c99
nunit
rsa
fftw
jwplayer
grafana
newrelic
xbee
telegram-bot
google-admin-sdk
trac
odoo-8
vimdiff
startup
stellar.js
template-engine
google-data-studio
cfml
oledb
charles
asp.net-core-webapi
dpdk
react-toolbox
slidetoggle
progress-bar
bioinformatics
autolisp
login-script
twos-complement
mnist
xajax
reactivekit
railstutorial.org
chef-solo
plaintext
phpdbg
jide
zenhub
magic-draw
custom-font
oracleclient
filehandle
rackspace-cloud
offline.js
gameplay-kit
android-instrumentation
materialized-path-pattern
simplepie
parfor
myspace
installshield-2010
vorpal.js
nservicebus5
nss
database-relations
libvlc
toran-proxy
tfs-workitem
avisynth
pypiserver
roadkill-wiki
zen-cart
ember-cli-rails
jsvc
nutiteq
kubuntu
code-documentation
procedural-programming
mass-assignment
boost-program-options
jbake
primitive-types
grails-plugin-rabbitmq
wxformbuilder
terminate
extjs2
amslidemenu
git-repo
gwt-openlayers
c++builder-xe5
node-blade
jquery-1.9
android-icons
onload-event
superscrollorama
cascadingdropdown
idn
extreme-programming
failing-tests
filemerge
u2netdk
webdev.webserver
uideviceorientation
django-piston
complex-event-processing

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