Android create and print pdf from layout view
Asked Answered
M

4

8

I am trying to create PDF file from xml layout view. I have a listview in that layout, adding items and setting height based on child. PDF is creating but not filling the whole page. What I have tried is,

 PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);


        // draw something on the page
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.pdf_layout, null);

        content.measure(2250, 1400);
        content.layout(0,0, 2250, 1400);

        tvName = (TextView)content.findViewById(R.id.tvName);
        tvDate = (TextView)content.findViewById(R.id.tvDate);
        tvAge = (TextView)content.findViewById(R.id.tvAge);
        tvGender = (TextView)content.findViewById(R.id.tvGender);
        tvPhone = (TextView)content.findViewById(R.id.tvPhone);
        lvList = (ListView)content.findViewById(R.id.lvList);
        lvList.setAdapter(adapter);
        Utils.setListViewHeight(lvList, CreatePDFDemo.this);

        tvName.setText(name);
        tvAge.setText(age + "Y");
        tvGender.setText(gender);
        tvPhone.setText(phone);

        content.draw(page.getCanvas());

        // finish the page
        document.finishPage(page);
        // add more pages
        // write the document content
        try {
            document.writeTo(output);
        } catch (IOException e) {
            e.printStackTrace();
        }

This its output is like this image,

enter image description here

How can I write layout view covering full width of pdf page?

Machree answered 29/4, 2017 at 19:25 Comment(0)
D
6

Change to this,

        int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);

        content.measure(measureWidth, measuredHeight);
        content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

This will get page full height and width.

Dody answered 29/4, 2017 at 20:43 Comment(0)
P
1

Use [PrintContent] (https://developer.android.com/reference/android/support/v4/print/PrintHelper.html)!

 // Get the print manager.
PrintHelper printHelper = new PrintHelper(this);

// Set the desired scale mode.
printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);

// Get the bitmap for the ImageView's drawable.
Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();

// Print the bitmap.
printHelper.printBitmap("Print Bitmap", bitmap);
Paraesthesia answered 3/1, 2018 at 9:52 Comment(1)
it only printing one page and how to solve this problem if layout is more than one page length?Itemize
F
1

I made a library to achieve this objective (Getting PDF from layout view).

The main code snippet is with the proper documentation -

 PdfGenerator.getBuilder()
                        .setContext(context)
                        .fromLayoutXMLSource()
                        .fromLayoutXML(R.layout.layout_print,R.layout.layout_print)
            /* "fromLayoutXML()" takes array of layout resources.
             * You can also invoke "fromLayoutXMLList()" method here which takes list of layout resources instead of array. */
                        .setDefaultPageSize(PdfGenerator.PageSize.A4)
            /* It takes default page size like A4,A5. You can also set custom page size in pixel
             * by calling ".setCustomPageSize(int widthInPX, int heightInPX)" here. */
                        .setFileName("Test-PDF")
            /* It is file name */
                        .setFolderName("FolderA/FolderB/FolderC")
            /* It is folder name. If you set the folder name like this pattern (FolderA/FolderB/FolderC), then
             * FolderA creates first.Then FolderB inside FolderB and also FolderC inside the FolderB and finally
             * the pdf file named "Test-PDF.pdf" will be store inside the FolderB. */
                        .openPDFafterGeneration(true)
            /* It true then the generated pdf will be shown after generated. */
                        .build(new PdfGeneratorListener() {
                            @Override
                            public void onFailure(FailureResponse failureResponse) {
                                super.onFailure(failureResponse);
                /* If pdf is not generated by an error then you will findout the reason behind it
                 * from this FailureResponse. */
                            }

                            @Override
                            public void showLog(String log) {
                                super.showLog(log);
                /*It shows logs of events inside the pdf generation process*/ 
                            }

                            @Override
                            public void onSuccess(SuccessResponse response) {
                                super.onSuccess(response);
                /* If PDF is generated successfully then you will find SuccessResponse 
                 * which holds the PdfDocument,File and path (where generated pdf is stored)*/
                
                            }
                        });
Florri answered 31/8, 2020 at 17:37 Comment(0)
T
0

Try to convert your layout into image then set that image to PDF. read this, maybe you will get some idea. Convert view to PDF

Testimony answered 30/4, 2017 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.