Generating PDF Reports in Swift: Libraries and Frameworks for iOS/macOS

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating PDF Reports in Swift (iOS/macOS)



Creating PDF documents on Apple platforms is a key task when developing business applications, financial reports, or document management systems. Unlike the web stack, where many third-party libraries are used, the Swift ecosystem offers both powerful built-in frameworks and specialized third-party solutions for working with PDF. In this article, we will thoroughly examine all the main tools for generating PDF reports in Swift for iOS and macOS.



Apple's Built-in Frameworks



Apple provides two main ways to create PDF documents: UIGraphicsPDFRenderer (for UIKit) and PDFKit (for working with existing PDFs). macOS also offers NSGraphicsContext with PDF support.



UIGraphicsPDFRenderer (iOS 10+)



This is the primary tool for generating PDF from UIKit content. It allows you to draw text, images, tables, and arbitrary graphics in the context of a PDF page.



import UIKit

func generateReport(data: String) -> Data { let pageSize = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4 let renderer = UIGraphicsPDFRenderer(bounds: pageSize) return renderer.pdfData { context in context.beginPage() // Title let title = "Sales Report" let attributes: [NSAttributedString.Key: Any] = [ .font: UIFont.boldSystemFont(ofSize: 24), .foregroundColor: UIColor.darkGray ] title.draw(at: CGPoint(x: 50, y: 50), withAttributes: attributes) // Content let textAttributes: [NSAttributedString.Key: Any] = [ .font: UIFont.systemFont(ofSize: 14) ] data.draw(at: CGPoint(x: 50, y: 100), withAttributes: textAttributes) // Separator line context.cgContext.setStrokeColor(UIColor.lightGray.cgColor) context.cgContext.setLineWidth(1) context.cgContext.move(to: CGPoint(x: 50, y: 90)) context.cgContext.addLine(to: CGPoint(x: 545, y: 90)) context.cgContext.strokePath() }}


PDFKit (iOS 11+, macOS 10.13+)



PDFKit is a higher-level framework that allows not only creating but also editing existing PDFs. It supports annotations, forms, bookmarks, and working with multi-page documents.



import PDFKit

func createPDFWithText() -> PDFDocument { let page = PDFPage() let pdfDocument = PDFDocument() // Create a page with text let text = "PDFKit makes it easy to add text and annotations" let textAnnotation = PDFAnnotation( bounds: CGRect(x: 50, y: 700, width: 500, height: 100), forType: .freeText, withProperties: nil ) textAnnotation.contents = text textAnnotation.font = UIFont.systemFont(ofSize: 18) page.addAnnotation(textAnnotation) pdfDocument.insert(page, at: 0) return pdfDocument}


Third-Party Libraries for Swift



Although the built-in tools cover most tasks, there are situations where specialized functionality is needed: working with templates, exporting to PDF from SwiftUI, or supporting complex layouts.



SwiftUI and PDF (iOS 14+, macOS 11+)



Starting with iOS 14, you can convert SwiftUI views to PDF using ImageRenderer:



import SwiftUI

struct ReportView: View { var body: some View { VStack(alignment: .leading) { Text("Monthly Report") .font(.largeTitle) .foregroundColor(.blue) Text("Data for August 2024") .font(.headline) Divider() Text("Revenue: 1,234,567 ₽") .font(.body) } .padding() }}

func generatePDFFromSwiftUI() -> Data { let vie

Recommendations