PDF Generation in Swift (iOS/macOS): A Complete Guide
Creating PDF documents is a common task in iOS and macOS applications: from generating reports and invoices to exporting data and creating e-books. In Swift, there are several built-in and third-party tools for this. In this article, we will thoroughly examine the main approaches to PDF generation on Apple platforms, their capabilities, and usage examples.
Built-in Apple Frameworks
The Apple ecosystem provides powerful tools for working with PDFs without third-party dependencies. The main ones are:
- UIGraphicsPDFRenderer (iOS 10+) — a modern API for creating PDFs in UIKit.
- PDFKit — a framework for displaying, editing, and creating PDFs (available on iOS 11+ and macOS 10.13+).
- Core Graphics (Quartz 2D) — a low-level C-based API for drawing and generating PDFs.
1. UIGraphicsPDFRenderer — Simple and Fast Start
This is the most convenient method for iOS. It allows you to create multi-page PDFs by drawing with UIKit (UIView, UIImage, text, etc.).
import UIKit
func generatePDF() -> Data? { let pageSize = CGRect(x: 0, y: 0, width: 595.2, height: 841.8) // A4 let renderer = UIGraphicsPDFRenderer(bounds: pageSize)
let data = renderer.pdfData { context in context.beginPage()
// Draw text let text = "Hello, PDF World!" let attributes: [NSAttributedString.Key: Any] = [ .font: UIFont.systemFont(ofSize: 24), .foregroundColor: UIColor.black ] text.draw(at: CGPoint(x: 50, y: 100), withAttributes: attributes)
// Draw image if let image = UIImage(named: "logo") { image.draw(in: CGRect(x: 50, y: 200, width: 200, height: 100)) }
// Second page context.beginPage() let text2 = "This is the second page" text2.draw(at: CGPoint(x: 50, y: 50), withAttributes: attributes) }
return data}
// Save to fileif let pdfData = generatePDF() { let documentsPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let pdfURL = documentsPath.appendingPathComponent("document.pdf") try? pdfData.write(to: pdfURL) print("PDF saved: \\(pdfURL)")}2. PDFKit — For Advanced Document Work
PDFKit provides the PDFDocument and PDFPage classes. It is ideal for creating PDFs from formatted text, tables, and images. It supports working with annotations, bookmarks, and forms.
import PDFKit
func createPDFWithPDFKit() -> PDFDocument { let document = PDFDocument()
// Create a page via NSAttributedString let pageRect = CGRect(x: 0, y: 0, width: 612, height: 792) // US Letter let text = NSAttributedString(string: "PDF example with PDFKitSecond line") let page = PDFPage()
// Render into the page context let renderer = UIGraphicsImageRenderer(bounds: pageRect) let image = renderer.image { ctx in text.draw(at: CGPoint(x: 72, y: 72)) }
// Create a page from the image (simplified) // In a real project, use PDFPage with drawBlock let finalPage = PDFPage(image: image) ?? PDFPage() document.insert(finalPage, at: 0)
return document}
// Saveif let document = createPDFWithPDFKit() as? PDFDocument { let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("pdfkit_example.pdf") document.write(to: url)}Important: PDFKit is only available on iOS 11+/macOS 10.13+. For older versions, use UIGraphicsPDFRenderer.
3. Core Graphics (Quartz 2D) — Maximum Control
If you
Also in library
PM2: Monitoring and Managing JavaScript Applications
C++ Application Configuration: Libraries and Approaches
Compression for JavaScript (Node.js): Complete Guide with Examples
Working with Excel in TypeScript: Libraries, Tools, and Code Examples