title: Generate and print a PDF with specific size in Android
body: <p>I'm working in an Android application, and I want to generate and print a PDF. But I'm having some trouble. I need to generate the <code>PDF</code> with 80mm of <code>width</code>, and the <code>height</code> may vary.</p>
  
  <p>I'm trying this:</p>
  
  <pre><code>public class PDFGenerator implements Runnable {
      private Context ctx;
      private View view;
      private Intent mShareIntent;
      private OutputStream os;
  
      public PDFGenerator(Context ctx, View view) {
          this.ctx = ctx;
          this.view = view;
          makeAndSharePDF();
      }
  
      public void makeAndSharePDF() {
          new Thread(this).start();
      }
  
      @TargetApi(Build.VERSION_CODES.KITKAT)
      public void run() {
          PrintAttributes printAttrs = new PrintAttributes.Builder().
                  setColorMode(PrintAttributes.COLOR_MODE_MONOCHROME).
                  setMediaSize(PrintAttributes.MediaSize.ISO_C7).
                  setResolution(new PrintAttributes.Resolution("zooey", ctx.PRINT_SERVICE, 500, 500)).
                  setMinMargins(PrintAttributes.Margins.NO_MARGINS).
                  build();
  
          PdfDocument document = new PrintedPdfDocument(ctx, printAttrs);
          PdfDocument.PageInfo pageInfo = new  PdfDocument.PageInfo.Builder(view.getWidth(), view.getHeight(), 1).create();
          PdfDocument.Page page = document.startPage(pageInfo);
          view.draw(page.getCanvas());
          document.finishPage(page);
  
          try {
              File pdfDirPath = new File(ctx.getFilesDir(), "pdfs");
              pdfDirPath.mkdirs();
              File file = new File(pdfDirPath, "danfe.pdf");
              Uri contentUri = FileProvider.getUriForFile(ctx, "br.com.rdscodes.nfcelular", file);
              os = new FileOutputStream(file);
              document.writeTo(os);
              document.close();
              os.close();
              shareDocument(contentUri);
          } catch (IOException e) {
              throw new RuntimeException("Error generating file", e);
          }
      }
  
      private void shareDocument(Uri uri) {
          mShareIntent = new Intent();
          mShareIntent.setAction(Intent.ACTION_SEND);
          mShareIntent.setType("application/pdf");
          mShareIntent.putExtra(Intent.EXTRA_SUBJECT, "NFC-e");
          mShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
          ctx.startActivity(mShareIntent);
      }
  }
  </code></pre>
  
  <p>This code generates the <code>PDF</code>, but with <code>letter size</code>.</p>
  
  <p>I saw in <a href="https://developer.android.com/reference/android/print/PrintAttributes.MediaSize.html" rel="noreferrer">Android Developers media size documentation</a> that the "ISO_C7" media size is the size the i almost want, but i can change all the "printAttrs" and nothing changes on the final result of the PDF.</p>
  
  <p>Are there better ways to generate and print a <code>PDF</code>?  How can I generate my <code>PDF</code> with 80mm of <code>width</code>?</p>
  
  <p>This part of the app was made by an old employee.</p>
  
  <p>Thanks.</p>