Android Generate PDF From View Convert Activity XML Layout ScrollView

Android Generate PDF From View Tutorial is today’s theme.

You can create the pdf file from any layout of your application programmatically.

In this example, we will generate our pdf file from xml layouts like linearlayout and scrollview.

I have not used any third party library in this tutorial.

At the end of the tutorial, you will be able to generate the PDF document from the current activity.

We will also save the generated pdf file in the internal storage.

Our Generated PDF

Below video showcases our PDF file.

Step 1. Changes in the gradle file

We are going to use PdfDocument class in this example. Now this PdfDocument class supports sdk version greater than 18.

Hence, we have to set the minSdkVersion as greater than 18 in the build.gradle(Module: app) file.

We require two permissions (Read and Write) in this example.

Add above two permissions in the AndroidManifest.xml file.

I have set the targetSdkVersion as 22 here. If you are targeting sdk version greater than 22 then you need to request for runtime permissions.

You can get reference for runtime permissions from this android marshmallow permissions guide.

So the final source code for build.gradle(Module: app) file is like below

Step 2. activity_main.xml and

Now we will create a pdf file from linearlayout.

This linearlayout contains one image, one textview and two buttons.

The height of the linearlayout is not so long that we need to use scrollview here. However, we will also learn how to make a pdf file that covers the whole scrollview later.

Copy the following code in activity_main.xml file

Now add below source code in file

Step 3. Information about Main Activity code

Now let us learn the code of the Main Activity step by step.

Consider below code

two buttons and linearlayout is found here with help of their ids.

llPdf is the linearlayout using which we will create the PDF file.

Let us understand following code

click event of btnScroll will open a new activity. We will create pdf file with scrollview in this activity.

Second is the button click where we will generate the pdf document from linearlayout.

First of all, we will create the bitmap from the height and width of the linearlayout using loadBitmapFromView() method.

Here is the code for the loadBitmapFromView() method.

We need to pass height and width of the linearlayout in the parameter.

This method will create a bitmap using canvas class and will return that bitmap.

After this, compiler will call the createPdf() method, which will finally generate the PDF file.

Code for createPdf() method is as per following lines

First the compiler will set the height and width of the pdf by using WindowManager and DisplayMetrics classes.

PdfDocument class will create a new pdf file object.

Then the compiler will fill this newly generated pdf file using Canvas and Paint classes.

Read the below line

Above line will define a name of the pdf file and a path where this pdf document will be saved.

After generating the pdf file successfully, compiler will open this file using openGeneratedPDF() method.

Learning openGeneratedPDF()

Following is the source code for this method

We need to give a path to the pdf file in the below line

After this, compiler will open a pdf document using Intent as per shown in the source code.

Step 4. Making PDF from Scrollview

Create a new activity named Scroll Activity.

Add below code in activity_scroll.xml

I have taken one button, one imageview and three textview. All these three widgets are the childd of the linearlayout.

Now scrollview is the parent of the linearlayout. We will generate the pdf file using this linearlayout which is the child of the scrollview.

Further, write below codes in the file

I have used the similar method to create the pdf document and to open it.

So you can get reference from step 3 about this thing.

Download Source Code For Android generate pdf from view

[sociallocker]Download Source Code For PDF From View Example[/sociallocker]

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

12 Responses

  1. milad says:

    your website is incredible and thanks for your tutorials. .I am developer.I would be glad to visit my instagram page(miladrsh96)

  2. savithri says:

    I got a below type of error … java.lang.IllegalArgumentException: width and height must be > 0
    how I solve it ??? any idea

  3. Youssef says:

    I followed this tutorial through and I got it to generate a pdf file, only problem is that the pdf-file is all black…

    • Iqbal says:

      //add this to your intent

  4. Youssef says:

    It all works except I only get a black pdf file

  5. Dushyant Sahu says:

    What can i do for multiple page?

    • Hardik Parsania says:

      Put images in scroll view.

      • Dushyant Sahu says:

        I have put scroll view inside list view item and the following code
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        gives only single page.
        i want to generate pdf depends on list view item. like multiple pages number.

  6. kavya says:

    I am getting a blank black pdf if i use this code for creating the pdf.What is the possible solution to rectify it?Please hep me.

  7. Aqila says:

    how to make the table from database to pdf?

Leave a Reply

Your email address will not be published. Required fields are marked *