Android Question Printing Framework

Rainer@B4A

Member
Licensed User
Longtime User
I use the printing library to convert html to pdf files. It works fine, but the user always has to change the page orientation to landscape and to define a useful filename. I'm searching for a way to preset these defaults by my program.

I found this old thread :
https://www.b4x.com/android/forum/threads/android-printing-framework.38796/page-2
where Erel shows how to change the printer.attributes :

B4X:
Sub CreatePrinterAttributes As JavaObject
   Dim builder As JavaObject
   builder.InitializeNewInstance("android.print.PrintAttributes.Builder", Null)
   Dim mediaSize As JavaObject
   mediaSize = mediaSize.InitializeStatic("android.print.PrintAttributes.MediaSize").GetField("ISO_A6")
   Return builder.RunMethod("build", Null)
End Sub

But I can't see where the defined mediaSize is used.
I was hoping I could adapt this snippet, but I doesn't work.
 

Rainer@B4A

Member
Licensed User
Longtime User
I found the missing code row :

B4X:
builder.RunMethod("setMediaSize", Array As Object(mediaSize))

To switch the default to landscape I use "UNKNOWN_LANDSCAPE" instead of "ISO_A6".

But to change the output file name is still open!
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Rainer@B4A

Member
Licensed User
Longtime User
No, this is the jobname.
I first filled a WebView with a HTML file and then after calling
Sub CreateWebPrintJob (wv As WebView)
a system print GUI window appears.
Now the user can check or change the print parameters and finaly start the job.

With my change above, landscape is now the default.
But I wish to set the destination name, standard is at the moment "default".

Even better it would be to have a lib which needs no user input.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i´m not sure where this should have to be placed but

https://developer.android.com/training/printing/custom-docs.html
Based on this the printadapter implementation must be included

B4X:
// Create a new PdfDocument with the requested page attributes
    pdfDocument = new PrintedPdfDocument(getActivity(), newAttributes);

    // Respond to cancellation request
    if (cancellationSignal.isCanceled() ) {
        callback.onLayoutCancelled();
        return;
    }

    // Compute the expected number of printed pages
    int pages = computePageCount(newAttributes);

    if (pages > 0) {
        // Return print information to print framework
        PrintDocumentInfo info = new PrintDocumentInfo
                .Builder("print_output.pdf")
                .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                .setPageCount(pages)
                .build();
        // Content layout reflow is complete
        callback.onLayoutFinished(info, true);
    } else {
        // Otherwise report an error to the print framework
        callback.onLayoutFailed("Page count calculation failed.");
    }
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Try this modified sub from the link you posted.

B4X:
Sub CreateWebPrintJob (wv As WebView, output As String)
    DIm jwv As JavaObject = wv
    Dim jPrintManager As JavaObject = jwv.RunMethodJO("getContext", null).RunMethod("getSystemService", Array As Object("print"))
 
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", Array As Object(output)) ' Changes here
    dim jobName as String = "Document"
   jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, CreatePrinterAttributes ))
End Sub

Please note that the changed call only works on Lollipop and above.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but it goes always into the Download folder
You know the name of the file.
You can check the Downloadfolder and copy the file to any other pace if you need to.

You also can try to set a full path instead of just a filename.
B4X:
dim fullpath as string = File.Combine(path,filename)
 
Upvote 0

Rainer@B4A

Member
Licensed User
Longtime User
Then output goes always to the download directory, even if I use your suggestion File.Combine().
Android converts the Dir and Filename in a string with underscores instead of the slashes.
For me it's OK now.

The only open wish is this version without any user interaction.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
but it goes always into the Download folder
i guess you need to live with that. The path is probably fixed to the downloadfolder in the browserengine.
I suggest the best is to move the file away from there and copy it to the folder where you need it.

The only open wish is this version without any user interaction.
You need to write a full wrap for the Printing Framework. The snippets in that thread are only small examples. Not all features are used. Write a complete Library using Java.

Edit to add: Even with a full Library i don´t know if you fully prevent user intervention. I guess google is not allowing this maybe.
 
Upvote 0
Top