Android Question Printing Framework

Discussion in 'Android Questions' started by Rainer@B4A, Apr 12, 2019.

  1. Rainer@B4A

    Rainer@B4A Member Licensed 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 :

    Code:
    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.
     
  2. Rainer@B4A

    Rainer@B4A Member Licensed User

    I found the missing code row :

    Code:
    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!
     
  3. DonManfred

    DonManfred Expert Licensed User

    what is the outputfilename on the code in the link above?

    "Document"?
     
  4. Rainer@B4A

    Rainer@B4A Member Licensed 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.
     
  5. DonManfred

    DonManfred Expert Licensed 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

    Code:
    // 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.");
        }
     
  6. DonManfred

    DonManfred Expert Licensed User

    Try this modified sub from the link you posted.

    Code:
    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: Apr 13, 2019
  7. Rainer@B4A

    Rainer@B4A Member Licensed User

    Thank you,
    now it's possible to set the file name, but it goes always into the Download folder.
     
  8. DonManfred

    DonManfred Expert Licensed User

    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.
    Code:
    dim fullpath as string = File.Combine(path,filename)
     
  9. Rainer@B4A

    Rainer@B4A Member Licensed User

    No, then path and filename will be combined as a long filename.
     
  10. DonManfred

    DonManfred Expert Licensed User

    Does it work if you just leave this long filename? Ist the file then saved to this path?
     
  11. Rainer@B4A

    Rainer@B4A Member Licensed 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.
     
  12. DonManfred

    DonManfred Expert Licensed User

    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.

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice