B4A Library Printing and Pdf creation

Discussion in 'Additional libraries, classes and official updates' started by Erel, Feb 28, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This is an Android 4.4+ (API 19+) library.

    Its two main features are:
    1. Creating Pdf documents with the PdfDocument object.
    2. Printing with the Printer object.

    Lets start with PdfDocument.

    Code:
    Dim pdf As PdfDocument
    pdf.Initialize
    pdf.StartPage(
    595842'A4 size
    pdf.Canvas.DrawLine(22593 , 840Colors.Blue, 4)
    pdf.Canvas.DrawText(
    "Hello"100100Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "1.pdf"False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
    1. Initialize.
    2. Call StartPage to add a new page. The page size is measured in 1 / 72th inch.
    3. Use the Canvas object to draw on the page. Note that you shouldn't use 'dip' units here.
    You should divide the text size with GetDeviceLayoutValues.Scale when calling Canvas.DrawText.
    4. Call FinishPage.
    5. Repeat the above 3 steps for each page.
    6. Save the document to a file.
    7. Close the pdf object.

    Printing

    upload_2017-2-28_15-3-28.png

    The printing feature is based on the OS printing framework. Most popular printers are supported. You do need to first install a printer plug-in.

    For example to print to a HP printer: https://play.google.com/store/apps/details?id=com.hp.android.printservice

    Cannon: https://play.google.com/store/apps/details?id=jp.co.canon.android.printservice.plugin

    The Printer object can print bitmaps, html documents, WebView content and PDF documents.

    Example:
    Code:
    Sub Globals
       
    Private printer As Printer
    End Sub

    Sub Activity_Create(FirstTime As Boolean)
       printer.Initialize(
    "")
       Print
    End Sub

    Sub Print
       printer.PrintHtml(
    "job"$"<b>Hello world!!!</b><br/>
    <h1>second line</h1>
    <img src="$
    {WebViewAssetFile("smiley.png")}"/>"$)
    End Sub

    Sub WebViewAssetFile (FileName As StringAs String
       
    Dim jo As JavaObject
       jo.InitializeStatic(
    "anywheresoftware.b4a.objects.streams.File")
       
    If jo.GetField("virtualAssetsFolder") = Null Then
         
    Return "file:///android_asset/" & FileName.ToLowerCase
       
    Else
         
    Return "file://" & File.Combine(jo.GetField("virtualAssetsFolder"), _
      jo.RunMethod(
    "getUnpackedVirtualAssetFile"Array As Object(FileName)))
       
    End If
    End Sub
    The WebViewAssetFile is a utility sub that creates the url of an asset file (the url depends on the compilation mode).

    Updates:
    V1.10 - Adds support for printing PDF documents:
    Code:
    Printer.PrintPdf("Job Name"File.DirAssets, "My Document.pdf")
     

    Attached Files:

    Last edited: May 11, 2017
    gvoulg, scsjc, jimmyF and 28 others like this.
  2. HAH

    HAH Active Member Licensed User

    Excellent, that what we was waiting for a looooong time ...
     
  3. LucianDesign

    LucianDesign Member Licensed User

    Excelent! It would be great if the print object could also print PDF files.
     
  4. Tayfur

    Tayfur Active Member Licensed User

  5. Erel

    Erel Administrator Staff Member Licensed User

    It is based on the same native API (PdfDocument).

    The user chooses these settings from the printing dialog.
     
    Tayfur likes this.
  6. Tayfur

    Tayfur Active Member Licensed User

    nice :)
    It would be easy for me to use the library.

    can i use with out printing dialog?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    No.
     
  8. lucdrb

    lucdrb Member Licensed User

    Hi,

    Should I've missing something, I get this error message after Pdf.Initialize:

    java.lang.NoClassDefFoundError: android.graphics.pdf.PdfDocument

    Luc
     
  9. LucianDesign

    LucianDesign Member Licensed User

    This is an Android 4.4+ (API 19+) library. You must have in your manifest file minSdkVersion = 19.
    Code:
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23"/>
     
    Erel likes this.
  10. lucdrb

    lucdrb Member Licensed User

    Thanks

    I use an old phone: Android 4.2.2
    I'm going to buy a new one ;)

    Luc
     
  11. menneaduplo

    menneaduplo Member Licensed User

    Hi,
    i installed B4a 6.80 and i tried to add the library printing.jar at my project.
    When compiling appears: "Maven artifact not found: com.android.support/support-v4".
    The API seems ok.
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23"/>

    Thanks
     
  12. DonManfred

    DonManfred Expert Licensed User

    See Step 2 here: https://www.b4x.com/b4a.html

    You need to install the two maven repositories.
     
    Devan likes this.
  13. menneaduplo

    menneaduplo Member Licensed User

    installing SDK updated and "Android support Repository" the issue is solved.
    Thank you very much
     
  14. bocker77

    bocker77 Member Licensed User

    With the printing code I can only print a file from the assets folder. Can I also print from let's say DirDefaultExternal folder and if so how?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

    Are you using PrintHtml? You can reference files from any folder.

    Code:
    Sub Print
       printer.PrintHtml(
    "job"$"<b>Hello world!!!</b><br/>
    <h1>second line</h1>
    <img src="file://${
    File.Combine(File.DirRootExternal, "smiley.png")}"/>"$)
    End Sub
     
  16. luciano deri

    luciano deri Active Member Licensed User

    Hy, i'm creating a PDF document i A4, but the is non the same result with two different device. I think that is a scaled problem. But a pdf document must not scaled ever. I not user dip unit musure but only integer value.
     
  17. Tayfur

    Tayfur Active Member Licensed User

    I had same problem; and screen resolations are diffrent for each device.

    @Erel 's advice is "GetDeviceLayoutValues.Scale"

    pdf.Canvas.DrawText("Hello", 100, 100, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")


    My question is same , See link :
    https://www.b4x.com/android/forum/threads/problem-with-pdf-in-printing-library.77654/#post-493717

    thanks
     
    Last edited: May 5, 2017
  18. luciano deri

    luciano deri Active Member Licensed User

    Yes, with this i had solve 90% of problem. Change a bit the dimension of rect elements.
     
  19. IDDGroup

    IDDGroup Member Licensed User

    its there a way to print the PDF?

    webview doesnt support PDF's.
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    V1.10 was released with support for printing pdf documents.
     
    LucianDesign likes this.
Loading...