The situation: my website generate pdf form html using html2pdf JavaScript and try below solution in
It successfully start downloading without crashing but the pdf file downloads is unsuccessful.
the kotlin code:
<code>private fun download(
url: String,
userAgent: String,
contentDisposition: String,
mimeType: String,
contentLength: Long
) {
val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image")
if (!folder.exists()) {
folder.mkdirs()
}
Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show()
// Remove the 'blob:' prefix if it exists
val cleanUrl = url.replace("blob:", "")
val request = DownloadManager.Request(Uri.parse(cleanUrl))
request.setMimeType(mimeType)
val cookie = CookieManager.getInstance().getCookie(cleanUrl)
request.addRequestHeader("cookie", cookie)
request.addRequestHeader("User-Agent", userAgent)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
)
val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType)
request.setTitle(fileName)
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"Image/$fileName"
)
val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
}
</code>
<code>private fun download(
url: String,
userAgent: String,
contentDisposition: String,
mimeType: String,
contentLength: Long
) {
val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image")
if (!folder.exists()) {
folder.mkdirs()
}
Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show()
// Remove the 'blob:' prefix if it exists
val cleanUrl = url.replace("blob:", "")
val request = DownloadManager.Request(Uri.parse(cleanUrl))
request.setMimeType(mimeType)
val cookie = CookieManager.getInstance().getCookie(cleanUrl)
request.addRequestHeader("cookie", cookie)
request.addRequestHeader("User-Agent", userAgent)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
)
val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType)
request.setTitle(fileName)
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"Image/$fileName"
)
val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
}
</code>
private fun download(
url: String,
userAgent: String,
contentDisposition: String,
mimeType: String,
contentLength: Long
) {
val folder = File(Environment.getExternalStorageDirectory().toString() + "/Download/Image")
if (!folder.exists()) {
folder.mkdirs()
}
Toast.makeText(this, "Download Started", Toast.LENGTH_SHORT).show()
// Remove the 'blob:' prefix if it exists
val cleanUrl = url.replace("blob:", "")
val request = DownloadManager.Request(Uri.parse(cleanUrl))
request.setMimeType(mimeType)
val cookie = CookieManager.getInstance().getCookie(cleanUrl)
request.addRequestHeader("cookie", cookie)
request.addRequestHeader("User-Agent", userAgent)
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE
)
val fileName = URLUtil.guessFileName(cleanUrl, contentDisposition, mimeType)
request.setTitle(fileName)
request.setNotificationVisibility(
DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED
)
request.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS,
"Image/$fileName"
)
val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager
downloadManager.enqueue(request)
}
I tried the recommendation about try in real device however the result is the same. Please help 🙂
the logcat:
<code>2024-06-03 23:13:39.866 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1
2024-06-03 23:13:40.542 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:40.883 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16
2024-06-03 23:13:40.948 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.603 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.644 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.332 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.337 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.627 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:43.182 7633-7633 Url com.coding.meet.webviewtoapp D blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e
2024-06-03 23:13:43.183 7633-7633 userAgent com.coding.meet.webviewtoapp D Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
2024-06-03 23:13:43.183 7633-7633 contentDisposition com.coding.meet.webviewtoapp D
2024-06-03 23:13:43.183 7633-7633 mimeType com.coding.meet.webviewtoapp D application/pdf
2024-06-03 23:13:43.188 7633-7633 contentLength com.coding.meet.webviewtoapp D 561986
2024-06-03 23:13:43.271 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135634846; UID 10064; state: DISABLED
2024-06-03 23:13:43.272 611-2517 Compatibil...geReporter system_server D Compat change id reported: 177438394; UID 10064; state: DISABLED
2024-06-03 23:13:43.273 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135772972; UID 10064; state: DISABLED
</code>
<code>2024-06-03 23:13:39.866 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1
2024-06-03 23:13:40.542 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:40.883 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16
2024-06-03 23:13:40.948 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.603 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.644 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.332 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.337 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.627 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:43.182 7633-7633 Url com.coding.meet.webviewtoapp D blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e
2024-06-03 23:13:43.183 7633-7633 userAgent com.coding.meet.webviewtoapp D Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
2024-06-03 23:13:43.183 7633-7633 contentDisposition com.coding.meet.webviewtoapp D
2024-06-03 23:13:43.183 7633-7633 mimeType com.coding.meet.webviewtoapp D application/pdf
2024-06-03 23:13:43.188 7633-7633 contentLength com.coding.meet.webviewtoapp D 561986
2024-06-03 23:13:43.271 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135634846; UID 10064; state: DISABLED
2024-06-03 23:13:43.272 611-2517 Compatibil...geReporter system_server D Compat change id reported: 177438394; UID 10064; state: DISABLED
2024-06-03 23:13:43.273 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135772972; UID 10064; state: DISABLED
</code>
2024-06-03 23:13:39.866 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=5244564.50ms min=5244564.50ms max=5244564.50ms count=1
2024-06-03 23:13:40.542 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:40.883 7633-7652 EGL_emulation com.coding.meet.webviewtoapp D app_time_stats: avg=58.57ms min=8.43ms max=488.87ms count=16
2024-06-03 23:13:40.948 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd6a 0ms Starting document clone", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Document cloned, using computed rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.470 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 931ms Starting DOM parsing", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.603 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1066ms Added image https://smartappx.site/assets/malaysia.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:41.644 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1107ms Added image https://smartappx.site/assets/jps-logo.png", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.332 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1794ms Starting renderer", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.337 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 1796ms Canvas renderer initialized (634x1373 at 0,0) with scale 2", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:42.627 7633-7633 chromium com.coding.meet.webviewtoapp I [INFO:CONSOLE(2)] "18fdea9cd86 2074ms Finished rendering", source: https://smartappx.site/js/html2pdf.bundle.min.js (2)
2024-06-03 23:13:43.182 7633-7633 Url com.coding.meet.webviewtoapp D blob:https://smartappx.site/547b98d3-ba77-4c79-a315-35e020c5d34e
2024-06-03 23:13:43.183 7633-7633 userAgent com.coding.meet.webviewtoapp D Mozilla/5.0 (Linux; Android 12; sdk_gphone64_x86_64 Build/SE1A.211212.001.B1; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36
2024-06-03 23:13:43.183 7633-7633 contentDisposition com.coding.meet.webviewtoapp D
2024-06-03 23:13:43.183 7633-7633 mimeType com.coding.meet.webviewtoapp D application/pdf
2024-06-03 23:13:43.188 7633-7633 contentLength com.coding.meet.webviewtoapp D 561986
2024-06-03 23:13:43.271 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135634846; UID 10064; state: DISABLED
2024-06-03 23:13:43.272 611-2517 Compatibil...geReporter system_server D Compat change id reported: 177438394; UID 10064; state: DISABLED
2024-06-03 23:13:43.273 611-2517 Compatibil...geReporter system_server D Compat change id reported: 135772972; UID 10064; state: DISABLED
New contributor
Tengku Muhammad Muzaffar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.