B4A Library Printing and Pdf creation

Status
Not open for further replies.
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.

B4X:
Dim pdf As PdfDocument
pdf.Initialize
pdf.StartPage(595, 842) 'A4 size
pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
pdf.Canvas.DrawText("Hello", 100, 100, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Yellow, "CENTER")
pdf.FinishPage
Dim out As OutputStream = File.OpenOutput(File.DirInternal, "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:
B4X:
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 String) As 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.11 - PrintPdf2 - Allows passing custom PrintAttributes, created with JavaObject.
V1.10 - Adds support for printing PDF documents:
B4X:
Printer.PrintPdf("Job Name", File.DirAssets, "My Document.pdf")
 

Attachments

  • Printing.zip
    7.1 KB · Views: 3,540
Last edited:

JNG

Member
Licensed User
Hi!

I am try to use this library but unable to point out the problem
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim pdf As PdfDocument
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim Htmlfile As String = GenerateHtml
    pdf.Initialize
    pdf.StartPage(595, 842) 'A4 size
    '    pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
    pdf.Canvas.DrawText(Htmlfile, 100, 100, Typeface.DEFAULT_BOLD, 30 / GetDeviceLayoutValues.Scale , Colors.Black, "CENTER")
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(File.DirRootExternal, "Test1.pdf", False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
End Sub

Sub Activity_Resume
   
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub GenerateHtml
    Dim stylelayout As String  =  $ "table{width: 100%; border-color: #000000;     border = 1;}" _
& " caption { display: table-caption; height:50px; }" _
& " th {border-width: 1px;            border-color:      #000000;font-size:16px; height:40px;} " _
& " td {border-width: 1px;  border-color:      #000000;font-size:16px; height:40px; }" _
& " a { text-decoration:none; color: #000;}"$
Dim caption As String = "Testing PDF"
    Dim Hstr As String = $"
<tr>
<th></th>
<th>9am</th>
<th>10am</th>
<th>11am</th>
<th>12am</th>
</tr>
<tr>
<th>Monday</th>
<td colspan="2">Geography</td>
<td>Math</td>
<td>Art</td>
</tr>
<tr>
<td>Name</td>
<th colspan="2">Gym</th>
<th>Home Ec</th>
</tr>
<tr>
<th>Tuesday</th>
<td colspan="2">Value</td>
<td>Address Ec</td>
</tr>"$


    Dim HtmlStr As String =  $"<!DOCTYPE html>
    <html><meta charset="utf-8">
    <body>
    <style type='text/css'>${stylelayout}</style> _
           
            <caption>${caption}</caption>
            <table>
            ${Hstr}
       
            </table>
            <img src="smiley.gif" alt="Smiley face" height="42" width="42">
    </body>
    </html>"$
    Log(HtmlStr)
    Return(HtmlStr)
   
   
End Sub
. Code attached.

Pl let me know where I am going wrong

regards
jng
 

Mikel Huerta

Member
Licensed User
Excuse me @Erel, wish version of PDF library are you using ? i am using PDF library version 0.25 .. and , if not are the same , where can i found the newst one ?

Thanks you.
 

BarryW

Active Member
Licensed User
Longtime User
how to call this library?

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.

B4X:
Dim pdf As PdfDocument
pdf.Initialize
pdf.StartPage(595, 842) 'A4 size
pdf.Canvas.DrawLine(2, 2, 593 , 840, Colors.Blue, 4)
pdf.Canvas.DrawText("Hello", 100, 100, Typeface.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

View attachment 53290

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:
B4X:
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 String) As 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:
B4X:
Printer.PrintPdf("Job Name", File.DirAssets, "My Document.pdf")
 

JOTHA

Well-Known Member
Licensed User
Longtime User
Hi Erel,

great Lib! Thank you for your excellent work again!

Whats the advantage over PDFWriter?
 

SAMER ISSA

Member
Licensed User
Longtime User
Thank you for replay KMatle ...:)
but have you an example please !:):):)
I was work with DonManfred library (pdf.jar) it have wordwrap but without (Aligns type) like "CENTER" "RIGHT" "LEFT" :(
and (printing.jar) library have Aligns type but without wrap text :(
 
Status
Not open for further replies.
Top