B4A Library Java-fPDF library - Create PDF files in B4A

Given that I have (and will be making use of several of the libraries that you have graciously provided, I thought I would share as well.
My first attempt, so please be gentle...

This library is an incomplete wrap of this Github library ==> https://github.com/nkiraly/Java-FPDF, which is a wrap of the fpdf found here http://www.fpdf.org/.

It is not yet complete, but I thought I would provide it to the community to try out.

Known Issues
* Image is not working as Android does not appear to support the javax.imageio classes which is required. I'd welcome any suggestions for an alternative.
* StarPolygon is not working, not sure why
* The legacy debugger needs to be used as the quick debugger will cause the following error:
Unexpected event (missing RaiseSynchronousEvents): pdfdoc_header
it will work ok in release mode. Would welcome any suggestions to fix this

The sample code shows how to use.

You also need to include java-fpdf.jar in your additional libraries directory.

Enjoy!

PDFDoc
Comment:
This Class Is used to define line styles
Author: Digitwell Solutions Ltd - Andrew Marshall
Version: 0.35
  • PDFDOCCoordinate
    Methods:
    • GetX As Float
    • GetY As Float
    • Initialize (x As Float, y As Float)
    • isInitialized As Boolean
  • PDFDoc
    Events:
    • acceptpagebreak ( As )
    • footer ( As )
    • header ( As )
    Fields:
    • IMAGETYPE_GIF As Int
    • IMAGETYPE_JPEG As Int
    • IMAGETYPE_PNG As Int
    • POSITION_BELOW As Int
    • POSITION_NEXTLINE As Int
    • POSITION_RIGHTOF As Int
    Methods:
    • AddLink As Int
      Creates a new Internal Link
      Return type: @return:the link identifier
    • AddPage (portrait As Boolean) As Int
      Starts a new page with the orientation as specified
      portrait: - boolean, true is portrait, false is landscape
      Return type: @return:-1 if an error or the number of the current page.
    • Cell (w As Float, h As Float, txt As String, bd As PDFDocBorders, ln As Int, al As String, fill As Boolean, link As Int)
      Outputs a cell on the page
      w: - Width of cell
      h: - Height of cell
      txt: - Text of the cell
      al: - Alignment, "L" left also default value, "R" right , "C" centered, "J" justified
      fill: - True to fill cell
      link: - link identifier of the cell
    • Circle (coords As PDFDocCoordinate, r As Float, aStart As Float, aFinish As Float, mode As String, segments As Int)
      Draws a circle.
      coords: the base coordinates
      r: the radius
      aStart: the starting angle
      aFinish: the finishing angle
      mode: the drawing mode - "S" is Shape, "F" is filled, "B" is both
      segments: the number of segments
    • Curve (start As PDFDocCoordinate, pt1 As PDFDocCoordinate, pt2 As PDFDocCoordinate, end As PDFDocCoordinate, drawmode As String)
      Draw a bezier curve
      start: - coord
      pt1: - 1st control coord
      pt2: - 2nd control coord
      end: - end coord
      drawmode: - "S" is Shape, "F" is filled, "B" is both
    • Ellipse (coords As PDFDocCoordinate, rx As Float, ry As Float, angle As Float, aStart As Float, aFinish As Float, mode As String, segments As Int)
      Draws an ellipse.
      coords: the base coordinates
      rx: the horizontal radius
      ry: the vertical radius
      angle: the orientation angle
      aStart: the starting angle
      aFinish: the finishing angle
      mode: the drawing mode - "S" is Shape, "F" is filled, "B" is both, "C" for closed ellipse
      segments: the number of segments
    • GetStringWidth (s As String) As Float
      Gets the width of the string in the current font
      String:
      Return type: @return:- Float, size of the string
    • GetX As Float
    • GetY As Float
    • Image (file As String, coords As PDFDocCoordinate, w As Float, h As Float, it As Int, link As Int)
      Puts an Image on the page
      file: - the file name of the image
      coords: - A PDFDocCordinate object with the coordinates on the page
      w: - width of the image
      h: - height of the image
      it: - type of image, 0 = PNG, 1 = JPEG, 2 = GIF
      link: - link identifier of the image or 0
    • Initalize (portrait As Boolean, Eventname As String)
      Initialise the PDFDoc object and set up the callbacks for header and footer
      ba: - The BA object
      portrait: - True for Portriat, False for Landscape
      Eventname: - The event name to call back for the Header and Footer drawing. The system will call eventname_header and eventname_footer
    • Line (start As PDFDocCoordinate, end As PDFDocCoordinate)
      Draw a line between 2 points on the page
      start: - Starting coordinate
      end: - ending coordinate
    • Link (x As Float, y As Float, w As Float, h As Float, link As Int)
      Put a link on the page
      x: - x position
      y: - y position
      w: - width
      h: - height
      link: - link id
    • Ln (h As Float)
      Line feed
      h: - The height of the line feed
    • MultiCell (w As Float, h As Float, txt As String, bd As PDFDocBorders, al As String, fill As Boolean)
      Output the text with automatic or explicit line breaks
      w: - Width
      h: - Height
      txt: - The text
      bd: - The borders as a PDFDocBorders object,
      al: - Alignment, "L" left also default value, "R" right , "C" centered, "J" justified
      fill: - True to fill cell
    • OutputFile (Filename As String)
      Ouputs the Generated PDF to a File passed in.
      Filename: - String containing the filename
    • OutputString As String

      Return type: @return:- A string version of the PDF output
    • PageNo As Int
      Return the current page number
      Return type: @return:- Page number
    • Polygon (coordlist() As PDFDocCoordinate, drawmode As String)
      Draw a polygon
      coordlist: - an array of PDFDocCoordinate Objects
      drawmode: - "S" is Shape, "F" is filled, "B" is both
    • Rect (coords As PDFDocCoordinate, w As Float, h As Float, drawmode As String)
      Draws a rectangle on the page
      coords: - Coordinates of the top left corner
      w: - Width of the rect
      h: - height of the rect
      drawmode: - "S" is Shape, "F" is filled, "B" is both
    • RegularPolygon (coords As PDFDocCoordinate, r As Float, sides As Int, angle As Float, mode As String)
      Draw a regular polygon
      coords: the base coordinates
      r: the radius
      sides: the number of sides
      angle: the orientation angle
      mode: the drawing mode - "S" is Shape, "F" is filled, "B" is both
    • RoundedRect (coord As PDFDocCoordinate, w As Float, h As Float, r As Float, drawmode As String)
      Draws a rounded rectangle
      coords: - Coordinates of the top left corner
      w: - Width of the rect
      h: - height of the rect
      r: - the corner radius
      drawmode: - "S" is Shape, "F" is filled, "B" is both
    • SetAuthor (author As String)
      Set the Author of the document
      author:
    • SetDrawColor (r As Int, g As Int, b As Int)
      Sets the rgb value of the drawing colour for all stroke operations
      r:
      g:
      b:
    • SetFillColor (r As Int, g As Int, b As Int)
      Sets the rgb colour for all fill operations
      r:
      g:
      b:
    • SetFont (FontName As String, style As String, size As Float)
      Sets the font for all further text
      FontName: - Name of the font
      style: - Can be either B for Bold, I for Italic, U for underline or Null
      size: - size in point of the font
    • SetLeftMargin (m As Float)
      Set the left Margin
      m: - The margin value
    • SetLineStyle (ls As PDFDocLineStyle)
      Set the line style using the PDFDocLineStyle object
      ls:
    • SetLineWidth (width As Float)
      Sets the thickness of the frame in mm
      width:
    • SetLink (link As Int, y As Float, page As Int)
      Sets the destination of an internal link
      link: - Link ID created by AddLink
      y: - target position; -1 indicates the current position. The default value is 0 (top of page).
      page: - Number of target page; -1 indicates the current page. This is the default value.
    • SetMargins (left As Float, top As Float, right As Float)
      Set all margins
      left:
      top:
      right:
    • SetRightMargin (margin As Float)
      Set the right Margin
      margin:
    • SetSubject (subject As String)
      Set the Subject of the document
      subject:
    • SetTextColor (r As Int, g As Int, b As Int)
      Set the text color for all text operations
      r:
      g:
      b:
    • SetTitle (title As String)
      Set the title of the document
      title:
    • SetTopMargin (margin As Float)
      Set the top margin
      margin:
    • SetX (xpos As Float)
      Set the x position on the page
      xpos:
    • SetXY (xpos As Float, ypos As Float)
      Set the X,Y position on the page, negative numbers are allowed for the y position
      xpos:
      ypos:
    • SetY (ypos As Float)
      Set the Y position on the page, negative numbers position from the bottom
      ypos:
    • StarPolygon (coords As PDFDocCoordinate, r As Float, vertices As Int, gaps As Int, angle As Float, mode As String)
      Draws a star polygon.
      coords: the base coordinates
      r: the radius
      vertices: the number of vertices
      gaps: the number of gaps
      angle: the orientation angle
      mode: the drawing mode - "S" is Shape, "F" is filled, "B" is both
    • Write (h As Float, txt As String, link As Int)
      Output text in flowing mode
      h: - Height
      txt: - string to write
      link: - Link id
    • aliasNbPages (alias As String)
      Set the alias for the total number of pages or null to use the default "{nb}"
      alias:
    • close
    • open
  • PDFDocBorders
    Methods:
    • Initialize (left As Boolean, top As Boolean, right As Boolean, bottom As Boolean)
      Set the borders for a cell
      left: - True to display border
      top-: True to display border
      right: - True to display border
      bottom: - True to display border
    • isInitialized As Boolean
      Check if the object is initialised
      Return type: @return:true if initialised
  • PDFDocLineStyle
    Fields:
    • CAP_BUTT As Int
    • CAP_ROUND As Int
    • CAP_SQUARE As Int
    • JOIN_BEVEL As Int
    • JOIN_MITER As Int
    • JOIN_ROUND As Int
    Methods:
    • GetCap As Int
      Get the Cap style
      Return type: @return:will be one of CAP_BUTT, CAP_ROUND, CAP_SQUARE
    • GetColor As Int[]
    • GetDashes As Float[]
      Get the dash pattern.
      Return type: @return:the dashes
    • GetJoin As Int
      Get the Join style
      Return type: @return:will be one of JOIN_MITER, JOIN_ROUND, JOIN_BEVEL
    • GetPhase As Float
      Get the dash phase
      Return type: @return:the phase
    • GetWidth As Float
      Return the width of the Linestyle
    • Initialize
      Initialize the object
    • SetCap (linestyle As Int)
      Set the Cap style
      linestyle: - Can be one of CAP_BUTT, CAP_ROUND, CAP_SQUARE
    • SetColor (r As Int, g As Int, b As Int)
    • SetDashes (dashes() As Float)
      Set the dash pattern. The dash pattern is given as an array of length
      values for the dashes and gaps. <br>
      For example: <code>{1.0f}</code> represents <code>1.0f</code> long
      dashes with <code>1.0f</code> long gaps;<br>
      <code>{2.0f, 1.0f}</code> represents <code>2.0f</code> long dashes
      with <code>1.0f</code> long gaps; etc.
      dashes: the dashes to set
    • SetJoin (joinstyle As Int)
      Set the Join style
      linestyle: - Can be one of JOIN_MITER, JOIN_ROUND, JOIN_BEVEL
    • SetWidth (width As Float)
      Sets the line width
      width: - the width to set
    • Setphase (phase As Float)
      Set the dash phase. The phase value shifts the point at which the dash
      pattern starts.
      phase: the phase to set
    • isInitialized As Boolean
      Check if the object is initialised
      Return type: @return:true if initialised
    • toString As String
      Line style as a string
 

Attachments

  • java-fpdf.jar
    66.5 KB · Views: 349
  • PDFDoc.zip
    12.2 KB · Views: 327
  • PDFTest.zip
    62.8 KB · Views: 350

Phayao

Active Member
Licensed User
Longtime User
Hello,
that looks like a great lib - since I have been struggling with PDFWriter (very basic) and formerly jPDF (yielding unpredictable results for multiline cells and images inside tables).
This example here gives me the following error when hitting btn1 (B4A v. 6.50):

** Service (starter) Create **
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Creating Portrait
java.lang.VerifyError: com/koadweb/javafpdf/FPDF
at uk.co.digitwell.libraries.pdfdoc.PDFDoc.Initalize(PDFDoc.java:71)
at uk.co.digitwell.sample.PDFTest.main._btn2_click(main.java:603)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19421)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5487)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)

I have copied java-jpdf.jar and the other .jar and .xml to my addLib folder.
I'm using Android V.25, maybe that's the problem ?

Thank you !
Chris
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Hi Chris,
Does the sample PDFTest app work?

I've just tried setting the target SDK to 25 and using the v25 android.jar and the sample app works fine.

Can you upload a sample app and I will take a look.

Cheers

Andrew
 

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Hi Chris,
Just downloaded, installed and tried on my android test devices:

HTC one M7, Kindle Fire (5th Gen), Huawei Honor 7

works fine on all.

What are device are you testing on?
 

Phayao

Active Member
Licensed User
Longtime User
thanks again - i'm using Samsung Note 8. I will try with the emulator and i can borrow another phone to test.
Let me test it and I give the results.
Hi Chris,
Just downloaded, installed and tried on my android test devices:

HTC one M7, Kindle Fire (5th Gen), Huawei Honor 7

works fine on all.

What are device are you testing on?
Hi Chris,
Just downloaded, installed and tried on my android test devices:

HTC one M7, Kindle Fire (5th Gen), Huawei Honor 7

works fine on all.

What are device are you testing on?

Hi Andrew,
well it's really strange - i'm using Samsung Note 8 and another Samsung phone and both produced this error.
I have to leave it as a mystery at the moment - trying with other phones as sson as I can get hands on.
Thanks for your quick replies !

Chris
 

DonManfred

Expert
Licensed User
Longtime User
Works fine on a Samsung S7 EDGE too
 
Top