A powerful, cross-platform Swift package for PDF compression
Works seamlessly on both iOS (UIKit) and macOS (AppKit)
- ๐ Cross-Platform - Works on iOS 13+ and macOS 10.15+
- โก Async Support - Modern Swift concurrency with progress tracking
- ๐๏ธ Flexible Compression - Low, Medium, High, or Custom levels
- ๐ Progress Callbacks - Real-time compression progress updates
- ๐งต Thread Safe - Safe for use across multiple threads
- ๐พ Multiple Input Sources - Compress from URL, Data, or PDFDocument
Add PDFCompressorKit to your project using Xcode:
- Go to File โ Add Package Dependencies
- Enter the repository URL:
https://github.com/YOUR_USERNAME/PDFCompressorKit.git - Select version 1.0.0 or later
Or add it directly to your Package.swift:
dependencies: [
.package(url: "https://github.com/GhuffraanAsghar/PDFCompressorKit.git", from: "1.0.0")
],
targets: [
.target(
name: "YourApp",
dependencies: ["PDFCompressorKit"]
)
]import PDFCompressorKit
let compressor = PDFCompressor()
// Simple compression
try compressor.compress(
inputURL: sourceURL,
outputURL: outputURL,
level: .medium
)| Level | Quality | Scale | Best For |
|---|---|---|---|
.low |
90% | 100% | Archival, printing |
.medium |
60% | 75% | Email, sharing |
.high |
30% | 50% | Web, maximum compression |
.custom(quality:scale:) |
Custom | Custom | Fine-grained control |
import PDFCompressorKit
let compressor = PDFCompressor()
do {
try compressor.compress(
inputURL: inputURL,
outputURL: outputURL,
level: .medium
)
print("โ
Compression complete!")
} catch {
print("โ Error: \(error.localizedDescription)")
}try await compressor.compressAsync(
inputURL: inputURL,
outputURL: outputURL,
level: .high
) { progress in
// Update your UI
print("Progress: \(Int(progress * 100))%")
}// From PDFDocument
let compressedData = try compressor.compress(
document: pdfDocument,
level: .medium
)
// From Data
let compressedData = try compressor.compress(
data: pdfData,
level: .high
)// 50% JPEG quality, 80% resolution
try compressor.compress(
inputURL: inputURL,
outputURL: outputURL,
level: .custom(quality: 0.5, scale: 0.8)
)let info = try compressor.getPDFInfo(url: pdfURL)
print("๐ Pages: \(info["pageCount"]!)")
print("๐พ Size: \(info["fileSizeFormatted"]!)")import SwiftUI
import PDFCompressorKit
struct ContentView: View {
@State private var progress: Double = 0
@State private var isCompressing = false
var body: some View {
VStack {
if isCompressing {
ProgressView(value: progress)
.padding()
Text("\(Int(progress * 100))%")
}
Button("Compress PDF") {
Task {
await compressPDF()
}
}
.disabled(isCompressing)
}
}
func compressPDF() async {
isCompressing = true
let compressor = PDFCompressor()
do {
try await compressor.compressAsync(
inputURL: inputURL,
outputURL: outputURL,
level: .medium
) { prog in
Task { @MainActor in
progress = prog
}
}
} catch {
print("Error: \(error)")
}
isCompressing = false
}
}| Method | Description |
|---|---|
compress(inputURL:outputURL:level:) |
Compress file to file |
compress(document:level:) |
Compress PDFDocument, returns Data |
compress(data:level:) |
Compress PDF data, returns Data |
compressAsync(inputURL:outputURL:level:progress:) |
Async file compression with progress |
compressAsync(document:level:progress:) |
Async document compression with progress |
getPDFInfo(url:) |
Get page count and file size |
estimateCompressedSize(inputURL:level:) |
Estimate final size |
| Error | Description |
|---|---|
.invalidPDF |
Input PDF cannot be read |
.cannotCreateOutput |
Output file creation failed |
.compressionFailed |
Compression processing error |
.fileNotFound |
Input file not found |
- iOS 13.0+
- macOS 10.15+
- Swift 5.5+
- Xcode 13.0+
PDFCompressorKit is available under the MIT license. See the LICENSE file for more info.
Contributions are welcome! Please feel free to submit a Pull Request.
Made with โค๏ธ by GhuffraanAsghar