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

//
// PdfComposer.swift
// GMERemittance
//
// Created by Sujal on 2/20/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
class PdfComposer: NSObject {
let pathToPdfHTMLTemplate = Bundle.main.path(forResource: "index", ofType: "html")
let pathToPdfHTMLTemplateWallet = Bundle.main.path(forResource: "wallet", ofType: "html")
let pathToImage = Bundle.main.path(forResource: "gme-logo", ofType: "png")!
var invoiceNumber: String!
var pdfFilename: String!
override init() {
super.init()
}
func renderPdf(pdfDetails: [String: String]) -> String! {
do {
let paymentMode = pdfDetails["PAYOUTMODE"]
let displayStyleNone = "display: none;"
let displayStyleShow = "border-top: 1px solid #cdced2;"
var HTMLContent = try String(contentsOfFile: pathToPdfHTMLTemplate!)
let image : UIImage = UIImage(named:"gme-logo")!
let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
let imageContent = "data:image/png;base64," + strBase64
HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: imageContent)
HTMLContent = HTMLContent.replacingOccurrences(of: "#AMOUNT#", with: pdfDetails["AMOUNT"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#CONTROLNUMBER#", with: pdfDetails["CONTROLNUMBER"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#TXDATE#", with: pdfDetails["TXDATE"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#TXNUMBER#", with: pdfDetails["TXNUMBER"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#RECIPIENT#", with: pdfDetails["RECIPIENT"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#RELATION#", with: pdfDetails["RELATION"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#PHONE#", with: pdfDetails["PHONE"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#EMAIL#", with: pdfDetails["EMAIL"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#SERVICEFEE#", with: pdfDetails["SERVICEFEE"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#TOTALAMOUNT#", with: pdfDetails["TOTALAMOUNT"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENT#", with: pdfDetails["AGENT"]!)
switch paymentMode {
case paymentModeType.cashPaymentPartner.rawValue?:
HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: "Payout Agent/Bank")
HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: pdfDetails["ADDRESS"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleNone)
case paymentModeType.bankPayment.rawValue?:
HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: "Payout Agent/Bank")
HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: pdfDetails["ADDRESS"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#ACCOUNTNUMBER#", with: pdfDetails["ACCOUNTNUMBER"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#BRANCH#", with: pdfDetails["BRANCH"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleNone)
case paymentModeType.homeDelivery.rawValue?:
HTMLContent = HTMLContent.replacingOccurrences(of:"#AGENTORBANK#", with: "Payout Mode")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSUP#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#DELIVERYADDRESS#", with: pdfDetails["DELIVERYADDRESS"]!)
HTMLContent = HTMLContent.replacingOccurrences(of:"#AGENTORBANK#", with: "Payout Mode")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: displayStyleNone)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: displayStyleShow)
HTMLContent = HTMLContent.replacingOccurrences(of: "#DISTRICT#", with: pdfDetails["DISTRICT"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#CITY#", with: pdfDetails["CITY"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#STATEORPROVINCE#", with: pdfDetails["STATEORPROVINCE"]!)
default:
HTMLContent = HTMLContent.replacingOccurrences(of: "#AGENTORBANK#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#ADDRESS#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEADDRESSDELIVERY#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEACNUM#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEBRANCH#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLECITY#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLEDISTRICT#", with: " ")
HTMLContent = HTMLContent.replacingOccurrences(of: "#STYLESTATE#", with: " ")
}
return HTMLContent
}
catch {
}
return nil
}
func exportHTMLContentToPDF(HTMLContent: String) -> Bool {
do {
let printPageRenderer = CustomPrintPageRenderer()
let printFormatter = UIMarkupTextPrintFormatter(markupText: HTMLContent)
printPageRenderer.addPrintFormatter(printFormatter, startingAtPageAt: 0)
let pdfData = drawPDFUsingPrintPageRenderer(printPageRenderer: printPageRenderer)
pdfFilename = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] + "/" + getFileName()
pdfData?.write(toFile: pdfFilename, atomically: true)
print(pdfFilename, "pdf file path")
return true
} catch {
}
return false
}
func renderPdfWallet(pdfDetails: [String: String]) -> String! {
do {
var HTMLContent = try String(contentsOfFile: pathToPdfHTMLTemplateWallet!)
let image : UIImage = UIImage(named:"gme-logo")!
let imageData:NSData = UIImagePNGRepresentation(image)! as NSData
let strBase64 = imageData.base64EncodedString(options: .lineLength64Characters)
let imageContent = "data:image/png;base64," + strBase64
HTMLContent = HTMLContent.replacingOccurrences(of: "#LOGO_IMAGE#", with: imageContent)
HTMLContent = HTMLContent.replacingOccurrences(of: "#TXDATE#", with: pdfDetails["TXDATE"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#TXNUMBER#", with: pdfDetails["TXNUMBER"]!)
HTMLContent = HTMLContent.replacingOccurrences(of: "#AMOUNT#", with: pdfDetails["AMOUNT"]!)
return HTMLContent
}
catch {
print("Unable to open and use HTML template files.")
}
return nil
}
func drawPDFUsingPrintPageRenderer(printPageRenderer: UIPrintPageRenderer) -> NSData! {
let data = NSMutableData()
UIGraphicsBeginPDFContextToData(data, CGRect.zero, nil)
for i in 0..<printPageRenderer.numberOfPages {
UIGraphicsBeginPDFPage()
printPageRenderer.drawPage(at: i, in: UIGraphicsGetPDFContextBounds())
}
UIGraphicsEndPDFContext()
return data
}
func getFileName() -> String {
// let date = Date()
// let calender = Calendar.current
// let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
//
// let year = components.year
// let month = components.month
// let day = components.day
// let hour = components.hour
// let minute = components.minute
// let second = components.second
//
// let dateTime_string = String(year!) + "-" + String(month!) + "-" + String(day!) + "_" + String(hour!) + "-" + String(minute!) + "-" + String(second!)
//
// return "GME_" + dateTime_string + ".pdf"
return "GME-sendmoney-" + String(NSDate().timeIntervalSince1970) + ".pdf"
}
}