class PDFUtil constructor(val context: Context) { /**Dimension For A4 Size Paper (1 inch = 72 points)**/ val PDF_PAGE_WIDTH = 595 //8.26 Inch val PDF_PAGE_HEIGHT = 842 //11.69 Inch /* Font Resource */ val fontStrawberry = ResourcesCompat.getFont(context, R.font.goryeongstrawberry) // Strawberry font // A4 Size Page Info val A4 = a4Paper() // Paint object to make a Text val title: Paint = Paint() // Title paint, Goryoengstrawberry font // A4 Size Paper Initializer private fun a4Paper() : PdfDocument.PageInfo { // Page Information : Page's Width, Height, and PageNumber val myPageInfo: PdfDocument.PageInfo = PdfDocument.PageInfo.Builder(PDF_PAGE_WIDTH, PDF_PAGE_HEIGHT, 1).create() return myPageInfo } // Draw Title fun titleText(text : String, canvas: Canvas) { title.setTypeface(fontStrawberry) title.color = ContextCompat.getColor(context, R.color.orange_80) title.textSize = 16f title.textAlign = Paint.Align.CENTER // Draw Text canvas.drawText(text, (canvas.width / 2).toFloat(), (canvas.height / 2).toFloat(), title) } @Throws(IOException::class) fun savePdfFileExternalStorage(filename: String, document: PdfDocument, context : Context) { try { // First, creating External Storage Directory, and a file name, // Second, write our PDF file to that location. // Third, close file val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) val file = File(dir, filename) val fos = FileOutputStream(file) document.writeTo(fos) fos.close() // on below line we are displaying a toast message as PDF file generated.. Toast.makeText(context, "PDF 파일이 다운로드 폴더에 저장되었습니다.", Toast.LENGTH_SHORT).show() } catch (e: Exception) { // below line is used // to handle error e.printStackTrace() // on below line we are displaying a toast message as fail to generate PDF Toast.makeText(context, "PDF파일 작성에 실패했습니다.", Toast.LENGTH_SHORT).show() } } }