# PDF export for Android sketch

**URL:** https://discourse.processing.org/t/pdf-export-for-android-sketch/7738
**Category:** Processing for Android
**Created:** [January 22, 2019, 12:58pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738 "2019-01-22T12:58:11Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Puran.Singh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/puran.singh/32/3367_2.png) [@Puran.Singh](https://discourse.processing.org/u/Puran.Singh)
#### Post date: [January 22, 2019, 12:58pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/1 "2019-01-22T12:58:11Z")

</div>

Hi everyone!  
I am making an app that does some sort of calculations and draws something with the result.  
Now I want to export a pdf of the same.  
Although my purpose is satisfied on a computer with pdf export library, but I’m not able to find any pdf library for android. Also, the pdf need not be the same thing as the screen, so screenshot would not work out.  
I’m new to processing, and any help will be appreciated 🙂

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [January 23, 2019, 3:23pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/2 "2019-01-23T15:23:22Z")

</div>

@Puran.Singh====  
With AS i have done that using the pdfDocument class; i cannot see any reason why it could not be used with P5, either taking a screen shot and making pdf from it or using the rootView where is PApplet.  
more details here: [https://developer.android.com/reference/android/graphics/pdf/PdfDocument](https://developer.android.com/reference/android/graphics/pdf/PdfDocument)  
ASAP i ll post some code for P5.

---

<div class="post-metadata">

### Author: ![Puran.Singh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/puran.singh/32/3367_2.png) [@Puran.Singh](https://discourse.processing.org/u/Puran.Singh)
#### Post date: [January 26, 2019, 3:31pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/3 "2019-01-26T15:31:19Z")

</div>

Thanks for your response, it will be of great help to me if you post any example sketch. I’m just a beginner in programming.  
Thanks! 😌

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [January 26, 2019, 3:36pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/4 "2019-01-26T15:36:30Z")

</div>

@Puran.Singh===

my (snippet code) is written with Android native && android studio; i can put it but if you are a beginner i dont think that you are able to use it with P5…So i have to adapt it myself : i ll do that as soon as possible…

---

<div class="post-metadata">

### Author: ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)
#### Post date: [February 3, 2019, 3:01pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/5 "2019-02-03T15:01:27Z")

</div>

@Puran.Singh===

As i told you i have addapted my code for AS to P5: it works; look at my comments especially for the resulting pdf.size. code below:

```auto

import android.os.Looper;
import android.graphics.BitmapFactory;
import android.os.Environment;
import java.awt.Color;
import android.widget.Toast;
import java.io.FileOutputStream;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.content.Context;
import android.graphics.pdf.PdfDocument;
import android.util.DisplayMetrics;
import android.view.WindowManager;

private Bitmap bitmap;
boolean click = false;
 String cheminComplet="";
 boolean bit= false;
 String imageName="";
 
void setup(){
  
  size(800,1000);
  background(255,0,0);
  Looper.prepare();
}

void draw(){
  ellipseMode(CENTER);
  ellipse(width/2,height/2,200,200);
  
  if(!bit && click){
   bitmap = createBitmap(imageName); 
   if(bitmap !=null){
   bit = true;
   createPdf();
   }
  }
 
}

private Bitmap createBitmap(String in){////could be better to create an handler for that
 
Bitmap b = null;
 b = BitmapFactory.decodeFile(cheminComplet+in);

 
return b;
}

    private void createPdf(){
    
      WindowManager wm = (WindowManager) this.getActivity().getSystemService(Context.WINDOW_SERVICE);
      
        DisplayMetrics displaymetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(displaymetrics);
        
        //UNCOMMENT IF YOU WANT THAT THE RESULT IS (resized) displayHeight && displayWidth:
        
        //float hight = displaymetrics.heightPixels ;
        //float width = displaymetrics.widthPixels ;
        //int convertHighet = (int) hight, convertWidth = (int) width;
        
        int convertHighet = (int) bitmap.getHeight(), convertWidth = (int) bitmap.getWidth();///if you have uncommented the 3 lines above coment this one
     
        PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
        PdfDocument.Page page = document.startPage(pageInfo);

        Canvas canvas = page.getCanvas();

        Paint paint = new Paint();
        canvas.drawPaint(paint);

        bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, convertHighet, true);//it is not scaled as i have used bitmap.getWidth(), getHeight()
        int RGB = android.graphics.Color.rgb(0, 0, 255); 
        paint.setColor(RGB);////as in this case the image size is == display this is useless
        canvas.drawBitmap(bitmap, 0, 0 , null);
        document.finishPage(page);

        // save the pdf
        String targetPdf = "/sdcard/pdfcreesansechelle.pdf";///path could be elsewhere!!!!!
        
        File filePath;
        filePath = new File(targetPdf);
        try {
            document.writeTo(new FileOutputStream(filePath));

         } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this.getActivity().getApplicationContext(), "quelque chose a foiré: " + e.toString(), Toast.LENGTH_LONG).show();
        }

        // close the document
        document.close();
        Toast.makeText(this.getActivity().getApplicationContext(), "Le pdf a été créé!!!", Toast.LENGTH_SHORT).show();

  ///now if you want you can add a method to read the pdf.....

    }
    
    public void mouseReleased(){
      
     if(!click){
  
   File externalDir = Environment.getExternalStorageDirectory();
    String path = externalDir.getAbsolutePath();
  cheminComplet = path+File.separator;
  imageName = "saisiedecran.png";
  saveFrame(path+File.separator+imageName);
  click=true;
  createBitmap(imageName);
  
  
  }
    }

```

---

<div class="post-metadata">

### Author: ![GWAK](https://avatars.discourse-cdn.com/v4/letter/g/13edae/32.png) [@GWAK](https://discourse.processing.org/u/GWAK)
#### Post date: [April 8, 2023, 7:42pm UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/6 "2023-04-08T19:42:37Z")

</div>

@akenaton

Thank you. you are a genius  
Thank you

---

<div class="post-metadata">

### Author: ![SantosCuccia](https://avatars.discourse-cdn.com/v4/letter/s/aeb1de/32.png) [@SantosCuccia](https://discourse.processing.org/u/SantosCuccia)
#### Post date: [July 3, 2023, 7:13am UTC](https://discourse.processing.org/t/pdf-export-for-android-sketch/7738/7 "2023-07-03T07:13:35Z")

</div>

I see, I learned. Thank you.
