PDF export for Android sketch

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 :slight_smile:

@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
ASAP i ll post some code for P5.

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! :relieved:

@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…

@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:


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);
  
  
  }
    }
1 Like

@akenaton

Thank you. you are a genius
Thank you

I see, I learned. Thank you.