0

I am not sure this is a duplicate because all what I found wasn't exactly what I am looking for.

I have an App that has an Activity where the user fills a form (Table layout with textviews and edittexts)

At the bottom there is a Print button which (As of right now) it generates a pdf file using itextpdf and saves it in "/storage/emulated/0/client pqr/pqr.pdf"

What I want to achieve is on that button click, the PDF is generated then sent to a network printer and print without a print preview.

Is this possible? At least possible with print preview?

I am new to this, and not familiar on how to implement the printing custom documents (https://developer.android.com/training/printing/custom-docs.html#print-adapter). I am not sure how to implement the print adapter/manager. I saw some pages with code snippet solution but I was not sure which class they go in.

I would really appreciate any help here as I have spent days googling and did not get anywhere.

Sam
  • 73
  • 1
  • 9
  • 1
    "Is this possible? At least possible with print preview?" -- last I checked, Android does not have print preview as a standard capability. It is up to individual apps to offer that. Beyond that, see [this answer](https://stackoverflow.com/a/33089957/115145). – CommonsWare Jan 19 '18 at 23:56
  • Hello @CommonsWare. thanks for the reply. I am trying to implement this project in mine. I created a MyPrintDocumentAdapter, however I am not sure where to do the printing call. From what I gather, it should be done in the onWrite method, but all examples show printing into a file. How and where can I connect to an IP printer and print to it? – Sam Jan 22 '18 at 16:15

1 Answers1

0

I created a MyPrintDocumentAdapter, however I am not sure where to do the printing call. From what I gather, it should be done in the onWrite method, but all examples show printing into a file. How and where can I connect to an IP printer and print to it?

A PrintDocumentAdapter and associated PrintJobs are for printing to the user's choice of printer from the available printers on their device.

The simplest solution, therefore, is for you to install the Android "print driver" app from the printer manufacturer. Then, you can use the Android printing framework (e.g., PrintDocumentAdapter) to that printer. Whether there will be print preview or not is up to Android and, possibly, the printer manufacturer.

If the printer manufacturer does not have an Android app that makes their printers available for printing, you would need to either:

  • Create a PrintService yourself that can work with those printers (and that is where you would connect to the IP address and do... something...), so you can use the Android printing framework with those printers, or

  • Bypass Android's printing framework entirely, and just write your own code to send the print job to the printer by one means or another.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am trying to print with an HP LaserJet. I just got a hold of their SDK. I compile the package and import the classes, however some of the objects are causing errors in Android Studio. Created a PDFAsset, a PrintItem, and a PrintJobData. However I cant setJobName as I get the famous "can not resolve symbol setJobName" error. HP support seems to be useless as I am yet to get a reply. – Sam Jan 23 '18 at 20:50