Start a Project

Create PDF view programmatically using PDFKit in swift.

In the article, I will discuss, how we can create PDF programmatically using the PDFKit framework.

First of all, The PDFKit is a framework available in AppKit since iOS 11.0 and macOS 10.4. Please click here to read more about PDFKit.

PDFView is mainly used to show the pdf file in our application and also it allows users to set zoom level, select content, navigate through a document, and copy the textual content to the Pasteboard, and also keeps track of page history.

Getting Started

Swift version: 5
iOS version: 13
Xcode: 11.3

First Create a project from Xcode.

File ▸ New ▸ Project…. Choose the iOS ▸ Application ▸ Single View App template and create a new project.

Then create a PDFCreator class where we will add all functions and variables which helps us to create the PDF.

Now we will add basic variables that manage the PDF layout.

pageWidth : To define page width of PDF.

pageHeight : To define page Height of PDF.

marginPoint : To define the page margin of PDF.

currentPage : To Store page count of the PDF.

Now create a function that creates the actual PDF data.

Now add a page footer in the PDF.

And, Now show this PDF Data in the ViewController.

 

 

I hope this code will help you better to understand how PDF generates. If you feel any doubt or query please comment below.

Thank you.

Exit mobile version