Android Question WebView2 and large array data

Hi. Im'using in a c# project and WebView2 the method PostSharedBufferToScript to pass big array CoreWebView2SharedBuffer.

Is there a way to do the same in B4A?

1762157901007.png


and this in JS module client side

1762157994719.png
 

fatima nesrin

New Member
Private Sub ButtonExport_Click
If TableData.Size = 0 Then
xui.MsgboxAsync("No data to export!", "Info")
Return
End If

' Initialize PDFWriter
PDFWriter1.Initialize("PDFWriter1", PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT)
PDFWriter1.setFont(Fonts.HELVETICA, Fonts.HELVETICA)
PDFWriter1.CreateNewDocument(PaperSize.A4_WIDTH, PaperSize.A4_HEIGHT)

' === PDF Title (centered) ===
Dim titleText As String = "Today Sales Report"
PDFWriter1.setFont(Fonts.HELVETICA_BOLD, Fonts.HELVETICA_BOLD)

' Approximate centering
Dim pageWidth As Int = PaperSize.A4_WIDTH
Dim textWidth As Int = titleText.Length * 7 ' 7 is approximate width per character
Dim titleX As Int = (pageWidth - textWidth) / 2

PDFWriter1.addText(titleX, 780, 20, titleText) ' Y position as needed


' Table layout
Dim startX As Int = 50
Dim startY As Int = 750
Dim rowHeight As Int = 25
Dim colWidths() As Int = Array As Int(100, 220, 100, 120)
Dim headers() As String = Array As String("Date", "Bill Type", "Amount", "Collection")

' Calculate total width
Dim totalWidth As Int = 0
For i = 0 To colWidths.Length - 1
totalWidth = totalWidth + colWidths(i)
Next

' === Draw table headers in bold ===
PDFWriter1.setFont(Fonts.HELVETICA_BOLD, Fonts.HELVETICA_BOLD)
Dim xPos As Int = startX
For i = 0 To headers.Length - 1
' Center text in cell
Dim cellX As Int = xPos + (colWidths(i) / 2) - (headers(i).Length * 3) ' Adjust 3 for approx half-width char
PDFWriter1.addText(cellX, startY - (rowHeight / 2) - 2, 14, headers(i))
xPos = xPos + colWidths(i)
Next

' Draw horizontal lines for header
PDFWriter1.addLine(startX, startY, startX + totalWidth, startY)
PDFWriter1.addLine(startX, startY - rowHeight, startX + totalWidth, startY - rowHeight)

' Draw vertical lines (columns)
xPos = startX
For i = 0 To colWidths.Length
PDFWriter1.addLine(xPos, startY, xPos, startY - rowHeight)
If i < colWidths.Length Then xPos = xPos + colWidths(i)
Next
' === Draw table rows ===
PDFWriter1.setFont(Fonts.HELVETICA, Fonts.HELVETICA) ' normal font
Dim yPos As Int = startY - rowHeight
For rowIndex = 0 To TableData.Size - 1
Dim row As List = TableData.Get(rowIndex)
xPos = startX
For i = 0 To row.Size - 1
Dim cellText As String = row.Get(i)
Dim cellX As Int
If i = 1 Then
' Bill Type left-aligned
cellX = xPos + 2
Else
' Other columns centered
cellX = xPos + (colWidths(i) / 2) - (cellText.Length * 2.5)
End If
PDFWriter1.addText(cellX, yPos - (rowHeight / 2) - 2, 12, cellText)
xPos = xPos + colWidths(i)
Next

' Draw horizontal line below row
PDFWriter1.addLine(startX, yPos, startX + totalWidth, yPos)

' Draw vertical lines for this row
xPos = startX
For i = 0 To colWidths.Length
PDFWriter1.addLine(xPos, yPos + rowHeight, xPos, yPos)
If i < colWidths.Length Then xPos = xPos + colWidths(i)
Next

yPos = yPos - rowHeight

' Add new page if needed
If yPos < 50 Then
PDFWriter1.newPage
yPos = startY
End If
Next

' Convert PDF
PDFWriter1.ConverseDocument
End Sub
Sub PDFWriter1_ConversionDone(Content As String)
PDFContent = Content

' Save PDF to Downloads folder
Dim outDir As String = File.Combine(File.DirRootExternal, "Download")
If File.Exists(outDir, "") = False Then File.MakeDir(File.DirRootExternal, "Download")

Dim fileName As String = "TodayReport.pdf"
PDFWriter1.outputToFile(outDir, fileName, PDFContent, "ISO-8859-1")

xui.MsgboxAsync(" PDF created: " & fileName & " in Downloads", "Success")

Dim f As JavaObject
f.InitializeNewInstance("java.io.File", Array(File.Combine(File.DirRootExternal, "Download/TodayReport.pdf")))

' Get content URI from FileProvider
Dim uri As JavaObject = FileProvider.GetFileUri(f)

' Create sharing intent
Dim i As Intent
i.Initialize(i.ACTION_SEND, "") ' leave data empty
i.SetType("application/pdf")
i.Flags = 1
i.PutExtra("android.intent.extra.STREAM", uri)

' Optional: subject & text
i.PutExtra("android.intent.extra.SUBJECT", "My PDF Report")
i.PutExtra("android.intent.extra.TEXT", "Please check the attached report.")

' Show chooser
Dim chooser As Intent
chooser.Initialize("android.intent.action.CHOOSER", "")
chooser.PutExtra("android.intent.extra.INTENT", i)
chooser.PutExtra("android.intent.extra.TITLE", "Share PDF via")

StartActivity(chooser)
End Sub by using these code i can create the pdf that is alaso doenloaded in download but i cannot share these downloaded pdf through email and watsapp from these share panel how can i resolve these
 
Upvote 0
Sorry, I wasn't very clear. I currently have a JavaScript SDK in Windows that uses Chrome and communicates via this "postSharedBufferToScript" using a C# WebView2.
The WebView loads a fake HTML page containing a JavaScript bundle that exposes some methods.

I was wondering how this could be ported to B4A. The area can be quite large, as it could contain a file of up to 100 MB, which is then processed by the JavaScript bundle.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tip: FileDirRootExternal = broken code.

I was wondering how this could be ported to B4A. The area can be quite large, as it could contain a file of up to 100 MB, which is then processed by the JavaScript bundle.
1. Loading 100mb to the device memory can lead to OOM errors.

2. Make sure that the JavaScript SDK is actually compatible with Android.

3. Start with the straightforward implementation. It might work.
 
Upvote 0
Top