Android Question [Solved] B4A creation pdf file from layout

TomKluz

Active Member
Licensed User
Hello,
I would like to print a raport from my app, eventually only create a pdf raport file

Some time ago I was exerciseing with a B4J and due Mr Erel recomendation and help of Stevel05 jFX8Print library is working.
Recently I would like to make the same printout (or pdf) in case of B4A after seeing example/tutorial: https://www.b4x.com/android/forum/threads/76712/#content.

After starting this example It seems to be that all 'shapes' and text appears by using Canvas features which makes effect on the printout (pdf file) but not on a display.
In B4J I have creatd a printout layout (file attached) which is displayed first. It cosist of many Label's elements and few ImageViews. I would like to achiewe exactly the same effect in B4A.

Should I 'drow' this raport and place text fields from the begining by Canvas in case of B4A ?
Is there another way to use a layout I have already created and then print a display on A4 paper ?

Kind regards
TomKluz
 

TomKluz

Active Member
Licensed User
Thank you.

May you please, give (point) me some example of using PDFDocument library in this case ?

I am trying myself to find a documentation but it is not easy. Only source I have found is B4X website and documentation of XUI library, but eaven there
I can't find a general description what it is and a reason to use it.
Obviously I am strictly following to your instructions.

Kind regards
TomKluz
 
Upvote 0

edgar_ortiz

Active Member
Licensed User
Longtime User
You can check:

 
Upvote 0

TomKluz

Active Member
Licensed User
This is what I am doing, but without a final result.
I have used your example to test of saving my layout into pdf file.
File is created but it is empty (white). I was trying to change ractangle parameters with no positiv effect.
I have attached this example.

May I ask for checking what is wrong with me?

Thank you in advance. TomKluz
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Never use File.DirDefaultExternal.
2. The layout is loaded with an animation. You need to let this animation complete.

This code produces:
<removed>

B4X:
Sub Globals
    Private rp As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Formularz_druk")
    Sleep(1000)
    Dim x As B4XView = Activity
    Dim bmp As Bitmap = x.Snapshot
    Dim pdf As PdfDocument
    pdf.Initialize
    pdf.StartPage(595, 842) 'A4 size
    Dim ScrRect As Rect
    ScrRect.Initialize(0, 0, bmp.Width,bmp.Height)
    Dim DestRect As Rect
    DestRect.Initialize(0, 0, 595, 842)
    pdf.Canvas.DrawBitmap(bmp, Null, DestRect)
    pdf.FinishPage
    Dim out As OutputStream = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "1.pdf", False)
    pdf.WriteToStream(out)
    out.Close
    pdf.Close
End Sub
 
Last edited:
Upvote 0

TomKluz

Active Member
Licensed User
Hello Mr Erel and Community,

In fact I have got a pdf file from my layout. This was my wish and I got it. Many thanks.

I would like to ask additional questions:
1. I had no idea that my layout is loaded with an animation. Pictures used are simple jpg files.
Is it my foult or is it possible to load it without of animation ?
2. The size of pdf file (1.pdf) is 10 times bigger then corresponding pdf file done by PdfCreator
used by B4J app to do it.
Is it a way to obtain smoller size of 1.pdf ?
3. I have a problem to see 1.pdf file via PC (Win 7) after connecting a smartfone by USB cable.
There is no problem to see it using Android file manager.
Is there any trik or something I am missing ?

Kind regards
Tomkluz
 
Upvote 0
Top