我知道之前有人問過這個問題,但仍然無法將 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();
}
uj5u.com熱心網友回復:
試試這個代碼:
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/338342.html
上一篇:如何洗掉此按鈕的圖形錯誤?
