0

I am trying to export some html as pdf file. I am using iText. My html has actually bengali (UTF-8) characters which are either not showing in the pdf or shown as ?????

Here is my code:

private void pdfCreate (String filePath) {

        try {

            String path = article.getTitle() + ".pdf";


            String rootPath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/mukti/";
            File root = new File(rootPath);
            if (!root.exists()) {
                root.mkdirs();
            }
            File f = new File(rootPath + path);
            if (f.exists()) {
                f.delete();
            }

            f.createNewFile();

            String d1 = "<html><head></head><body style=\"font-family:Kalpurush; color: red; background: green;\"> 2546654 ০১৩ সালে ক্লেইনার পার্কিন্সের এক জরিপ অনুযায়ী প্রতিদিন একজন মানুষ গড়ে ১০০-১৫০ বার তার মোবাইল ফোন চেক করতো। ৩ </body></html>";


            OutputStream myFile = new FileOutputStream(f);
            Document document = new Document();

            document.addCreationDate();
            document.setPageSize(PageSize.A4);
            document.setMargins(36, 36, 36, 36);
            document.setMarginMirroring(true);


            PdfWriter writer = PdfWriter.getInstance(document, myFile);
            document.open();

            XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

            InputStream is;

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
                is = new ByteArrayInputStream(d1.getBytes(StandardCharsets.UTF_8));
            } else
                is = new ByteArrayInputStream(d1.getBytes("UTF-8"));

            String FONT = "assets/fonts/KONGO.ttf";
            XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
            fontImp.register(FONT);


            worker.parseXHtml(writer, document, is, Charset.forName("UTF-8"), fontImp);

            document.close();
            myFile.close();
            System.out.println("Done.");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

I have tried this answers but none is working

Things i tried 1

Things i tried 2

My output PDF:

https://drive.google.com/file/d/17L-cbtKQE5AZjQFD4HeqRPjh83FQiF4I/view?usp=sharing

Ahsan Aasim
  • 1,177
  • 3
  • 14
  • 40

1 Answers1

0

It seems my code is working perfectly. It was the font family name in the html body that wasn't spelled correctly.

Ahsan Aasim
  • 1,177
  • 3
  • 14
  • 40