0

I have this method to generate a PDF from the current activity:

public boolean convertToPDF(String pdfFileName) {

    PdfDocument document = new PdfDocument();

    View content = GraphActivity.rl_main;
    int pageNumber = 1;
    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(),
    content.getHeight(), pageNumber).create();

    PdfDocument.Page page = document.startPage(pageInfo);

    content.draw(page.getCanvas());

    document.finishPage(page);

    pdfFile = new File(pdfFileName);

        try {
            pdfFile.createNewFile();
            OutputStream out = new FileOutputStream(pdfFile);
            document.writeTo(out);
            document.close();
            out.close();
        } catch (IOException e) {
            Log.e("Error", "Could not create PDF file!");
            return false;
        }
    return true;
}

For some reason, when running this method in Marshmallow, I get a PDF file with unreadable characters. I have granted the app permission WRITE_EXTERNAL_STORAGE at runtime. Is there anything I need to do different for Marshmallow? I can confirm this works on older Android versions.

EDIT: I am using custom typefaces in my assets folder. I wonder if this has anything to do with it.

UPDATE: I tried disabling the custom typefaces, and that fixes the issue. However, I would prefer to preserve my custom typefaces.

Pink Jazz
  • 784
  • 4
  • 13
  • 34
  • Call `out.flush()` and `out.getFD().sync()` before `out.close()` (after modifying `out` to be of type `FileOutputStream`). You might also try drawing your content into a `Bitmap`-backed `Canvas` to see whether the problem is in the drawing or the PDF generation. Also, please do not put `View` objects in `static` fields, and please do disk I/O on background threads. – CommonsWare Aug 24 '16 at 17:02
  • Tried that just now, no luck. – Pink Jazz Aug 24 '16 at 17:31
  • Update: I tried disabling the custom typefaces, and that seems to work. However, I would prefer to retain my custom typefaces. – Pink Jazz Aug 24 '16 at 17:41
  • If you think that this is a bug in Android, create a reproducible test case and [file an issue](http://b.android.com). If you think that this may be a problem in your implementation, you would need to edit your question to show how you are using custom typefaces. – CommonsWare Aug 24 '16 at 17:47

0 Answers0