Compression SDK Integration Guide: Python/Java/C# Multi-Language Calls

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.

ComponentTechnologyFunctionOutput
Compression Engine CoreRustExecute compression algorithmsStatic library .a/.lib
ABI Export LayerRust #[no_mangle]Export C-compatible functionsDynamic library .so/.dylib/.dll
Language Binding LayerLanguage FFIWrap native callsLanguage-specific packages/libraries
License ModuleRust + encryptionVerify license keyEmbedded in dynamic library
Config ManagementRust structCompression parameter managementJSON/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.

PlatformDynamic LibraryOSCPU ArchitectureUse Case
linux-x64libsmartslim.soLinuxx86_64Server/Desktop
linux-arm64libsmartslim.soLinuxaarch64ARM server/Raspberry Pi
macos-universallibsmartslim.dylibmacOSx86_64+arm64Intel + Apple Silicon
windows-x64smartslim.dllWindowsx86_64Windows desktop/server
linux-server-x64libsmartslim.soLinuxx86_64Server optimized
linux-server-arm64libsmartslim.soLinuxaarch64ARM 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 FunctionParametersReturn ValueDescription
smartslim_initlicense_key: Stringhandle: HandleInitialize SDK, verify license
smartslim_create_configformat, level, qualityconfig: ConfigHandleCreate compression config
smartslim_compresshandle, input, output, configresult: ResultCodeExecute single-file compression
smartslim_compress_batchhandle, files[], config, workersresults[]: ResultCodeBatch compression (multi-concurrent)
smartslim_get_infohandle, filepathFileInfo: structGet file compression info
smartslim_free_configconfig: ConfigHandlevoidRelease config resources
smartslim_freehandle: HandlevoidRelease 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.

ParametersOptionsDefaultDescription
formatpdf/image/video/office/ofdAuto-detectFile type
levellow/medium/high/ultramediumCompression level
quality1-10075Quality factor (image/PDF)
performanceultrafast/fast/balanced/quality/optimalbalancedPerformance tier
workers1-164Concurrent threads
securityDISABLED/LOW/MEDIUM/HIGH/MAXIMUMMEDIUMSecurity 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.

StepPython CodeDescription
1.Load librarylib = ctypes.CDLL('./libsmartslim.so')Load dynamic library
2.Initializehandle = lib.smartslim_init(b"YOUR_LICENSE")Pass license key
3.Create configconfig = lib.smartslim_create_config(b"pdf", b"medium", 75)PDF/medium/q75
4.Execute compressionlib.smartslim_compress(handle, b"in.pdf", b"out.pdf", config)Compress file
5.Release resourceslib.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.

StepJava CodeDescription
1.Load librarySystem.loadLibrary("smartslim")Load DLL/SO
2.Declare methodnative long smartslim_init(String key)JNI method declaration
3.Initializelong handle = smartslim_init("YOUR_LICENSE")Get instance handle
4.Create configlong config = smartslim_create_config("pdf","medium",75)Config parameters
5.Execute compressionsmartslim_compress(handle, "in.pdf", "out.pdf", config)Compress file
6.Release resourcessmartslim_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.

StepC# CodeDescription
1.Declare function[DllImport("smartslim")] static extern IntPtr smartslim_init(string key)P-Invoke declaration
2.InitializeIntPtr handle = smartslim_init("YOUR_LICENSE")Get handle
3.Create configIntPtr config = smartslim_create_config("pdf","medium",75)Config parameters
4.Execute compressionsmartslim_compress(handle, "in.pdf", "out.pdf", config)Compress file
5.Release resourcessmartslim_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 StrategySingle-file Time100-file Total TimeMemory UsageLicense Tier
Single-threaded serial0.8s80s50MBTrial (1 concurrent)
4-thread concurrent0.8s22s180MBStandard (4 concurrent)
8-thread concurrent0.8s12s320MBProfessional (8 concurrent)
16-thread concurrent0.8s7s600MBEnterprise (16+ concurrent)
Streaming compression mode1.2s95s15MBAll 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 CountAvg per FileTotal SizeCompressed Size8-concurrent TimeRatio
100 PDFs15MB1.5GB320MB12s78.7%
100 PNGs8MB800MB180MB8s77.5%
100 DOCX5MB500MB120MB6s76.0%
100 MP4s50MB5GB1.8GB45s64.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.

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.