Hallo,
from time to time someone is asking on how to create a pdf with b4a.
I dont know how to do that ONLY with b4a.
Today i spend a few hours to write a b4a-example and a php-script which creates the pdf using fpdf.
ATTENTION: This b4a-example uses CreateMap so you need B4A V3.8 for it to work...
The logic "in app" is simple.
- A list with commands is created and send to php-script.
- The php-script do all given commands in order to create a pdf.
- The pdf is then written to disc and the url to this pdf is returned to the app.
- The app see this url and download this pdf
- After finished downloading the pdf it is showed in a pdf-viewer
As you can see; most of the fpdf-features are wrapped in php-script
To TEST you can use the url provided in the example. But please, if you want to USE it in your app. Please use your own webserver!
Hope you enjoy this example and be the code a kind of help in your future projects
Version 1.1 of PHP-Script now uses mpdf- instead of fpdf-class and is now able to use unicode
Due to the size of the php-archive you need to download it here. It´s all you need on php to run that on your server.
from time to time someone is asking on how to create a pdf with b4a.
I dont know how to do that ONLY with b4a.
Today i spend a few hours to write a b4a-example and a php-script which creates the pdf using fpdf.
ATTENTION: This b4a-example uses CreateMap so you need B4A V3.8 for it to work...
The logic "in app" is simple.
- A list with commands is created and send to php-script.
- The php-script do all given commands in order to create a pdf.
- The pdf is then written to disc and the url to this pdf is returned to the app.
- The app see this url and download this pdf
- After finished downloading the pdf it is showed in a pdf-viewer
B4X:
Dim cmds As List
cmds.Initialize
' "init" should be the first action
cmds.Add(CreateMap("action": "init", "grid": 5,"showfooter": True))
' Author, Creator, Title, Subject and Keywords should be called before the first "NewPage"
cmds.Add(CreateMap("action": "SetAuthor", "author": "Manfred Ssykor"))
cmds.Add(CreateMap("action": "SetCreator", "creator": "b4a-php-pdf"))
cmds.Add(CreateMap("action": "SetTitle", "title": "Title bla bla bla"))
cmds.Add(CreateMap("action": "SetSubject", "subject": "Subject bla bla bla"))
cmds.Add(CreateMap("action": "SetKeywords", "keywords": "Keywords bla "))
' NewPage creates a new Page in pdf
cmds.Add(CreateMap("action": "NewPage"))
cmds.Add(CreateMap("action": "SetFont","font": "DejaVu","style": "","size": 12))
cmds.Add(CreateMap("action": "settext", "text": "Примерен доставчик ООД","border": "LBRT", "x": 140, "y": 10, "w": 72, "h": 10, "r": 255,"g": 0,"b": 0,"align":"C"))
' SetFont can be used to change the font used in pdf after this action
cmds.Add(CreateMap("action": "SetFont","font": "courier","style": "","size": 12))
' SetText writes a text into the pdf.
' border can be one OR more chars out of L, B, T, R (resp. Left, Bottom, Top, Right)
' x, y, w and h are the position and width/height of textbox
' r, g, b are the color for this text in RGB-format
' align can be L,C,R (leftjustify, center, rightjustify
cmds.Add(CreateMap("action": "SetText", "text": " ","border": "LBR", "x": 5, "y": 5, "w": 72, "h": 5, "r": 64,"g": 64,"b": 64,"align":"L"))
cmds.Add(CreateMap("action": "setfont","font": "courier","style": "","size": 14))
' Red Text
cmds.Add(CreateMap("action": "settext", "text": "this is a test","border": "", "x": 5, "y": 10, "w": 72, "h": 5, "r": 255,"g": 0,"b": 0,"align":"C"))
' Green Text
cmds.Add(CreateMap("action": "settext", "text": "Hello world","border": "B", "x": 5, "y": 25, "w": 72, "h": 5, "r": 0,"g": 255,"b": 0,"align":"L"))
' Blue Text
cmds.Add(CreateMap("action": "SetText", "text": "Hellooooo :-)","border": "", "x": 5, "y": 50, "w": 72, "h": 5, "r": 0,"g": 0,"b": 255,"align":"L"))
' SetDrawColor definies the color for lines, boxes and so on
' r,g,b is the color to use for drawings in RGB-format
cmds.Add(CreateMap("action": "SetDrawColor", "r": 128, "g": 128, "b": 128))
cmds.Add(CreateMap("action": "line", "x1": 5, "y1": 50, "x2": 72, "y2": 5))
' SetFillColor definies the color for filling rects for example
' r,g,b is the color to use for fill in RGB-format
cmds.Add(CreateMap("action": "SetfillColor", "r": 128, "g": 0, "b": 128))
' rect draws a rectangle
' x, y, w, h is the position and size of the rect
' style can be
' D OR empty String: draw. This Is the default value.
' F: fill
' DF OR FD: draw AND fill
cmds.Add(CreateMap("action": "rect", "x": 5, "y": 75, "w": 25, "h": 25, "style":"DF"))
' SetImage put a image into the pdf
' imgurl = url of the image
' x,y,w,h is the position and size of the image
' type can be JPG, JPEG, PNG and GIF.
cmds.Add(CreateMap("action": "SetImage", "imgurl": "http://chart.googleapis.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World", "x": 40, "y": 80, "w": 180, "h": 75,"type": "png"))
' SetLineWidth sets the width of a line of rect´s or line´s (float)
cmds.Add(CreateMap("action": "SetLinewidth", "w": 1.5))
cmds.Add(CreateMap("action": "SetImage", "imgurl": "http://pdf.basic4android.de/b4arulez.gif", "x": 5, "y": 160, "w": 75, "h": 75,"type": "gif"))
cmds.Add(CreateMap("action": "SetDrawColor", "r": 128, "g": 0, "b": 128))
cmds.Add(CreateMap("action": "line", "x1": 48, "y1": 180, "x2": 70, "y2": 175))
cmds.Add(CreateMap("action": "setfont","font": "courier","style": "BI","size": 16))
cmds.Add(CreateMap("action": "settext", "text": "Erel rulez!","border": "0", "x": 75, "y": 170, "w": 25, "h": 5, "r": 255,"g": 0,"b": 0,"align":"L"))
cmds.Add(CreateMap("action": "SetImage", "imgurl": "http://pdf.basic4android.de/icon.png", "x": 125, "y": 180, "w": 72, "h": 72,"type": "png"))
Dim json As JSONGenerator
json.Initialize2(cmds)
Log(json.ToString)
Dim job As HttpJob
Dim p As PhoneId
job.Initialize("createpdf", Me)
job.download2("http://pdf.basic4android.de/", Array As String( _
"json", json.ToString, _
"DeviceID", p.GetDeviceId _
))
B4X:
Sub JobDone(job As HttpJob)
ProgressDialogHide
If job.Success = True Then
If job.JobName = "getpdf" Then
Dim OutStream As OutputStream
Log("DownloadReady: "&job.Tag)
OutStream = File.OpenOutput(File.DirRootExternal, job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
File.Copy2(job.GetInputStream,OutStream) ' save the file
OutStream.Close
Log(job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
Dim i As Intent
i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal & "/", job.Tag))
'i.SetComponent("android/com.android.internal.app.ResolverActivity")
i.SetType("application/pdf")
StartActivity(i)
End If
If job.JobName = "createpdf" Then
Log(job.GetString)
Dim parser As JSONParser
parser.Initialize(job.GetString)
Dim m As Map = parser.NextObject
Dim pdf As String = m.Get("pdf")
Dim resultcode As Int = m.Get("resultcode")
If resultcode = 0 Then
If pdf <> "" Then
Dim job As HttpJob
job.Initialize("getpdf", Me)
job.Tag = "result.pdf"
job.download(pdf)
End If
End If
End If
Else
Log("Error: " & job.ErrorMessage)
End If
End Sub
As you can see; most of the fpdf-features are wrapped in php-script
To TEST you can use the url provided in the example. But please, if you want to USE it in your app. Please use your own webserver!
Hope you enjoy this example and be the code a kind of help in your future projects
Version 1.1 of PHP-Script now uses mpdf- instead of fpdf-class and is now able to use unicode
Due to the size of the php-archive you need to download it here. It´s all you need on php to run that on your server.
Attachments
Last edited: