Android: Formatting problem when converting to PDF

90 Views Asked by At

I'm trying to convert my TextView content to .PDF, I don't have a problem saving the content, but I have a problem converting it to PDF. I can save the content from TextView in a .HTML document, but when I change the extension to .pdf, I try to open the file, PDF Reader reports a problem with the format of my pdf file. I beg you for help!!!

Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

MainActivity:

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
          float hight = displaymetrics.heightPixels;
          float width = displaymetrics.widthPixels;

          int convertHighet = (int) hight, convertWidth = (int) width;

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();

Paint paint = new Paint();
canvas.drawPaint(paint);

paint.setColor(Color.BLUE);
document.finishPage(page);


File sdcard = getBaseContext().getExternalFilesDir(null);
File dir = new File(sdcard.getAbsolutePath() + "/MYDIR/");
     dir.mkdir();
File file = new File(dir, "MYFILE.pdf");
FileOutputStream os = null;
     try {
         os = new FileOutputStream(file);                  
         os.write(Html.toHtml((Spanned) returnedText.getText()).getBytes());
         os.close();
            } catch (IOException e) {
              e.printStackTrace();
                                }

     // close the document
     document.close();
0

There are 0 best solutions below