|
@@ -23,7 +23,6 @@
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
-import PDFGenerator
|
|
|
import WeScan
|
|
|
import GoogleMobileVision
|
|
|
|
|
@@ -40,7 +39,7 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
|
|
|
var titleServerUrl = ""
|
|
|
var arrayImages = [UIImage]()
|
|
|
var fileName = CCUtility.createFileNameDate("scan", extension: "pdf")
|
|
|
- var password: PDFPassword = ""
|
|
|
+ var password: String = ""
|
|
|
var fileType = "PDF"
|
|
|
var ocr = true
|
|
|
|
|
@@ -186,9 +185,9 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
|
|
|
self.form.delegate = nil
|
|
|
|
|
|
if newValue as! Int == 1 {
|
|
|
- rowFileTape.selectorOptions = ["TXT"] //["PDF","TXT"]
|
|
|
- rowFileTape.value = "TXT"
|
|
|
- fileType = "TXT"
|
|
|
+ rowFileTape.selectorOptions = ["PDF","TXT"]
|
|
|
+ rowFileTape.value = "PDF"
|
|
|
+ fileType = "PDF"
|
|
|
rowPassword.disabled = true
|
|
|
rowCompressionQuality.disabled = true
|
|
|
} else {
|
|
@@ -256,9 +255,9 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
|
|
|
if formRow.tag == "password" {
|
|
|
let stringPassword = newValue as? String
|
|
|
if stringPassword != nil {
|
|
|
- password = PDFPassword(stringPassword!)
|
|
|
+ password = stringPassword!
|
|
|
} else {
|
|
|
- password = PDFPassword("")
|
|
|
+ password = ""
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -275,7 +274,7 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
|
|
|
// rowPassword
|
|
|
if fileType == "JPG" {
|
|
|
rowPassword.value = ""
|
|
|
- password = PDFPassword("")
|
|
|
+ password = ""
|
|
|
rowPassword.disabled = true
|
|
|
} else {
|
|
|
rowPassword.disabled = false
|
|
@@ -449,84 +448,54 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate {
|
|
|
|
|
|
if fileType == "PDF" {
|
|
|
|
|
|
- var pdfPages = [PDFPage]()
|
|
|
+ let pdfData = NSMutableData()
|
|
|
+ UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, nil)
|
|
|
+ let context = UIGraphicsGetCurrentContext()
|
|
|
|
|
|
- //Generate PDF
|
|
|
- for var image in self.arrayImages {
|
|
|
+ for image in self.arrayImages {
|
|
|
|
|
|
- image = changeImageFromQuality(image, dpiQuality: dpiQuality)
|
|
|
-
|
|
|
- guard let data = image.jpegData(compressionQuality: 0.5) else {
|
|
|
+ let imageCompress = changeImageFromQuality(image, dpiQuality: dpiQuality)
|
|
|
+ guard let data = imageCompress.jpegData(compressionQuality: 0.5) else {
|
|
|
self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ UIGraphicsBeginPDFPageWithInfo(CGRect(x: 0, y: 0, width: imageCompress.size.width, height: imageCompress.size.height), nil)
|
|
|
+ UIImageView.init(image: UIImage(data: data)!).layer.render(in: context!)
|
|
|
+
|
|
|
// Text Recognition
|
|
|
if NCBrandOptions.sharedInstance.useMLVision {
|
|
|
let rowTextRecognition: XLFormRowDescriptor = self.form.formRow(withTag: "textRecognition")!
|
|
|
|
|
|
if rowTextRecognition.value as! Int == 1 {
|
|
|
-
|
|
|
- let page = PDFPage.image(UIImage(data: data)!)
|
|
|
-
|
|
|
- guard let features = self.textDetector?.features(in: image, options: nil) as? [GMVTextBlockFeature] else {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- do {
|
|
|
- let data = try PDFGenerator.generated(by: [page])
|
|
|
- let nddata = NSMutableData(data: data)
|
|
|
-
|
|
|
- /*
|
|
|
- UIGraphicsBeginPDFContextToData(nddata, CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height), nil)
|
|
|
+ if let features = self.textDetector?.features(in: image, options: nil) as? [GMVTextBlockFeature] {
|
|
|
|
|
|
for textBlock in features {
|
|
|
for textLine in textBlock.lines {
|
|
|
- for textElement in textLine.elements {
|
|
|
-
|
|
|
- let font = UIFont.systemFont(ofSize: 10, weight: UIFont.Weight.regular)
|
|
|
-
|
|
|
- let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
- paragraphStyle.alignment = NSTextAlignment.left
|
|
|
- paragraphStyle.lineBreakMode = NSLineBreakMode.byWordWrapping
|
|
|
-
|
|
|
- let textFontAttributes = [
|
|
|
- NSAttributedString.Key.font: font,
|
|
|
- NSAttributedString.Key.foregroundColor: UIColor.red,
|
|
|
- NSAttributedString.Key.paragraphStyle: paragraphStyle
|
|
|
- ]
|
|
|
-
|
|
|
- let text:NSString = textElement.value! as NSString
|
|
|
-
|
|
|
- text.draw(in: textElement.bounds, withAttributes: textFontAttributes)
|
|
|
- }
|
|
|
+
|
|
|
+ let bounds = textLine.bounds
|
|
|
+ let text = textLine.value!
|
|
|
+
|
|
|
+ let font = UIFont.systemFont(ofSize: bounds.size.height, weight: .regular)
|
|
|
+ let bestFittingFont = UIFont.bestFittingFont(for: text, in: bounds, fontDescriptor: font.fontDescriptor, additionalAttributes: nil)
|
|
|
+ let paragraphStyle:NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle
|
|
|
+ paragraphStyle.alignment = NSTextAlignment.natural
|
|
|
+
|
|
|
+ let textFontAttributes = [NSAttributedString.Key.font: bestFittingFont, NSAttributedString.Key.foregroundColor: UIColor.red]
|
|
|
+ text.draw(in: bounds, withAttributes: textFontAttributes)
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- UIGraphicsEndPDFContext()
|
|
|
- */
|
|
|
-
|
|
|
- let page = PDFPage.binary(nddata as Data)
|
|
|
- pdfPages.append(page)
|
|
|
-
|
|
|
- } catch (let error) {
|
|
|
- print(error)
|
|
|
- continue
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- let page = PDFPage.image(UIImage(data: data)!)
|
|
|
- pdfPages.append(page)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ UIGraphicsEndPDFContext();
|
|
|
+
|
|
|
do {
|
|
|
- try PDFGenerator.generate(pdfPages, to: fileNameGenerateExport, password: password)
|
|
|
+ try pdfData.write(toFile: fileNameGenerateExport, options: .atomic)
|
|
|
} catch {
|
|
|
- self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
|
|
|
- return
|
|
|
+ print("error catched")
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -707,4 +676,35 @@ class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+extension UIFont {
|
|
|
+
|
|
|
+ /**
|
|
|
+ Will return the best font conforming to the descriptor which will fit in the provided bounds.
|
|
|
+ */
|
|
|
+ static func bestFittingFontSize(for text: String, in bounds: CGRect, fontDescriptor: UIFontDescriptor, additionalAttributes: [NSAttributedString.Key: Any]? = nil) -> CGFloat {
|
|
|
+ let constrainingDimension = min(bounds.width, bounds.height)
|
|
|
+ let properBounds = CGRect(origin: .zero, size: bounds.size)
|
|
|
+ var attributes = additionalAttributes ?? [:]
|
|
|
+
|
|
|
+ let infiniteBounds = CGSize(width: CGFloat.infinity, height: CGFloat.infinity)
|
|
|
+ var bestFontSize: CGFloat = constrainingDimension
|
|
|
+
|
|
|
+ for fontSize in stride(from: bestFontSize, through: 0, by: -1) {
|
|
|
+ let newFont = UIFont(descriptor: fontDescriptor, size: fontSize)
|
|
|
+ attributes[.font] = newFont
|
|
|
+
|
|
|
+ let currentFrame = text.boundingRect(with: infiniteBounds, options: [.usesLineFragmentOrigin, .usesFontLeading], attributes: attributes, context: nil)
|
|
|
+
|
|
|
+ if properBounds.contains(currentFrame) {
|
|
|
+ bestFontSize = fontSize
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return bestFontSize
|
|
|
+ }
|
|
|
+
|
|
|
+ static func bestFittingFont(for text: String, in bounds: CGRect, fontDescriptor: UIFontDescriptor, additionalAttributes: [NSAttributedString.Key: Any]? = nil) -> UIFont {
|
|
|
+ let bestSize = bestFittingFontSize(for: text, in: bounds, fontDescriptor: fontDescriptor, additionalAttributes: additionalAttributes)
|
|
|
+ return UIFont(descriptor: fontDescriptor, size: bestSize)
|
|
|
+ }
|
|
|
+}
|