Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

i am trying to create pdf using pdfbox. i am storing EditText data as html in Sqlite DB.

now i am retrieving data from sqliteDB and creating pdf of that. this data is having marathi language as well as english language.

i am using NotoSerifDevanagari-Bold font and have added it to assets folder. from there i am accessing this font into code. but i am getting error. please find my code and error below.

AssetManager assetManager;
PDFBoxResourceLoader.init(getApplicationContext());
File FilePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
assetManager = getAssets();
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);
PDFont font = PDType0Font.load(document, assetManager.open("notoserifdevanagaribold.ttf"));
PDPageContentStream contentStream;
// Define a content stream for adding to the PDF
contentStream = new PDPageContentStream(document, page);
Cursor data = mDatabaseHelper.getDataByDeckname(deckname);
StringBuilder builder=new StringBuilder();
while (data.moveToNext()) {
    String front_page_desc = data.getString(3);
    String back_page_desc =  data.getString(4);
    contentStream.beginText();
    contentStream.setNonStrokingColor(15, 38, 192);
    contentStream.setFont(font, 12);
    contentStream.newLineAtOffset(100, 700);
    contentStream.showText(Html.fromHtml(front_page_desc).toString());
    contentStream.endText();
    contentStream.beginText();
    contentStream.setNonStrokingColor(15, 38, 192);
    contentStream.setFont(font, 12);
    contentStream.newLineAtOffset(100, 700);
    contentStream.showText(Html.fromHtml(back_page_desc).toString());
    contentStream.endText();
contentStream.close();
String path = FilePath.getAbsolutePath() + "/temp.pdf";
document.save(path);
document.close();

ERROR

W/System.err: java.lang.IllegalArgumentException: No glyph for U+000A in font NotoSerifDevanagari-Bold

I tried so many examples for above error but i am not able to fix the issue. this error i am getting on contentStream.showText(Html.fromHtml(front_page_desc).toString()); line. can someone please help me on above.

if i am replacing contentStream.showText(Html.fromHtml(front_page_desc).toString()); with contentStream.showText(" TEsting content page back"); then also i am getting same error. – Ajinkya kadam Jan 6, 2020 at 10:43 i have removed contentStream.newLineAtOffset(100, 700); but then also i am getting same error. – Ajinkya kadam Jan 6, 2020 at 11:02 'contentStream.showText(" TEsting content page back"); then also i am getting same error.' - as there appears to be no U+000A character in that string, that is hard to believe. – mkl Jan 6, 2020 at 11:41 can you please explain bit i have not understood properly. what char should be there so that my code will run. – Ajinkya kadam Jan 6, 2020 at 12:10

As per this link U+000A is the Unicode for new line. Any font will fail if you try to render it. In order to avoid such error you can try something like this:

 String[] lines = text.split("\\n");
        for (String line : lines) {
            if (!line.isBlank()) {
                contentStream.showText(line);
               // add new line here if you want to
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.