title: generate PDF from Android using PrintedPdfDocument and View.draw
body: <p>I have this layout, and I would like to print everything on the screen to PDF.
  including all the content in the scroll view. I will also try to do this using IText later, but for now I want to see if I can do it using PrintedPdfDocument and View.draw. What I have generates the PDF but only part of the layout is visible on the PDF.</p>
  
  <p>The full project is on: 
  <strong><a href="https://github.com/ammar5001/PrintPDF2" rel="noreferrer">https://github.com/ammar5001/PrintPDF2</a></strong></p>
  
  <p>Here is the layout:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
  android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
  android:id="@+id/relativeLayout"&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Title"
      android:id="@+id/textView"
      android:textSize="24dp"
      android:textStyle="bold"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textView2"
      android:text="Auther Name\nTitle\nContacts"
      android:textSize="16dp"
      android:layout_below="@+id/textView"
      android:layout_alignParentStart="true" /&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Some Company\nStreet Address\nCity, State\nzip code"
      android:id="@+id/textView3"
      android:layout_below="@+id/textView"
      android:layout_alignParentEnd="true"
      android:textSize="16dp"
      android:layout_marginBottom="10dp" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Print PDF"
      android:id="@+id/button1"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true"
      android:onClick="printPDF" /&gt;
  
  &lt;ScrollView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/scrollView"
      android:layout_below="@id/textView3"
      android:layout_above="@id/button1"
      android:layout_marginTop="10dp"&gt;
  
      &lt;LinearLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="For some applications, such as drawing apps, page layout apps and other apps that focus on graphic output, creating beautiful printed pages is a key feature. In this case, it is not enough to print an image or an HTML document. The print output for these types of applications requires precise control of everything that goes into a page, including fonts, text flow, page breaks, headers, footers, and graphic elements."
              android:id="@+id/textView4"
              android:layout_marginBottom="10dp" /&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Creating print output that is completely customized for your application requires more programming investment than the previously discussed approaches. You must build components that communicate with the print framework, adjust to printer settings, draw page elements and manage printing on multiple pages.  This lesson shows you how you connect with the print manager, create a print adapter and build content for printing."
              android:id="@+id/textView5"
              android:layout_marginBottom="10dp"/&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="When your application manages the printing process directly, the first step after receiving a print request from your user is to connect to the Android print framework and obtain an instance of the PrintManager class. This class allows you to initialize a print job and begin the printing lifecycle. The following code example shows how to get the print manager and start the printing process"
              android:id="@+id/textView6"
              android:layout_marginBottom="10dp"/&gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="The example code above demonstrates how to name a print job and set an instance of the PrintDocumentAdapter class which handles the steps of the printing lifecycle. The implementation of the print adapter class is discussed in the next section."
              android:id="@+id/textView7"
              android:layout_marginBottom="10dp"/&gt;
      &lt;/LinearLayout&gt;
  &lt;/ScrollView&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and here is the method I am using:</p>
  
  <pre><code>  public void printPDF(View view){
      if(isExternalStorageWritable()) {
          String filename = getFileName();
          File file = new File(getAlbumStorageDir("PDF"), filename);
          try {
              FileOutputStream outputStream = new FileOutputStream(file);
              createPDF(outputStream);
          } catch (IOException e) {
              e.printStackTrace();
              Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
          }
      }
  }
  
  private void createPDF(FileOutputStream outputStream) throws IOException {
      PrintedPdfDocument document = new PrintedPdfDocument(this,
              getPrintAttributes());
  
      // start a page
      PdfDocument.Page page = document.startPage(1);
  
      // draw something on the page
      View content = getContentView();
      content.draw(page.getCanvas());
  
      // finish the page
      document.finishPage(page);
      //. . .
      // add more pages
      //. . .
      // write the document content
      document.writeTo(outputStream);
  
      //close the document
      document.close();
  }
  
  
  private View getContentView() {
      return findViewById(R.id.relativeLayout);
  }
  </code></pre>