All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Android Generate PDF From View

Last Updated : Mar 11, 2024

Android Generate PDF From View

In this article we will show you the solution of android generate pdf from view, third-party libraries are frequently used by developers to generate PDFs in Android Studio.

Can I create professional PDFs from Android Studio without using any of these libraries?

That is what we will discover today. You can achieve the desired result by using XML and the built-in PDFDocument class.

Student report cards are displayed in the PDF in our scenario.

As a result, the data class must include the student's name, subject grades, and total grade. We will use two data classes: one for all specifics and another for subject marks.

Then populate their values in your activity or fragment to create objects of these data classes. Calculate the total marks by subtracting the subject marks.

If you don't have access to the Room database or the internet, you can get the data you need from the internet.

It will require two layout files for our PDF: one for the overall layout of the PDF and one for the item row layout of the recycler view for the subject marks.

An image containing the layout view must first be created using the XML file as the layout for our PDF.

The first step is to obtain the layout instance, as shown below.

val view = inflater.inflate(R.layout.layout pdf page, null) val inflater = LayoutInflater.from(context)

As soon as our layout instance is ready, we can use the data class objects that we created earlier to assign values to our fields such as student name, marks, and so on.

using the findViewById method ().

Add the bitmap to your PDF.

This bitmap object can now be inserted into our PDF document as follows:

  • If the A4 size portrait is needed, create a PDFDocument object with a page size of 595 X 842 PostScript points (1/72nd of an inch).
  • pdfDocument = PdfDocument val ()
  • The value of pageInfo is PdfDocument.
  • PageInfo.Builder(595, 842, 1). (595, 842, 1).
  • create()

To begin drawing on our page, use startPage(), and when finished, use finshPage(). The bitmap will be created on the page using drawBitmap().

  • pdfDocument.startPage = val page (pageInfo)
  • page.canvas.drawBitmap(bitmap, 0F, 0F, null) (bitmap, 0F, 0F, null)
  • pdfDocument.finishPage(page)
  • FileOutputStream allows us to place our document in any file path we create with the File class.
  • File(context.getExternalFilesDir(null), "bitmapPdf.pdf") val filePath
  • pdfDocument.writeTo(FileOutputStream(filePath))
  • After successfully creating the PDF, call close().
  • pdfDocument.close()

That's all there is to it.

Step By Step Guide On Android Generate PDF From View :-

PDFVerticalView verticalView = new PDFVerticalView(getApplicationContext());
PDFTextView pdfTextView1 = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P)
                                                         .setText("TextView1");
verticalView.addView(pdfTextView1)
PDFTextView pdfTextView2 = new PDFTextView(getApplicationContext(), PDFTextView.PDF_TEXT_SIZE.P)
                                                         .setText("TextView2");
verticalView.addView(pdfTextView2)
LinearLayout layout = verticalView.getView();
  1. Any dynamic data destined for the PDF should be stored in a data class.
  2. Our next step will be to create the XML files that define the layout of the PDF. Create a bitmap image from the layout.
  3. Include the bitmap in your PDF.
  4. We can now insert this bitmap object into our PDF document.
  5. In the code, we create a pdf as a vertical view, and we include a text view for programme execution.

Conclusion :-

Student report cards are displayed in the PDF in our scenario. As a result, the data class must include the student's name, subject grades, and total grade.

We will use two data classes: one for all specifics and another for subject marks.

I hope this article on android generate pdf from view helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪