Generate Pdf of the page with listview android studio

Asked

Viewed 5 times

-1

I am facing difficulties to adjust an application page layout in android studio, generate image and save in pdf. I have a layout with several Textview and a Listview with scrolling, I can transform the layout into bitmap and save to pdf, but as the listview has many items, the image comes out only with what is appearing on the screen and not with all the content.

I believe it is something of layout configuration, however I am beginner and I’m 3 days banging head with it. Any help would be welcome.

Follows the XML layout:

<?xml version="1.0" encoding="utf-8"?>

<androidx.drawerlayout.widget.Drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/Drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background" tools:context=". Debits">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/pdf"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView29"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:text="@string/textoRelatorio"
        android:textSize="19sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txtNomeProf2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Maria Antonia da Silva"
        android:text=""
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textSize="14sp"
        android:textStyle="bold|italic"
        android:textColor="#7E7E7E"
        app:layout_constraintEnd_toEndOf="@+id/textView29"
        app:layout_constraintStart_toStartOf="@+id/textView29"
        app:layout_constraintTop_toBottomOf="@+id/textView29" />

    <TextView
        android:id="@+id/txtCargoProf2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Artista"
        android:text=""
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="#7E7E7E"
        android:textSize="12sp"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="@+id/txtNomeProf2"
        app:layout_constraintStart_toStartOf="@+id/txtNomeProf2"
        app:layout_constraintTop_toBottomOf="@+id/txtNomeProf2" />

    <TextView
        android:id="@+id/txtDocProf2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="00000-00"
        android:text=""
        android:textAlignment="center"
        android:gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.AppCompat.Display1"
        android:textColor="#7E7E7E"
        android:textSize="12sp"
        android:textStyle="italic"
        app:layout_constraintEnd_toEndOf="@+id/txtCargoProf2"
        app:layout_constraintStart_toStartOf="@+id/txtCargoProf2"
        app:layout_constraintTop_toBottomOf="@+id/txtCargoProf2" />

    <View
        android:id="@+id/divider3"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="8dp"
        android:background="@color/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/txtDocProf2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/lstDebitos" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/lstDebitos"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="120dp"
        android:scrollbars="vertical"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@id/divider15"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.recyclerview.widget.RecyclerView>

    <View
        android:id="@+id/divider15"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/darkgray"
        app:layout_constraintBottom_toTopOf="@+id/txtTotDebitos"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtTotDebitos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="120dp"
        android:layout_marginRight="120dp"
        android:layout_marginBottom="5dp"
        android:text="R$0.00"
        android:textColor="@color/corDinheiro"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:gravity="center"
        android:text="Total:"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="@id/txtTotDebitos"
        app:layout_constraintEnd_toStartOf="@+id/txtTotDebitos"
        app:layout_constraintTop_toTopOf="@+id/txtTotDebitos" />


</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    app:headerLayout="@layout/header"
    app:itemIconTint="@color/darkgray"
    app:itemTextColor="@color/colorPrimary"
    app:menu="@menu/drawermenu" />

</androidx.drawerlayout.widget.Drawerlayout>

Class responsible for generating the bitmap and writing the pdf (this working):

//Gerar arquivo PDF IMAGEM
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public Uri gerarPDF(){

    bitmap = gerarBitmap();
    PdfDocument documentoPDF = new PdfDocument();
    PdfDocument.PageInfo detalhesPagina = new PdfDocument.PageInfo.Builder(bitmap.getWidth(),bitmap.getHeight(),1).create();

    PdfDocument.Page novaPagina = documentoPDF.startPage(detalhesPagina);

    Canvas canvas = novaPagina.getCanvas();


    Rect rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());
    canvas.drawBitmap(bitmap,null,rect,null);

    documentoPDF.finishPage(novaPagina);

    String nomeArquivo = String.format("Detalhes_%s",data);

    File pasta = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/ReabAPP/tets");
    if(!pasta.exists()){
        pasta.mkdirs();
    }
    filePath = new File(pasta.getAbsolutePath() + File.separator + nomeArquivo + ".pdf");

    try{
        documentoPDF.writeTo(new FileOutputStream(filePath));
        Toast.makeText(this,"PDF criado",Toast.LENGTH_SHORT).show();
    }catch (IOException e){
        e.printStackTrace();
        Toast.makeText(this,"Erro ao gerar PDF: " + e.toString(),Toast.LENGTH_LONG).show();
    }

    documentoPDF.close();
    Uri uriPdf = FileProvider.getUriForFile(getBaseContext(), getBaseContext().getApplicationContext().getPackageName() + ".fileprovider", filePath);

    return uriPdf;
}
public Bitmap gerarBitmap(){
    ConstraintLayout layout = findViewById(R.id.pdf);
    //.setBackgroundColor(Color.LTGRAY);

    bitmap = bitmap.createBitmap(layout.getWidth(),layout.getHeight(), Bitmap.Config.ARGB_8888);
    
    Canvas canvas = new Canvas(bitmap);
    layout.draw(canvas);
    return bitmap;
}
public void compartilharImagem(){

    bitmap = gerarBitmap();

    String path = MediaStore.Images.Media.insertImage(getContentResolver(),bitmap,"Detalhes",null);

    Uri uriImagem = Uri.parse(path);

    Intent intent = new Intent(Intent.ACTION_SEND)
            .setType("image/jpeg")
            .putExtra(Intent.EXTRA_STREAM,uriImagem);
    startActivityForResult(Intent.createChooser(intent,"Compartilhar Imagem"),1011);
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void compartilharPDF(){
    uriPdf = gerarPDF();
    if(uriPdf != null){
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("application/pdf");
        intent.putExtra(Intent.EXTRA_STREAM, uriPdf);

        startActivityForResult(Intent.createChooser(intent,"Compartilhar os Detalhes"),1011);
    }
}
///////////////////////

If anyone can help me I’d be grateful.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.