Prompt Title: Download and Print PDF (Kotlin, Swift)

Created 11 months ago
Votes 0
Views 159 times
IT
0
Human: show me sample code to download pdf file and send it to the print preview for android using kotlin and ios using swift 5
ChatGPT:

Here's some sample code in Kotlin for Android to download a PDF file and send it to the print preview:

java
val pdfUrl = "https://example.com/sample.pdf" val request = DownloadManager.Request(Uri.parse(pdfUrl)) val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager val downloadId = downloadManager.enqueue(request) val query = DownloadManager.Query().apply { setFilterById(downloadId) } val cursor = downloadManager.query(query) if (cursor.moveToFirst()) { val downloadStatus = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) if (downloadStatus == DownloadManager.STATUS_SUCCESSFUL) { val pdfUri = Uri.parse(cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))) val printIntent = Intent(Intent.ACTION_VIEW).apply { setDataAndType(pdfUri, "application/pdf") flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK } startActivity(Intent.createChooser(printIntent, "Print PDF")) } }

And here's some sample code in Swift 5 for iOS to download a PDF file and send it to the print preview:

vbnet
let pdfUrl = URL(string: "https://example.com/sample.pdf")! let session = URLSession.shared let downloadTask = session.downloadTask(with: pdfUrl) { (url, response, error) in if let error = error { print("Error downloading PDF: \(error)") return } guard let pdfUrl = url else { print("Error getting PDF URL") return } let printController = UIPrintInteractionController.shared let printInfo = UIPrintInfo.printInfo() printInfo.outputType = .general printController.printInfo = printInfo printController.printingItem = pdfUrl printController.present(animated: true, completionHandler: nil) } downloadTask.resume()
avatar
Dhanu
Shared 1 prompt
Created 11 months ago

Leave a Comment

Related Tag Prompts

94
0
392
11
151
0
Docker Pro
1 year ago 2023-01-29 06:08:59 AIPRM
308
0
MySQL Marvel
1 year ago 2023-02-13 00:05:32 AIPRM
260
0
Ansible Expert
1 year ago 2023-02-01 09:23:50 AIPRM
193
0
PostgreSQL Pro
1 year ago 2023-02-07 03:45:19 AIPRM
114
0
270
1
Dockerize me
1 year ago 2023-02-17 08:27:58 Chad Thompson-Smith
52
0
Windows Wizard
1 year ago 2023-01-21 13:43:15 AIPRM
163
0
Linux Legend
1 year ago 2023-01-22 12:08:21 AIPRM
325
0
MacOS Master
1 year ago 2023-01-22 10:29:36 AIPRM