CocoaPods trunk is moving to be read-only. Read more on the blog, there are 9 months to go.

PDFCompressorKit 1.1.0

PDFCompressorKit 1.1.0

Maintained by Ghuffran Asghar.



  • By
  • Ghuffran Asghar

PDFCompressorKit

Platform Swift License SPM

A powerful, cross-platform Swift package for PDF compression
Works seamlessly on both iOS (UIKit) and macOS (AppKit)


โœจ Features

  • ๐ŸŽ 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

๐Ÿ“ฆ Installation

Swift Package Manager

Add PDFCompressorKit to your project using Xcode:

  1. Go to File โ†’ Add Package Dependencies
  2. Enter the repository URL:
    https://github.com/YOUR_USERNAME/PDFCompressorKit.git
    
  3. 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"]
    )
]

๐Ÿš€ Quick Start

import PDFCompressorKit

let compressor = PDFCompressor()

// Simple compression
try compressor.compress(
    inputURL: sourceURL,
    outputURL: outputURL,
    level: .medium
)

๐Ÿ“– Usage

Compression Levels

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

Basic Compression

import PDFCompressorKit

let compressor = PDFCompressor()

do {
    try compressor.compress(
        inputURL: inputURL,
        outputURL: outputURL,
        level: .medium
    )
    print("โœ… Compression complete!")
} catch {
    print("โŒ Error: \(error.localizedDescription)")
}

Async Compression with Progress

try await compressor.compressAsync(
    inputURL: inputURL,
    outputURL: outputURL,
    level: .high
) { progress in
    // Update your UI
    print("Progress: \(Int(progress * 100))%")
}

In-Memory Compression

// From PDFDocument
let compressedData = try compressor.compress(
    document: pdfDocument,
    level: .medium
)

// From Data
let compressedData = try compressor.compress(
    data: pdfData,
    level: .high
)

Custom Compression Settings

// 50% JPEG quality, 80% resolution
try compressor.compress(
    inputURL: inputURL,
    outputURL: outputURL,
    level: .custom(quality: 0.5, scale: 0.8)
)

Get PDF Information

let info = try compressor.getPDFInfo(url: pdfURL)
print("๐Ÿ“„ Pages: \(info["pageCount"]!)")
print("๐Ÿ’พ Size: \(info["fileSizeFormatted"]!)")

๐ŸŽฏ Real-World Example

iOS/macOS App Integration

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
    }
}

๐Ÿ› ๏ธ API Reference

PDFCompressor

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

PDFCompressorError

Error Description
.invalidPDF Input PDF cannot be read
.cannotCreateOutput Output file creation failed
.compressionFailed Compression processing error
.fileNotFound Input file not found

๐Ÿ“‹ Requirements

  • iOS 13.0+
  • macOS 10.15+
  • Swift 5.5+
  • Xcode 13.0+

๐Ÿ“„ License

PDFCompressorKit is available under the MIT license. See the LICENSE file for more info.


๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.


Made with โค๏ธ by GhuffraanAsghar