Skip to content

Instantly share code, notes, and snippets.

@chiragthummar
Created April 11, 2024 04:06
Show Gist options
  • Save chiragthummar/e10cf77757d613df2e82391198eeabbd to your computer and use it in GitHub Desktop.
Save chiragthummar/e10cf77757d613df2e82391198eeabbd to your computer and use it in GitHub Desktop.
Create pdf from your text
private fun GeneratePDF() {
try {
val path =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.toString() + "/MyPDF"
val file = File(path, "my_file.pdf")
val pdfWriter = PdfWriter(file)
val pdfDocument = PdfDocument(pdfWriter)
val document = Document(pdfDocument)
val font = PdfFontFactory.createFont(
"assets/regular.ttf",
PdfEncodings.IDENTITY_H
)
document.setFont(font)
pdfDocument.defaultPageSize = PageSize.A4
val secondLine = Paragraph(
"${
SimpleDateFormat(
"dd-MM-yyyy hh:mm a",
Locale.getDefault()
).format(Date())
} \n Created By : My"
).setBold().setFontSize(12f).setItalic()
document.add(secondLine)
val line = SolidLine(1f)
val linebreak = LineSeparator(line)
document.add(linebreak)
document.close()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment