How To Convert Text Content Into Pdf File In Swift 2.0


Create new single view application Xcode project by naming as”DemoPdf”. Set the values as follows and save it as a folder on your desktop.

Begin the Project:

Go to the mainStoryboard and Add a button by giving name “Generate PDF” as and a Textview to the main storyboard from the Object library.Now connect the outlet by naming as “ txtView ” for the textview and the action method ”btnGeneratePDF” for the button.

Here is the complete Code in ViewController.swift file

import UIKit

class ViewController: UIViewController {

//make the ordinary variable for pageSize of pdf we obtain after generating.

var pageSize:CGSize!

// make the outlet for the textview.

@IBOutlet weak var txtView: UITextView!

override func viewDidLoad() {



// make the action method for generating into a pdf file.

@IBAction func btnGeneratePDF(sender: AnyObject) {

pageSize = CGSizeMake (850, 1100)

let fileName: NSString = “kindle.pdf”

let path:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)

let documentDirectory: AnyObject = path.objectAtIndex(0)

let pdfPathWithFileName = documentDirectory.stringByAppendingPathComponent(fileName as String)


//lines written to get the document directory path for the generated pdf file.

if let documentsPath = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first?.path {


// “var/folder/…/documents ” copy the full path



// method to generate pdf file to display the text and image in pdf file.

func generatePDFs(filePath: String) {

UIGraphicsBeginPDFContextToFile(filePath, CGRectZero, nil)

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 768, 1024), nil)







// draw the custom background view to display the text and image in pdf.

func drawBackground () {

let context:CGContextRef = UIGraphicsGetCurrentContext()!

let rect:CGRect = CGRectMake(0, 0, pageSize.width, pageSize.height)

CGContextSetFillColorWithColor(context, UIColor.brownColor().CGColor)

CGContextFillRect(context, rect)


// draw the custom textview to display the text enter into it into pdf.

func drawText(){

let context:CGContextRef = UIGraphicsGetCurrentContext()!

let font = UIFont(name: “HelveticaNeue-UltraLight”, size: CGFloat(20))!

CGContextSetFillColorWithColor(context, UIColor.orangeColor().CGColor)

let textRect : CGRect = CGRectMake(200, 350, ((self.txtView).frame).size.width, ((self.txtView).frame).size.height)

let myString : NSString = self.txtView.text

let paraStyle = NSMutableParagraphStyle()

paraStyle.lineSpacing = 6.0

let fieldFont = UIFont(name: “Helvetica Neue”, size: 30)

let parameters: NSDictionary = [ NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle ,NSFontAttributeName: fieldFont!]

myString.drawInRect(textRect, withAttributes: parameters as? [String : AnyObject])


// draw the custom image to display into pdf with the given text you enter into textview

func drawImage(){

let imgRect:CGRect = CGRectMake(284, 50, 200, 200)

let image : UIImage = UIImage(named:“logo.png”)!



override func didReceiveMemoryWarning() {


// Dispose of any resources that can be recreated.

} }

You can download Demo project from given link below :

How To Convert Text Content Into Pdf File In Swift 2.0

Post navigation

0 0 vote
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x