Search code examples
androidpdfitext

Unable to create PDF in Android 11(R)


Greetings to community

I am generating PDF reports using ITEXT library.

'com.itextpdf:itextpdf:5.0.6'

I am doing this like that

 Document document = new Document();
   fileName = FileName + Common.getCurrentDateTime() + ".pdf";
   
    fileName1 = FileName + Common.getCurrentDateTime() + ".pdf";
    String dest = context.getExternalFilesDir(null) + "/RTS";

    File dir = new File(dest);
    if (!dir.exists())
        dir.mkdirs();


    try {
        File file = new File(dest, fileName);
        file.createNewFile();
        FileOutputStream fOut = new FileOutputStream(file, false);
        //FileOutputStream fOut = new FileOutputStream(file);
        //PdfWriter.getInstance(document, new FileOutputStream(dest));
        PdfWriter.getInstance(document, fOut);
        //  PdfWriter.getInstance(document, fOut);
    } catch (DocumentException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());
        //   dialog.dismiss();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.v("PdfError", e.toString());
        //  dialog.dismiss();


    } catch (IOException e) {
        e.printStackTrace();
    }

Please note that this solution is working fine till Android (10)Q. I have gone through many solution and spend almost a week with this problem. I am testing Android (11)R scenario on Emulator.

Now PDF has created thanks to people who comment on this question

 document.close();
    //   File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
    File file = new File(dest+"/"+fileName1);
    // File file = new File("/" +"Internal storage"+ "/" +"RTS" +"/" + fileName);
    if (!file.exists()) {
        file.mkdir();
    }
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    //intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setDataAndType(Uri.fromFile(file), "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    ((Activity) context).startActivity(intent);
    listsOfListReportModelList.clear();

Now unable to open file like this. Saying to make name shorter.


Solution

  • Solution to create and display PDF in Android 11

    Create @xml/provide_paths.xml in resource directory

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
        <external-path
            name="external_files"
            path="." />
        <root-path
            name="external_files"
            path="/storage/" />
    </paths>
    

    Add this is Manifest.xml application tag

         <application
    <--other properties-->
                android:requestLegacyExternalStorage="true"
                android:usesCleartextTraffic="true">
    
         <provider
                android:name="androidx.core.content.FileProvider"
                android:authorities="${applicationId}.provider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths" />
            </provider>
    

    Now create and display PDF

        String fileName1 = "";
    Document document = new Document();
        // Location to save
        fileName1 = "TEST" + ".pdf";
        String dest = context.getExternalFilesDir(null) + "/";
    
        File dir = new File(dest);
        if (!dir.exists())
            dir.mkdirs();
    
    
        try {
            File file = new File(dest, fileName);
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file, false);
            PdfWriter.getInstance(document, fOut);
        } catch (DocumentException e) {
            e.printStackTrace();
            Log.v("PdfError", e.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.v("PdfError", e.toString());
    
    
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        // Open to write
            document.open();
            document.add(new Paragraph(""));
            document.add(new Chunk(""));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    
    
          document.close();
    
        File pdfFile = new File(dest+"/"+fileName1);
        if (!pdfFile.exists()) {
            pdfFile.mkdir();
        }
    
    
    
        if (pdfFile != null && pdfFile.exists() ) //Checking for the file is exist or not
        {
            
            Intent intent = new Intent(Intent.ACTION_VIEW);
    
    
            Uri mURI = FileProvider.getUriForFile(
                    context,
                    context.getApplicationContext()
                            .getPackageName() + ".provider", pdfFile);
            intent.setDataAndType(mURI, "application/pdf");
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    
            try {
                context.startActivity(intent);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        } else {
    
            Toast.makeText(context, "The file not exists! ", Toast.LENGTH_SHORT).show();
    
        }
    

    This is how you can make PDF through iText library and can display it.