Create PDF view programmatically using PDFKit in swift.

Updated 2 December 2020


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.


PDF Screenshot


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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


  • Dipesh
    • Debabrata Roy (Moderator)
      • Ronald
        • Debabrata Roy (Moderator)
  • Hubert
    • Debabrata Roy (Moderator)
      • Andrew
        • Debabrata Roy (Moderator)
  • Esteban
    • Debabrata Roy (Moderator)
  • Debabrata Roy (Moderator)
    • Debabrata Roy (Moderator)
      • Debabrata Roy (Moderator)
  • Björn
    • Debabrata Roy (Moderator)
  • Start a Project

      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home