Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create PDF of RecyclerView in FULL length

I want to create a PDF of "full page" of the activity. The view contains a RecyclerView with many items.

I can take a full dimensions of my Recyclerview but the file is drawed only of the current view. This is my code:

    public void tela (){ // create a new document
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)

    {
        PdfDocument document = new PdfDocument();

        getScreenshotFromRecyclerView(mRecyclerView);
        content = mRecyclerView;
        content.setBackgroundColor(Color.parseColor("#303030"));


        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(wil,
                height, 1).create();

    // create a new page from the PageInfo
        PdfDocument.Page page = document.startPage(pageInfo);

    // repaint the user's text into the page
        content.draw(page.getCanvas());

    // do final processing of the page
        document.finishPage(page);

    // saving pdf document to sdcard
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy - HH-mm-ss",Locale.getDefault());
        String pdfName = "Revisões_"
                + sdf.format(Calendar.getInstance().getTime()) + ".pdf";

    // all created files will be saved at path /sdcard/PDFDemo_AndroidSRC/
        File outputFile = new File(Environment.getExternalStorageDirectory().getPath(), pdfName);

        try {
            outputFile.createNewFile();
            OutputStream out = new FileOutputStream(outputFile);
            document.writeTo(out);
            document.close();
            out.close();
            Toast.makeText(this,"PDF gerado com sucesso",Toast.LENGTH_SHORT).show();
            Log.i("Gerou", "pdf");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// getting the limits

    public void getScreenshotFromRecyclerView(RecyclerView view) {
    RecyclerView.Adapter adapter = view.getAdapter();
    if (adapter != null) {
        int size = adapter.getItemCount();

        for (int i = 0; i < size; i++) {
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
            adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
            height += holder.itemView.getMeasuredHeight();
        }
        wil=view.getMeasuredWidth();


    }
}

The result is this:
enter image description here

Can I create the pdf with the all values of my Recyclerview?

Thanks.

like image 293
Rafael Avatar asked Jul 09 '16 06:07

Rafael


1 Answers

I implemented a helper class to handle image saving to PDF. In the method saveImageToPDF() I pass:

  • a TabLayout, which is above my recyclerView
  • the Bitmap of the recyclerView
  • the Context

to get the recyclerView Bitmap I used this Take a screenshot of RecyclerView in FULL length

public class PDFHelper {

    private File mFolder;
    private File mFile;
    private Context mContext;

    public PDFHelper(File folder, Context context) {

        this.mContext = context;
        this.mFolder = folder;

        if(!mFolder.exists())
            mFolder.mkdirs();
    }

    public void saveImageToPDF(View title, Bitmap bitmap, String filename) {

        mFile = new File(mFolder, filename + ".pdf");
        if (!mFile.exists()) {
            int height = title.getHeight() + bitmap.getHeight();
            PdfDocument document = new PdfDocument();
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), height, 1).create();
            PdfDocument.Page page = document.startPage(pageInfo);
            Canvas canvas = page.getCanvas();
            title.draw(canvas);

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

            document.finishPage(page);

            try {
                mFile.createNewFile();
                OutputStream out = new FileOutputStream(mFile);
                document.writeTo(out);
                document.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
like image 96
Ecaterina Galeru Avatar answered Sep 18 '22 23:09

Ecaterina Galeru