Skip to content

Instantly share code, notes, and snippets.

@omkar-tenkale
Last active July 23, 2021 21:22

Revisions

  1. omkar-tenkale revised this gist Jul 23, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ImagesToPdf.java
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@


    @SuppressLint("StaticFieldLeak")
    private static void createPdf(Context context,ArrayList<File> data){
    public static void createPdf(Context context,ArrayList<File> data){
    File pdfFile = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + "TemperoryPDF_"+System.currentTimeMillis()+".pdf");
    Toast.makeText(context, "Creating PDF,Please wait..", Toast.LENGTH_SHORT).show();
    new AsyncTask<Void, Void, Void>() {
  2. omkar-tenkale renamed this gist Jul 23, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. omkar-tenkale revised this gist Jul 23, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions PDFUtil.java
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,7 @@
    // > - No need to declare and grant storage permission :)


    @SuppressLint("StaticFieldLeak")
    private static void createPdf(Context context,ArrayList<File> data){
    File pdfFile = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + "TemperoryPDF_"+System.currentTimeMillis()+".pdf");
    Toast.makeText(context, "Creating PDF,Please wait..", Toast.LENGTH_SHORT).show();
  4. omkar-tenkale created this gist Jul 23, 2021.
    51 changes: 51 additions & 0 deletions PDFUtil.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    // Just pass context and list of file
    // It will create a PDF file in cache dir
    // Add your own logic to share/open PDF
    // Works fast and reliable method

    // > - No library needed (Uses android internal android.graphics.pdf.PdfDocument classes)
    // > - Works in background, Doesn''t freeze UI
    // > - No need to declare and grant storage permission :)


    private static void createPdf(Context context,ArrayList<File> data){
    File pdfFile = new File(context.getExternalCacheDir().getAbsolutePath() + File.separator + "TemperoryPDF_"+System.currentTimeMillis()+".pdf");
    Toast.makeText(context, "Creating PDF,Please wait..", Toast.LENGTH_SHORT).show();
    new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... voids) {
    PdfDocument document = new PdfDocument();
    try {
    for(File item:data) {
    Bitmap bitmap = BitmapFactory.decodeFile(item.getAbsolutePath());
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
    PdfDocument.Page page = document.startPage(pageInfo);
    Canvas canvas = page.getCanvas();
    Paint paint = new Paint();
    paint.setColor(Color.parseColor("#ffffff"));
    canvas.drawPaint(paint);
    bitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
    canvas.drawBitmap(bitmap, 0, 0, null);
    document.finishPage(page);
    }
    document.writeTo(new FileOutputStream(pdfFile));
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    document.close();
    }
    return null;
    }

    @Override
    protected void onPostExecute(Void unused) {
    super.onPostExecute(unused);
    if(pdfFile.exists() && pdfFile.length()>0) {
    FileUtil.openFile(context, pdfFile.getAbsolutePath()); // See: https://gist.github.com/omkar-tenkale/34d3aa1966653e6949d1ddaee1ba3355
    }else {
    Toast.makeText(context, "Something went wrong creating the PDF :(", Toast.LENGTH_SHORT).show();
    }
    }
    }.execute();

    }