B4A Library HtmlToPdf

Hello everybody,

I've created this library that converts an html file (or string) to a pdf file programmatically.

A couple of important notes: it only works on devices with SDK versions >= 21 and it cannot read files from the asset folder (you can either copy the file to the internal folder and use it from there or read the content with File.ReadString and use the result)

You can find an example attached to this thread.

  • PalmoHtmlToPdf
    • Events:
      • Finished (Success As Boolean)
    • Functions:
      • ConvertFromFile(DirectoryInput as String,FilenameInput as String,DirectoryOutput as String,FilenameOutput as String)
      • ConvertFromString(htmlData as String, DirectoryOutput as String, FilenameOutput as String)
      • Initialize (EventName As String)
 

Attachments

  • HtmlToPdf_example.zip
    9.5 KB · Views: 668
  • HtmlToPdf.zip
    7.2 KB · Views: 705

Johan Schoeman

Expert
Licensed User
Longtime User
I know this is an old thread but the error that I get relates to this library (or my ignorance). I have taken your script from your example.html in the /Files folder and created a string lateral with it. Then used your library method to pass the string literal to your library to create the PDF. The sub looks as follows and creates the PDF successfully that can be opened with and of the PDF viewers that I have on my device:

Creates an openable PDF:
Sub Print
    'Works only on devices with SDK >= 21
    Dim rr As RuntimePermissions
    rr.CheckAndRequest(rr.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
  
    Activity.LoadLayout("0")
    phtmltopdf.Initialize("phtmltopdf")
    'You can't use a file from the assets folder
'    File.Copy(File.DirAssets,"example.html",File.DirInternal,"example.html")
    'You can create the pdf directly from an html file
  
    Dim ssl As String = $"
<html>
<!-- Text between angle brackets is an HTML tag and is not displayed.
Most tags, such as the HTML and /HTML tags that surround the contents of
a page, come in pairs; some tags, like HR, for a horizontal rule, stand
alone. Comments, such as the text you're reading, are not displayed when
the Web page is shown. The information between the HEAD and /HEAD tags is
not displayed. The information between the BODY and /BODY tags is displayed.-->
<head>
<title>Enter a title, displayed at the top of the window.</title>
</head>
<!-- The information between the BODY and /BODY tags is displayed.-->
<body>
<h1>Enter the main heading, usually the same as the title.</h1>
<p>Be <b>bold</b> in stating your key points. Put them in a list: </p>
<ul>
<li>The first item in your list</li>
<li>The second item; <i>italicize</i> key words</li>
</ul>
<p>Improve your image by including an image. </p>
<p><img src="https://alternative.me/icons/basic4android.png" alt="A Great HTML Resource"></p>
<p>Add a link to your favorite <a href="https://www.dummies.com/">Web site</a>.
Break up your page with a horizontal rule or two. </p>
<hr>

</body>
</html>"$

  
    phtmltopdf.ConvertFromString(ssl,File.DirRootExternal,"example.pdf")
  
End Sub

When I remove the last 4 lines of script the PDF created will not open:
B4X:
Sub Print
    'Works only on devices with SDK >= 21
    Dim rr As RuntimePermissions
    rr.CheckAndRequest(rr.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
  
    Activity.LoadLayout("0")
    phtmltopdf.Initialize("phtmltopdf")
    'You can't use a file from the assets folder
'    File.Copy(File.DirAssets,"example.html",File.DirInternal,"example.html")
    'You can create the pdf directly from an html file
  
    Dim ssl As String = $"
<html>
<!-- Text between angle brackets is an HTML tag and is not displayed.
Most tags, such as the HTML and /HTML tags that surround the contents of
a page, come in pairs; some tags, like HR, for a horizontal rule, stand
alone. Comments, such as the text you're reading, are not displayed when
the Web page is shown. The information between the HEAD and /HEAD tags is
not displayed. The information between the BODY and /BODY tags is displayed.-->
<head>
<title>Enter a title, displayed at the top of the window.</title>
</head>
<!-- The information between the BODY and /BODY tags is displayed.-->
<body>
<h1>Enter the main heading, usually the same as the title.</h1>
<p>Be <b>bold</b> in stating your key points. Put them in a list: </p>
<ul>
<li>The first item in your list</li>
<li>The second item; <i>italicize</i> key words</li>
</ul>

<hr>

</body>
</html>"$

  
    phtmltopdf.ConvertFromString(ssl,File.DirRootExternal,"example.pdf")
  
End Sub

Why wont the PDF be openable?
 

Attachments

  • HtmlToPDFJHS.zip
    10 KB · Views: 72

Johan Schoeman

Expert
Licensed User
Longtime User
When I delete the PDF that was created (in DirRootExternal) and run the amended HTML script then it seems to be working fine. For some reason Android does not seem to like the PDF to be overwritten if it already exists. Anyone that can please shed some light on this observation?
 

Johan Schoeman

Expert
Licensed User
Longtime User
Here is an update for the library that will allow one to create the PDF in either Portrait or Landscape modes. Jar and XML attached

B4X:
    phtmltopdf.Initialize("phtmltopdf")
    phtmltopdf.Orientation = phtmltopdf.LANDSCAPE

PalmoHtmlToPdf

Author:
Palmosoft, modified by Johan Schoeman
Version: 1.1
  • PalmoHtmlToPdf
    • Fields:
      • LANDSCAPE As Int
      • PORTRAIT As Int
    • Functions:
      • ConvertFromFile (paramString1 As String, paramString2 As String, DirectoryOutput As String, FilenameOutput As String)
      • ConvertFromString (paramString1 As String, DirectoryOutput As String, FilenameOutput As String)
      • Initialize (paramString As String)
    • Properties:
      • Orientation As Int [write only]
 

Attachments

  • PalmoHtmlToPdf.jar
    7.3 KB · Views: 68
  • PalmoHtmlToPdf.xml
    2.5 KB · Views: 58

Johan Schoeman

Expert
Licensed User
Longtime User
Here is another update. It should show as V1.2 in the B4A IDE. With my primitive knowledge of HTML it builds beautiful PDF's including tables.

Have done away with the "Orientation" in the wrapper and added a Builder to the Wrapper. The Builder can for now do the following:
1. setMediaSize - options for now are:
MediaSize Options:
    MediaSize_ISO_A4
    MediaSize_ISO_A4_asLandscape
    MediaSize_ISO_A0
    MediaSize_ISO_A1
    MediaSize_ISO_A10
    MediaSize_ISO_A2
    MediaSize_ISO_A3
    MediaSize_ISO_A3_asLandscape

I am attaching the Java code in case you want to edit it and add additional MediaSizes to the Builder(other than the 8 listed above)

2. setResolution
3. setMargins
4. setColorMode
5. setDuplexMode
Duplex Mode:
    DUPLEX_MODE_LONG_EDGE
    DUPLEX_MODE_NONE
    DUPLEX_MODE_SHORT_EDGE

JAR and XML also attached.

Sample Code:
    phtmltopdf.Initialize("phtmltopdf")
    phtmltopdfBulder.Initialize
    phtmltopdf.PrintAttributes = phtmltopdfBulder.setMediaSize(phtmltopdfBulder.MediaSize_ISO_A4) _
                                    .setResolution(600, 600) _
                                    .setMargins(10, 10, 10, 10) _
                                    .build

PalmoHtmlToPdf

Author:
Palmosoft, modified by Johan Schoeman
Version: 1.2
  • PalmoHtmlToPdf
    • Events:
      • finished (value As Boolean)
    • Functions:
      • ConvertFromFile (paramString1 As String, paramString2 As String, DirectoryOutput As String, FilenameOutput As String)
      • ConvertFromString (paramString1 As String, DirectoryOutput As String, FilenameOutput As String)
      • Initialize (paramString As String)
    • Properties:
      • PrintAttributes As android.print.PrintAttributes [write only]
        Build the Builder and then pass the Builder to this method. Eg:
        Example:
        phtmltopdf.PrintAttributes = phtmltopdfBulder.setMediaSize(phtmltopdfBulder.MediaSize_ISO_A4).setResolution(600, 600).setMargins(10, 10, 10, 10).build
  • printAttributeBuilder
    • Fields:
      • DUPLEX_MODE_LONG_EDGE As Int
      • DUPLEX_MODE_NONE As Int
      • DUPLEX_MODE_SHORT_EDGE As Int
      • MediaSize_ISO_A0 As Int
      • MediaSize_ISO_A1 As Int
      • MediaSize_ISO_A10 As Int
      • MediaSize_ISO_A2 As Int
      • MediaSize_ISO_A3 As Int
      • MediaSize_ISO_A3_asLandscape As Int
      • MediaSize_ISO_A4 As Int
      • MediaSize_ISO_A4_asLandscape As Int
    • Functions:
      • build As android.print.PrintAttributes
        Create the Builder after all other builder methods were set.
      • Initialize
        Initialize the builder
      • IsInitialized As Boolean
      • setColorMode (col As Int) As printAttributeBuilder
      • setDuplexMode (mode As Int) As printAttributeBuilder
        A valid duplex mode or zero.
        Value is either 0 or a combination of PrintAttributes.DUPLEX_MODE_NONE, PrintAttributes.DUPLEX_MODE_LONG_EDGE, and PrintAttributes.DUPLEX_MODE_SHORT_EDGE
      • setMargins (L As Int, T As Int, W As Int, H As Int) As printAttributeBuilder
        Specifies content margins in Mills
      • setMediaSize (mediasize As Int) As printAttributeBuilder
        Specifies a supported media size.
        Media size is the dimension of the media on which the content is printed.
        For example, the NA_LETTER media size designates a page with size 8.5" x 11".
      • setResolution (x As Int, y As Int) As printAttributeBuilder
        Specifies a supported resolution in DPI (dots per inch)
 

Attachments

  • JavaCode.zip
    4.3 KB · Views: 64
  • PalmoHtmlToPdf.jar
    8.2 KB · Views: 78
  • PalmoHtmlToPdf.xml
    7.6 KB · Views: 84
Last edited:
Top