0

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.

view

I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.

Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.

Nihal Singh
  • 61
  • 2
  • 10

2 Answers2

1

try this:

create a WebView and copy the text of your edittext in it:

webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");

and convert webview to pdf by below function:

private void createWebPrintJob(WebView webView) {

PrintManager printManager = (PrintManager) this
        .getSystemService(Context.PRINT_SERVICE);

PrintDocumentAdapter printAdapter =
        webView.createPrintDocumentAdapter();

String jobName = getString(R.string.app_name) + " Print Test";

if (printManager != null) {
    printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());
}
}

after that user can select page size for example A4

Mehrdad
  • 1,477
  • 15
  • 17
  • will that work for `TextView` too ? if yes, then do I have to `webview.loadData(yourTextView.gettext().tostring, "text/html", "UTF-8");` for each, and every TextView in my layout ? – Nihal Singh May 02 '20 at 06:43
  • yes. but you should put the text of all textview in a single string variable – Mehrdad May 02 '20 at 07:14
  • Umm how can I do that ? Each textview contains different string, as I have already mentioned in my question, I am fetching data from my database then I am displaying that data in textviews – Nihal Singh May 02 '20 at 07:31
0

There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.

The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.

- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).

check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2

and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591

- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.

They may be other solutions, but these are the popular ones.

Let Me know if it works for you and also if you're stuck. Thank you!

choubari
  • 41
  • 7