Эх сурвалжийг харах

Make text files printable

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch 3 жил өмнө
parent
commit
9c0662484e

+ 6 - 0
iOSClient/Data/NCDatabase.swift

@@ -24,6 +24,7 @@
 
 import UIKit
 import RealmSwift
+import NCCommunication
 
 protocol DateCompareable {
     var dateKey: Date { get }
@@ -415,6 +416,11 @@ class tableMetadata: Object, NCUserBaseUrl {
 
 extension tableMetadata {
     var fileExtension: String { (fileNameView as NSString).pathExtension }
+
+    var isPrintable: Bool {
+        return (classFile == NCCommunicationCommon.typeClassFile.image.rawValue && contentType != "image/svg+xml") ||
+        ["application/pdf", "com.adobe.pdf"].contains(contentType) || contentType.hasPrefix("text/")
+    }
 }
 
 class tablePhotoLibrary: Object {

+ 23 - 12
iOSClient/Main/NCFunctionCenter.swift

@@ -25,6 +25,7 @@ import UIKit
 import NCCommunication
 import Queuer
 import JGProgressHUD
+import SVGKit
 
 @objc class NCFunctionCenter: NSObject, UIDocumentInteractionControllerDelegate, NCSelectDelegate {
     @objc public static let shared: NCFunctionCenter = {
@@ -281,21 +282,31 @@ import JGProgressHUD
     // MARK: - Print
 
     func printDocument(metadata: tableMetadata) {
-
         let fileNameURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
-
-        if UIPrintInteractionController.canPrint(fileNameURL) {
-
-            let printInfo = UIPrintInfo(dictionary: nil)
-            printInfo.jobName = fileNameURL.lastPathComponent
-            printInfo.outputType = .photo
-
-            let printController = UIPrintInteractionController.shared
-            printController.printInfo = printInfo
-            printController.showsNumberOfCopies = true
+        let printController = UIPrintInteractionController.shared
+        let printInfo = UIPrintInfo(dictionary: nil)
+        printInfo.jobName = fileNameURL.lastPathComponent
+        printInfo.outputType = metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue ? .photo : .general
+        printController.printInfo = printInfo
+        printController.showsNumberOfCopies = true
+        
+        guard !UIPrintInteractionController.canPrint(fileNameURL) else {
             printController.printingItem = fileNameURL
-            printController.present(animated: true, completionHandler: nil)
+            printController.present(animated: true)
+            return
         }
+
+        // can't print without data
+        guard let data = try? Data(contentsOf: fileNameURL), let text = String(data: data, encoding: .utf8) else { return }
+
+        let textWithoutNewlines = text.replacingOccurrences(of: "\n", with: "<br />")
+        let formatter = UIMarkupTextPrintFormatter(markupText: textWithoutNewlines)
+        formatter.perPageContentInsets.top = 72
+        formatter.perPageContentInsets.bottom = 72
+        formatter.perPageContentInsets.left = 72
+        formatter.perPageContentInsets.right = 72
+        printController.printFormatter = formatter
+        printController.present(animated: true)
     }
 
     // MARK: - Save photo

+ 2 - 10
iOSClient/Menu/NCCollectionViewCommon+Menu.swift

@@ -181,16 +181,8 @@ extension NCCollectionViewCommon {
         //
         // PRINT
         //
-        if (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
-            actions.append(
-                NCMenuAction(
-                    title: NSLocalizedString("_print_", comment: ""),
-                    icon: NCUtility.shared.loadImage(named: "printer"),
-                    action: { _ in
-                        NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
-                    }
-                )
-            )
+        if metadata.isPrintable {
+            actions.append(.printAction(metadata: metadata))
         }
 
         //

+ 11 - 0
iOSClient/Menu/NCMenuAction.swift

@@ -176,4 +176,15 @@ extension NCMenuAction {
             }
         )
     }
+
+    /// Open AirPrint view to print a single file
+    static func printAction(metadata: tableMetadata) -> NCMenuAction {
+        NCMenuAction(
+            title: NSLocalizedString("_print_", comment: ""),
+            icon: NCUtility.shared.loadImage(named: "printer"),
+            action: { _ in
+                NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
+            }
+        )
+    }
 }