Home > Blockchain >  How to convert Html page to Pdf in android studio
How to convert Html page to Pdf in android studio

Time:10-27

I know this question has been asked previously but still, I can't convert the HTML file to pdf. I have tried some solutions provided on the internet but they are outdated. It would be a big help if someone can help me solve my issue. I am saving the HTML file with the following lines of code.

 String directory_path = String.valueOf(getApplicationContext().getExternalFilesDir(null));
        File file = new File(directory_path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String targetPdf = directory_path "myFile.html";
        File filePath = new File(targetPdf);

        String html = Html.toHtml(text.getText());

        try {
            FileOutputStream out = new FileOutputStream(filePath);
            byte[] data = html.getBytes();
            out.write(data);
            out.close();
            Log.e(TAG, "File Save : "   filePath.getPath());
            Toast.makeText(this,"Saved in " filePath.getPath(),Toast.LENGTH_SHORT).show();


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

CodePudding user response:

Try this code :

PdfConverter converter = PdfConverter.getInstance();
String directory_path = Environment.getExternalStorageDirectory().toString();
File file = new File(directionPath, "name_file.pdf");
String htmlString = "<html><body><p>Hello World</p></body></html>";
converter.convert(getContext(), htmlString, file);
  • Related