SimplyPDF — Create PDF in Android through code or JSON

Soumya Kanti Kar
wwdablu
Published in
5 min readMay 19, 2019

[Update]
Version 2.x has been released since the last release of version 1.2.0. In here I have added several features, removed some and fixed several defects too. This article is updated based on Version 2.x.

Following are the list of changes which have been incorporated as part of Version 2.x release:

  • Switched from Java to Kotlin
  • Min SDK has been increase from 22 to 23
  • Added the feature to add custom page modifiers like PageHeaders.
  • Feature to provide custom margin values from both code and JSON
  • Ability to add custom converts which can use custom composers via JSON
  • Margins and alignments have been fixed for all the native composers
  • Tables now support the addition of bitmaps via ImageCell.
  • JSONEngine has been made more modular to support custom nodes.
  • Composer specific tests have been moved out from MainActivity to respective classes.
  • Code restructuring
  • Dependent libraries have been updated
  • Bug fixes that were observed during testing
  • ShapeComposer has removed as it can be handled by ImageComposer directly.

Version 2.x contains several breaking changes, so it will not be a straight upgrade from the previous versions. Most of the code will remain the same but few changes will be required.

At first we need to create a PDF document on which the content will be written.

simplyPdfDocument = SimplyPdf.with(this, fileToSaveIn)
.colorMode(DocumentInfo.ColorMode.COLOR)
.paperSize(PrintAttributes.MediaSize.ISO_A4)
.margin(Margin(startMargin, topMargin, endMargin, bottomMargin)
.pageModifier(PageHeader())
.firstPageBackgroundColor(Color.WHITE)
.paperOrientation(DocumentInfo.Orientation.PORTRAIT)
.build();

This will provide us with the object through which we can access the PDF document and perform actions on it. Internally it uses the PrintedPdfDocument provided by Android. SimplyPdfDocument provides additional methods and makes the PDF creation much simpler.

SimplyPdfDocument also provide access to the default composers which will be discussed in the later part of the story. They can be accessed as following:

  • simplyPdfDocument.text — To access the default TextComposer
  • simplyPdfDocument.image — To access the default ImageComposer

Composers

Composers are classes which are used to render content in the PDF document. The library as of now provides three (2) UnitComposers and one (1) GroupComposer.

UnitComposers are those composers which provide a singular objective and is it the leaf node of the Composer tree. They are:

  • TextComposer — Allows the developer to write text content in PDF
  • ImageComposer — Allows the developer to draw bitmap images

GroupComposers on the other hand uses the objective of the UnitComposers and allow homogenous or heterogeneous objective. Currently it is:

  • TableComposer — Allows the developers to render text and bitmap contents within a table format. They use TextCell and ImageCell for the purpose of rendering.

A composer is also supported by its respective property class. The property class defines the property of the composer as to how should it behave. For example, it can be text color for the TextComposer.

Composers and Properties

Let us consider TextComposer. This allows the developer to write textual content in the PDF. Its supported property class is TextProperties.

simplyPdfDocument.text.write("Hello World", TextProperties().apply {
textSize = 12
textColor = "#000000"
typeface = Typeface.DEFAULT
})

In here we are using the TextComposer to write the text SimplyPDF on the simplePdfDocument. The text will be of size 12, with black text color and default typeface. An operation performed using a default Composer must be provided with a Property object.

TextComposer
ImageComposer
TableComposer
TableComposer sample

JSON Driven Approach

This approach tries to simplify the process a bit more than writing more code. Here a JSON is required to be defined which will drive the process of PDF creation. Some of the benefits of this approach:

  • No PDF generation related code is written in the application and hence it is entirely abstracted.
  • JSON can be obtained from remote server which provides the benefit of dynamic content update.
  • JSON can be used as template to replace placeholders and then use it to generate PDF.

Consider the below JSON structure:

{
"page" : [
{
"type" : "world"
},
{
"type" : "setup",
"margin" : {
"start" : 125,
"top" : 125,
"end" : 125,
"bottom" : 125
},
"backgroundcolor" : "#C8C8C8"
},
{
"type" : "header",
"contents" : [
{
"type" : "text",
"content" : "Simplypdf",
"properties" : {
"size" : 24,
"color" : "#000000",
"underline" : false,
"strikethrough" : false,
"alignment" : "ALIGN_CENTER"
}
},
{
"type" : "text",
"content" : "Version 2.0",
"properties" : {
"size" : 20,
"color" : "#000000",
"underline" : true,
"strikethrough" : false,
"alignment" : "ALIGN_CENTER"
}
}
]
}
],
"contents" : [
{
"type" : "text",
"content" : "Simplypdf developed by Soumya Kanti Kar",
"properties" : {
"size" : 24,
"color" : "#000000",
"underline" : true,
"strikethrough" : false
}
},
{
"type" : "image",
"source" : "https://avatars0.githubusercontent.com/u/28639189?s=400&u=bd9a720624781e17b9caaa1489345274c07566ac&v=4"
},
{
"type" : "text",
"content" : "Source code published in GitHub",
"properties" : {
"size" : 20,
"color" : "#000000",
"underline" : true,
"strikethrough" : false
}
},
{
"type" : "newpage"
},
{
"type" : "space",
"height" : 125
},
{
"type" : "table",
"contents" : [
{
"row" : [
{
"type" : "text",
"content" : "Version",
"width" : 50,
"properties" : {
"size" : 24,
"color" : "#000000",
"underline" : true,
"strikethrough" : false
}
},
{
"type" : "text",
"content" : "2.0.0",
"width" : 50,
"properties" : {
"size" : 24,
"color" : "#000000",
"underline" : true,
"strikethrough" : false
}
}
]
},
{
"row" : [
{
"type" : "text",
"content" : "Source Code Available in GitHub",
"width" : 100,
"properties" : {
"size" : 24,
"color" : "#000000",
"underline" : true,
"strikethrough" : false
}
}
]
},
{
"row" : [
{
"type" : "image",
"type" : "image",
"source" : "https://avatars0.githubusercontent.com/u/28639189?s=400&u=bd9a720624781e17b9caaa1489345274c07566ac&v=4"
}
]
}
],
"properties" : {
"width" : 1,
"color" : "#000000"
}
}
]
}

This JSON defines that the PDF document will contain the defined contents. This it is required to be provided SimplyPDF which will generate the document.

As usual the source code is available in GitHub. Do let me know if this helps you in your projects. This is sufficiently helpful in the project in which I am working. Thanks all again.

--

--

Soumya Kanti Kar
wwdablu
Editor for

Android developer. Interested in working on open source Android libraries and applications.