BLUF: SmartSlim SDK exports dynamic libraries via standard C ABI, supporting Python/Java/C#/Rust integration. The core call flow has four steps — initialize SDK, create compression config, execute compression, release resources. Single-file compression of a 100MB PDF takes about 0.8s, with license verification latency under 5ms. This article provides complete integration code examples for three languages, API parameter tables, performance tuning strategies, and a batch compression demo. Below, we start with the SDK architecture and detail each language's integration method.
If you need to call compression services via HTTP API instead of SDK integration, we recommend readingCompression API Guide: RESTful Interface Documentation。
1. SDK Architecture and Integration Principles
SmartSlim SDK's foundation is the Rust compression engine, exported as dynamic libraries (.so/.dylib/.dll) via standard C ABI (Application Binary Interface). Any language supporting FFI (Foreign Function Interface) can load and call it. The benefit of this architecture: write the core engine once in Rust, reuse the same binary library across multiple languages, without reimplementing compression logic in each language.
| Component | Technology | Function | Output |
|---|---|---|---|
| Compression Engine Core | Rust | Execute compression algorithms | Static library .a/.lib |
| ABI Export Layer | Rust #[no_mangle] | Export C-compatible functions | Dynamic library .so/.dylib/.dll |
| Language Binding Layer | Language FFI | Wrap native calls | Language-specific packages/libraries |
| License Module | Rust + encryption | Verify license key | Embedded in dynamic library |
| Config Management | Rust struct | Compression parameter management | JSON/TOML config |
The SDK provides 6 platform pre-compiled libraries, covering mainstream operating systems and CPU architectures, ready to use without compiling Rust code yourself.
| Platform | Dynamic Library | OS | CPU Architecture | Use Case |
|---|---|---|---|---|
| linux-x64 | libsmartslim.so | Linux | x86_64 | Server/Desktop |
| linux-arm64 | libsmartslim.so | Linux | aarch64 | ARM server/Raspberry Pi |
| macos-universal | libsmartslim.dylib | macOS | x86_64+arm64 | Intel + Apple Silicon |
| windows-x64 | smartslim.dll | Windows | x86_64 | Windows desktop/server |
| linux-server-x64 | libsmartslim.so | Linux | x86_64 | Server optimized |
| linux-server-arm64 | libsmartslim.so | Linux | aarch64 | ARM server optimized |
2. Core API Parameter Reference
The SDK's core APIs are divided into four categories: initialization, configuration, compression, and release. The table below lists the main API functions and their parameters.
| API Function | Parameters | Return Value | Description |
|---|---|---|---|
| smartslim_init | license_key: String | handle: Handle | Initialize SDK, verify license |
| smartslim_create_config | format, level, quality | config: ConfigHandle | Create compression config |
| smartslim_compress | handle, input, output, config | result: ResultCode | Execute single-file compression |
| smartslim_compress_batch | handle, files[], config, workers | results[]: ResultCode | Batch compression (multi-concurrent) |
| smartslim_get_info | handle, filepath | FileInfo: struct | Get file compression info |
| smartslim_free_config | config: ConfigHandle | void | Release config resources |
| smartslim_free | handle: Handle | void | Release SDK instance |
Compression configuration parameters support 4 compression levels (low/medium/high/ultra) x 15 scenarios x 5 performance tiers (ultrafast/fast/balanced/quality/optimal), totaling 300 combinations, covering virtually all compression needs.
| Parameters | Options | Default | Description |
|---|---|---|---|
| format | pdf/image/video/office/ofd | Auto-detect | File type |
| level | low/medium/high/ultra | medium | Compression level |
| quality | 1-100 | 75 | Quality factor (image/PDF) |
| performance | ultrafast/fast/balanced/quality/optimal | balanced | Performance tier |
| workers | 1-16 | 4 | Concurrent threads |
| security | DISABLED/LOW/MEDIUM/HIGH/MAXIMUM | MEDIUM | Security level |
3. Code Examples for Three Languages
Below are complete integration examples for Python, Java, and C#, all demonstrating compression of a single PDF file.
1. Python Integration (ctypes)
Python loads the dynamic library via the ctypes module and calls exported functions. The following code implements the complete flow of initializing the SDK, creating config, compressing PDF, and releasing resources. Compressing a 100MB PDF takes about 0.8s.
| Step | Python Code | Description |
|---|---|---|
| 1.Load library | lib = ctypes.CDLL('./libsmartslim.so') | Load dynamic library |
| 2.Initialize | handle = lib.smartslim_init(b"YOUR_LICENSE") | Pass license key |
| 3.Create config | config = lib.smartslim_create_config(b"pdf", b"medium", 75) | PDF/medium/q75 |
| 4.Execute compression | lib.smartslim_compress(handle, b"in.pdf", b"out.pdf", config) | Compress file |
| 5.Release resources | lib.smartslim_free_config(config); lib.smartslim_free(handle) | Release memory |
2. Java Integration (JNI)
Java calls the dynamic library via JNI (Java Native Interface). You need to write native method declarations first, then load the library file. The following code shows the core call flow, suitable for Spring Boot and other Java backend project integration.
| Step | Java Code | Description |
|---|---|---|
| 1.Load library | System.loadLibrary("smartslim") | Load DLL/SO |
| 2.Declare method | native long smartslim_init(String key) | JNI method declaration |
| 3.Initialize | long handle = smartslim_init("YOUR_LICENSE") | Get instance handle |
| 4.Create config | long config = smartslim_create_config("pdf","medium",75) | Config parameters |
| 5.Execute compression | smartslim_compress(handle, "in.pdf", "out.pdf", config) | Compress file |
| 6.Release resources | smartslim_free_config(config); smartslim_free(handle) | Release memory |
3. C# Integration (P-Invoke)
C# calls the dynamic library via P/Invoke (Platform Invocation Services). Use the DllImport attribute to declare external functions, suitable for .NET/.NET Core project integration. The following code supports both Windows and Linux platforms.
| Step | C# Code | Description |
|---|---|---|
| 1.Declare function | [DllImport("smartslim")] static extern IntPtr smartslim_init(string key) | P-Invoke declaration |
| 2.Initialize | IntPtr handle = smartslim_init("YOUR_LICENSE") | Get handle |
| 3.Create config | IntPtr config = smartslim_create_config("pdf","medium",75) | Config parameters |
| 4.Execute compression | smartslim_compress(handle, "in.pdf", "out.pdf", config) | Compress file |
| 5.Release resources | smartslim_free_config(config); smartslim_free(handle) | Release memory |
4. Performance Tuning and Batch Compression
SDK integration performance tuning focuses on three dimensions: concurrency, memory, and batch processing. The table below compares different tuning strategies.
| Tuning Strategy | Single-file Time | 100-file Total Time | Memory Usage | License Tier |
|---|---|---|---|---|
| Single-threaded serial | 0.8s | 80s | 50MB | Trial (1 concurrent) |
| 4-thread concurrent | 0.8s | 22s | 180MB | Standard (4 concurrent) |
| 8-thread concurrent | 0.8s | 12s | 320MB | Professional (8 concurrent) |
| 16-thread concurrent | 0.8s | 7s | 600MB | Enterprise (16+ concurrent) |
| Streaming compression mode | 1.2s | 95s | 15MB | All licenses |
For batch compression, we recommend using the SDK's built-in smartslim_compress_batch interface, passing a file list and concurrency count. The SDK uses Rust async task scheduling internally, which is more efficient than implementing multi-threading in each language. Below is performance data from a batch compression demo of 100 PDF files.
| File Count | Avg per File | Total Size | Compressed Size | 8-concurrent Time | Ratio |
|---|---|---|---|---|---|
| 100 PDFs | 15MB | 1.5GB | 320MB | 12s | 78.7% |
| 100 PNGs | 8MB | 800MB | 180MB | 8s | 77.5% |
| 100 DOCX | 5MB | 500MB | 120MB | 6s | 76.0% |
| 100 MP4s | 50MB | 5GB | 1.8GB | 45s | 64.0% |
If you need to understand the underlying algorithm comparison of Rust compression libraries, seeRust Compression Library Comparison: Why Choose Rust for Compression Engines. If you need to deploy compression services via Docker containers, seeDocker Compression Service Deployment。
5. FAQ
Q1: Which programming languages does the compression SDK support?
SmartSlim SDK exports dynamic libraries via standard C ABI, supporting Python (ctypes), Java (JNI), C# (P-Invoke), and Rust (FFI) integration. The SDK provides 6 platform pre-compiled libraries (linux-x64/arm64, macos-universal, windows-x64, etc.), ready to use without compilation. Other languages supporting C ABI calls (such as Go, Node.js, PHP) can also integrate through their respective FFI mechanisms.
Q2: What is the API call flow for the compression SDK?
The standard call flow has four steps: 1. Call smartslim_init to initialize the SDK and pass in the license key; 2. Call smartslim_create_config to create compression configuration (specifying format, level, performance tier); 3. Call smartslim_compress to execute compression (passing input and output file paths); 4. Call smartslim_free to release resources. Single-file compression of a 100MB PDF takes about 0.8s, with license verification latency under 5ms.
Q3: How to implement batch compression with the SDK?
Batch compression has two approaches: first, loop through the single-file compression interface with multi-threaded concurrency (Python uses concurrent.futures, Java uses ThreadPoolExecutor); second, use the SDK's built-in batch compression interface smartslim_compress_batch, passing a file list and concurrency count, with the SDK handling Rust async task scheduling internally. The second approach is recommended — Standard license supports 4 concurrent, Professional supports 8, and Enterprise supports 16+.
Q4: What to do when SDK integration encounters errors?
Common errors and solutions: 1. UnsatisfiedLinkError (Java) or DLL load failure — check if the dynamic library path and system architecture match; 2. License verification failure — check if the license key is correct and not expired; 3. Out of memory — reduce concurrency or enable streaming compression mode; 4. Unsupported format — check if the SDK version supports the target format. It is recommended to enable DEBUG logging to diagnose issues, or contact SmartSlim technical support.
Summary
SmartSlim SDK achieves write-once, call-from-any-language via standard C ABI, supporting Python/Java/C#/Rust integration. The core call flow has four steps: initialize, create config, execute compression, release resources. Single-file compression of a 100MB PDF takes 0.8s, and batch compression of 100 files with 8 concurrent takes only 12s. Performance tuning has three dimensions: concurrency (4–16 threads), memory (streaming mode reduces 75% usage), and batch processing (built-in batch interface).
Remember three points: first, select the pre-compiled library matching your system architecture (6 platforms fully covered); second, prefer the built-in batch interface over custom multi-threading for batch compression; third, for production environments, Professional or higher license (8 concurrent) is recommended to balance performance and cost. If you prefer HTTP API calls over SDK integration, see the Compression API Guide.
Related Articles
Need to Compress Files? Try SmartSlim
Built on a self-developed Rust compression engine, supporting 10 categories and 40+ formats including PDF, images, video, Office, and OFD, with local compression that keeps your data on-premises.