Create Pdf file in iOS Swift
Posted By : Nitin Bhatt | 26-Dec-2017
In this blog i have discussed how can we create pdf file in iOS swift.
To create Pdf file form html we are using TPPDF for more details about TPPDF please go through this link :-
https://github.com/Techprimate/TPPDF
Here are the list of commands which we will use to create document :-
Add Image :-
pdf.addImage(.contentCenter, image: UIImage(named: "receiptLogo")!, size: CGSize(width: 50, height: 50))
Add Text :-
pdf.addText(.contentCenter, text: "Please enter the text", lineSpacing: 1)
Add Spacing :-
pdf.addSpace(space: 2)
Add Table :-
let orderDateAndTimeTableData: [[String]] = [ ["25-Dec-2017", "9:19AM"] ] pdf.setFont(.contentLeft, font: UIFont.systemFont(ofSize: 5)) let orderDateAndTimeTableDataAligment: [[TableCellAlignment]] = [ [.center, .center] ] let orderDateAndTimeTableDataWidth: [CGFloat] = [ 0.5, 0.5 ] let tableStyle = TableStyleDefaults.simple tableStyle.setCellStyle(row: 0, column: 0, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) tableStyle.setCellStyle(row: 0, column: 1, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) tableStyle.setCellStyle(row: 0, column: 0, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) pdf.addTable(data: orderDateAndTimeTableData, alignment: orderDateAndTimeTableDataAligment, relativeColumnWidth: orderDateAndTimeTableDataWidth, padding: 0, margin: 0, style: TableStyleDefaults.simple)
Write this function inside your file to create pdf and don't forget to import TPPDF :-
func generatePDF() { let pdf = PDFGenerator(pageSize: CGSize(width: 230, height: 842)) // let pdf = PDFGenerator(format: .a5) pdf.addImage(.contentCenter, image: UIImage(named: "receiptLogo")!, size: CGSize(width: 50, height: 50)) pdf.addSpace(space: 10) pdf.addText(.contentCenter, text: "Testing PDF Receipt", lineSpacing:1) pdf.addSpace(space: 10) let orderDateAndTimeTableData: [[String]] = [ ["25-Dec-2017", "9:24AM"] ] pdf.setFont(.contentLeft, font: UIFont.systemFont(ofSize: 5)) let orderDateAndTimeTableDataAligment: [[TableCellAlignment]] = [ [.center, .center] ] let orderDateAndTimeTableDataWidth: [CGFloat] = [ 0.5, 0.5 ] let tableStyle = TableStyleDefaults.simple tableStyle.setCellStyle(row: 0, column: 0, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) tableStyle.setCellStyle(row: 0, column: 1, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) tableStyle.setCellStyle(row: 0, column: 0, style: TableCellStyle(fillColor: .white, textColor: .black, font: UIFont.systemFont(ofSize: 4.0))) pdf.addTable(data: orderDateAndTimeTableData, alignment: orderDateAndTimeTableDataAligment, relativeColumnWidth: orderDateAndTimeTableDataWidth, padding: 0, margin: 0, style: TableStyleDefaults.simple) let userName = UserDefaults.standard.object(forKey: "username") as! String let storeDetails: [[String]] = [ ["TRANSACTION : 12345678", "CASHIER : Nitin"] ] pdf.setFont(.contentLeft, font: UIFont.systemFont(ofSize: 5)) let storeDetailsAligment: [[TableCellAlignment]] = [ [.center, .center] ] let storeDetailsWidth: [CGFloat] = [ 0.5, 0.5 ] pdf.addTable(data: storeDetails, alignment: storeDetailsAligment, relativeColumnWidth: storeDetailsWidth, padding: 0, margin: 0, style: TableStyleDefaults.simple) pdf.addSpace(space: 10) pdf.addText(.contentCenter, text: "THANK YOU", lineSpacing:1) pdf.addSpace(space: 10) let url = pdf.generatePDFfile("example") }
Hope this will help Thank You
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Nitin Bhatt
Nitin is an Assistant Project Manager specializing in iOS Application Development. He is an avid reader.