Created
April 11, 2024 04:06
-
-
Save chiragthummar/e10cf77757d613df2e82391198eeabbd to your computer and use it in GitHub Desktop.
Create pdf from your text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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