Last active
July 23, 2021 21:22
Revisions
-
omkar-tenkale revised this gist
Jul 23, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ @SuppressLint("StaticFieldLeak") 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>() { -
omkar-tenkale renamed this gist
Jul 23, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
omkar-tenkale revised this gist
Jul 23, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); -
omkar-tenkale created this gist
Jul 23, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); }