1. Products
  2.   PDF
  3.   Swift
  4.   PDFGenerator
 
  

Swift PDF Generator for PDF Creation & Processing

Open Source Swift PDF Generator API that enables software developers to Create, Edit, Manipulate, & Render PDF Documents with ease.

PDFGenerator is an open source Swift library that allows software developers to generate PDF documents inside their own Swift applications. It is written in Swift 4 and offers a simple API that makes it easy to create PDF documents programmatically without using Adobe Acrobat. The library is available on GitHub under the MIT License, making it free to use and modify for any purpose. The library is also highly reliable, with robust error handling and recovery mechanisms that ensure that PDF documents are generated correctly and consistently.

PDFGenerator is very easy to use and offers excellent performance as well as reliability making it easy for developers to create PDF documents with just a couple of lines of code. The library provides various important features for working with PDF documents, such as adding text to PDF, inserting images to PDF pages, adding tables, specifying PDF page layout, adding formatting to PDF documents, setting page margins, adding headers and footers to PDF, insert page numbering, add watermarks, and many more.

The library provides a simple and intuitive API that makes it easy to generate PDF documents without having to deal with the complexities of PDF file format. The library has been extensively tested and optimized for performance, ensuring that it can handle even large and complex PDF documents with ease. Whether you are developing an iOS or macOS application, PDFGenerator is a great that can simplify the process of generating PDF documents and save you time and effort.

Previous Next

Getting Started with PDFGenerator

The recommend way to install PDFGenerator is using CocoaPods. Please use the following command for a smooth installation.

Install PDFGenerator via CocoaPods

pod 'PDFGenerator', '~> 3.1'

pod install 

You can download it directly from GitHub.

Generate PDF Documents via Swift API

The open source PDFGenerator library enables software developers to create new PDF documents inside their own Swift Apps. The library has included support for various important features related to PDF file management, such as multi-page documents creation, creating interactive PDF documents, inserting new pages to existing PDFs, inserting headers and footers, page numbering support, adding text and image watermarks, PDF annotations (links, comments, and form fields), generating reports and many more.

Generate PDF from Image(s) via Swift API

public enum PDFPage {
    case whitePage(CGSize) // = A white view
    case view(UIView)
    case image(UIImage)
    case imagePath(String)
    case binary(Data)
    case imageRef(CGImage)
}

func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.View(v1)
    let page2 = PDFPage.View(v2)
    let page3 = PDFPage.WhitePage(CGSizeMake(200, 100))
    let page4 = PDFPage.Image(UIImage(contentsOfFile: "path/to/image1.png")!)
    let page5 = PDFPage.ImagePath("path/to/image2.png")
    let pages = [page1, page2, page3, page4, page5]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst)
    } catch (let e) {
        print(e)
    }
}

Password Protect PDF Files via Swift API

The PDFGenerator library has included complete support for creating Password protected PDF documents inside the Swift library. The Library makes it easy to add password protection to PDF files in various ways, such as setting a password for the PDF file, setting a password for a particular page, password protection (with string), using PDFPassword model, and many more. The following example shows how software developers can create and generate a PDF document with password inside Swift applications.

How to Generate PDF with password via Swift?

// generate PDF with password: 123456
func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.view(v1)
    let page2 = PDFPage.view(v2)
    let pages = [page1, page2]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst, password: "123456")
        // or use PDFPassword model
        try PDFGenerator.generate(pages, to: dst, password: PDFPassword("123456"))
        // or use PDFPassword model and set user/owner password
        try PDFGenerator.generate(pages, to: dst, password: PDFPassword(user: "123456", owner: "abcdef"))
    } catch let error {
        print(error)
    }
}

Generate Custom DPI PDF via Swift Library

Custom DPI (dots per inch) refers to the resolution at which an image or document is printed or displayed. DPI is a measure of the density of the pixels or dots that make up an image, with higher DPI resulting in a higher quality and more detailed image. The open source PDFGenerator Swift library enables software developers to generate PDF documents programmatically inside Swift applications. To generate a custom DPI PDF using PDFGenerator, developers can use the following code example.

Extract Metadata from PDF Document

PDFGenerator library gives software developers the power to read & extract metadata from a PDF document via PHP. Metadata includes very important information about the PDF document. The library supports metadata like Creator, Keywords, Subject, Author, Title, ModDate, CreationDate, and Trapped.

Generate Custom DPI PDF File using Swift API

// generate dpi300 PDF (default: 72dpi)
func generatePDF() {
    let v1 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 100.0))
    v1.backgroundColor = .red
    let v2 = UIView(frame: CGRect(x: 0.0,y: 0, width: 100.0, height: 200.0))
    v2.backgroundColor = .green

    let page1 = PDFPage.View(v1)
    let page2 = PDFPage.View(v2)
    let pages = [page1, page2]

    let dst = NSTemporaryDirectory().appending("sample1.pdf")
    do {
        try PDFGenerator.generate(pages, to: dst, dpi: .dpi_300)
    } catch (let e) {
        print(e)
    }
}
 English