簡體   English   中英

如何在android studio中將Html頁面轉換為Pdf

[英]How to convert Html page to Pdf in android studio

我知道之前有人問過這個問題,但仍然無法將 HTML 文件轉換為 pdf。 我嘗試了互聯網上提供的一些解決方案,但它們已經過時了。 如果有人可以幫助我解決我的問題,那將是一個很大的幫助。 我正在使用以下代碼行保存 HTML 文件。

 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();
        }

試試這個代碼:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM