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.

What is One PDFGenerator?

PDFGenerator is a powerful open source Swift free library designed to help developers generate PDF files seamlessly within iOS and macOS applications. Written in Swift 4, this lightweight yet efficient PDF generator provides a straightforward and intuitive Swift API PDF solution for creating professional-quality PDF documents without relying on Adobe Acrobat. Hosted on GitHub and licensed under MIT, PDFGenerator is completely free to use, modify, and integrate into commercial or personal projects. With advanced error handling and strong performance, the library ensures reliability and consistency in every generated document, making it a trusted tool for Swift developers.

Packed with robust features, PDFGenerator simplifies common PDF operations such as adding text, images, and tables, formatting layouts, setting margins, inserting page numbers, adding headers, footers, and watermarks. Its well-structured API allows developers to create, manipulate, and manage PDF documents with just a few lines of Swift code. Though primarily focused on PDF creation, the library's extensibility opens doors for tasks like Split/Merge Multiple PDFs when integrated with complementary tools. Whether you're building a content-rich app or automating document workflows, PDFGenerator helps you efficiently generate PDF files and streamline PDF document management in any Swift-based environment.

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.

How to 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 API?

// 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.

How to 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)
    }
}

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.

 English