You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
10 KiB

6 years ago
  1. //
  2. // PdfComposer.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 2/20/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class PdfComposer: NSObject {
  10. let pathToPdfHTMLTemplate = Bundle.main.path(forResource: "index", ofType: "html")
  11. let pathToPdfHTMLTemplateWallet = Bundle.main.path(forResource: "wallet", ofType: "html")
  12. let pathToImage = Bundle.main.path(forResource: "gme-logo", ofType: "png")!
  13. var invoiceNumber: String!
  14. var pdfFilename: String!
  15. override init() {
  16. super.init()
  17. }
  18. func renderPdf(pdfDetails: [String: String]) -> String! {
  19. do {
  20. let paymentMode = pdfDetails["PAYOUTMODE"]
  21. let displayStyleNone = "display: none;"
  22. let displayStyleShow = "border-top: 1px solid #cdced2;"
  23. var HTMLContent = try String(contentsOfFile: pathToPdfHTMLTemplate!)
  24. let image : UIImage = UIImage(named:"gme-logo")!
  25. let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
  26. let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
  27. let imageContent = "data:image/png;base64," + strBase64
  28. HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: imageContent)
  29. HTMLContent = HTMLContent.replacingOccurrences(of: "#AMOUNT#", with: pdfDetails["AMOUNT"]!)
  30. HTMLContent = HTMLContent.replacingOccurrences(of: "#CONTROLNUMBER#", with: pdfDetails["CONTROLNUMBER"]!)
  31. HTMLContent = HTMLContent.replacingOccurrences(of: "#TXDATE#", with: pdfDetails["TXDATE"]!)
  32. HTMLContent = HTMLContent.replacingOccurrences(of: "#TXNUMBER#", with: pdfDetails["TXNUMBER"]!)
  33. HTMLContent = HTMLContent.replacingOccurrences(of: "#RECIPIENT#", with: pdfDetails["RECIPIENT"]!)
  34. HTMLContent = HTMLContent.replacingOccurrences(of: "#RELATION#", with: pdfDetails["RELATION"]!)
  35. HTMLContent = HTMLContent.replacingOccurrences(of: "#PHONE#", with: pdfDetails["PHONE"]!)
  36. HTMLContent = HTMLContent.replacingOccurrences(of: "#EMAIL#", with: pdfDetails["EMAIL"]!)
  37. HTMLContent = HTMLContent.replacingOccurrences(of: "#SERVICEFEE#", with: pdfDetails["SERVICEFEE"]!)
  38. HTMLContent = HTMLContent.replacingOccurrences(of: "#TOTALAMOUNT#", with: pdfDetails["TOTALAMOUNT"]!)
  39. HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENT#", with: pdfDetails["AGENT"]!)
  40. switch paymentMode {
  41. case paymentModeType.cashPaymentPartner.rawValue?:
  42. HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: "Payout Agent/Bank")
  43. HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: pdfDetails["ADDRESS"]!)
  44. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleNone)
  45. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleNone)
  46. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleNone)
  47. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleNone)
  48. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleNone)
  49. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleNone)
  50. case paymentModeType.bankPayment.rawValue?:
  51. HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: "Payout Agent/Bank")
  52. HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: pdfDetails["ADDRESS"]!)
  53. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleNone)
  54. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleShow)
  55. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleShow)
  56. HTMLContent = HTMLContent.replacingOccurrences(of: "#ACCOUNTNUMBER#", with: pdfDetails["ACCOUNTNUMBER"]!)
  57. HTMLContent = HTMLContent.replacingOccurrences(of: "#BRANCH#", with: pdfDetails["BRANCH"]!)
  58. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleNone)
  59. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleNone)
  60. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleNone)
  61. case paymentModeType.homeDelivery.rawValue?:
  62. HTMLContent = HTMLContent.replacingOccurrences(of:"#AGENTORBANK#", with: "Payout Mode")
  63. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSUP#", with: displayStyleNone)
  64. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleShow)
  65. HTMLContent = HTMLContent.replacingOccurrences(of: "#DELIVERYADDRESS#", with: pdfDetails["DELIVERYADDRESS"]!)
  66. HTMLContent = HTMLContent.replacingOccurrences(of:"#AGENTORBANK#", with: "Payout Mode")
  67. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleNone)
  68. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleNone)
  69. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleShow)
  70. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleShow)
  71. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleShow)
  72. HTMLContent = HTMLContent.replacingOccurrences(of: "#DISTRICT#", with: pdfDetails["DISTRICT"]!)
  73. HTMLContent = HTMLContent.replacingOccurrences(of: "#CITY#", with: pdfDetails["CITY"]!)
  74. HTMLContent = HTMLContent.replacingOccurrences(of: "#STATEORPROVINCE#", with: pdfDetails["STATEORPROVINCE"]!)
  75. default:
  76. HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: " ")
  77. HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: " ")
  78. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: " ")
  79. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: " ")
  80. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: " ")
  81. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: " ")
  82. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: " ")
  83. HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: " ")
  84. }
  85. return HTMLContent
  86. }
  87. catch {
  88. }
  89. return nil
  90. }
  91. func exportHTMLContentToPDF(HTMLContent: String) -> Bool {
  92. do {
  93. let printPageRenderer = CustomPrintPageRenderer()
  94. let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
  95. printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
  96. let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
  97. pdfFilename = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/" + getFileName()
  98. pdfData?.write(toFile: pdfFilename, atomically: true)
  99. print(pdfFilename, "pdf file path")
  100. return true
  101. } catch {
  102. }
  103. return false
  104. }
  105. func renderPdfWallet(pdfDetails: [String: String]) -> String! {
  106. do {
  107. var HTMLContent = try String(contentsOfFile: pathToPdfHTMLTemplateWallet!)
  108. let image : UIImage = UIImage(named:"gme-logo")!
  109. let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
  110. let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
  111. let imageContent = "data:image/png;base64," + strBase64
  112. HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: imageContent)
  113. HTMLContent = HTMLContent.replacingOccurrences(of: "#TXDATE#", with: pdfDetails["TXDATE"]!)
  114. HTMLContent = HTMLContent.replacingOccurrences(of: "#TXNUMBER#", with: pdfDetails["TXNUMBER"]!)
  115. HTMLContent = HTMLContent.replacingOccurrences(of: "#AMOUNT#", with: pdfDetails["AMOUNT"]!)
  116. return HTMLContent
  117. }
  118. catch {
  119. print("Unable to open and use HTML template files.")
  120. }
  121. return nil
  122. }
  123. func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
  124. let data = NSMutableData()
  125. UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
  126. for i in 0..<printPageRenderer.numberOfPages {
  127. UIGraphicsBeginPDFPage()
  128. printPageRenderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
  129. }
  130. UIGraphicsEndPDFContext()
  131. return data
  132. }
  133. func getFileName() -> String {
  134. // let date = Date()
  135. // let calender = Calendar.current
  136. // let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
  137. //
  138. // let year = components.year
  139. // let month = components.month
  140. // let day = components.day
  141. // let hour = components.hour
  142. // let minute = components.minute
  143. // let second = components.second
  144. //
  145. // let dateTime_string = String(year!) + "-" + String(month!) + "-" + String(day!) + "_" + String(hour!) + "-" + String(minute!) + "-" + String(second!)
  146. //
  147. // return "GME_" + dateTime_string + ".pdf"
  148. return "GME-sendmoney-" + String(NSDate().timeIntervalSince1970) + ".pdf"
  149. }
  150. }