libreoffice_convert_rust

Crates.iolibreoffice_convert_rust
lib.rslibreoffice_convert_rust
version0.1.0
created_at2026-01-02 18:23:32.723411+00
updated_at2026-01-02 18:23:32.723411+00
descriptionA Rust library for converting documents using LibreOffice
homepage
repositoryhttps://gitcode.com/dnrops/libreoffice_convert_rust
max_upload_size
id2019015
size51,926
zinso (zinso)

documentation

README

LibreOffice Convert Rust

A Rust library for converting documents using LibreOffice. Supports converting various document formats to PDF and other formats.

Features

  • 🚀 Support for multiple document formats (docx, xlsx, pptx, odt, ods, odp, etc.)
  • 📝 Automatic detection of LibreOffice installation path
  • ⚙️ Customizable conversion options (retry count, interval time, etc.)
  • 🔧 Async API design powered by Tokio
  • 🛡️ Comprehensive error handling
  • 💻 Command-line tool included

Installation

As a Library

Add dependency to your Cargo.toml:

[dependencies]
libreoffice_convert_rust = "0.1.0"
tokio = { version = "1", features = ["full"] }

Command-line Tool

cargo build --release

The compiled binary will be located at target/release/libreoffice_convert.exe (Windows) or target/release/libreoffice_convert (Linux/Mac).

Configuration

Environment Variables

Set the LibreOffice executable path (optional):

Windows PowerShell:

$env:LIBRE_OFFICE_EXE = "E:/DATA/libreeoffice/program/soffice.exe"

Linux/Mac Bash:

export LIBRE_OFFICE_EXE="/usr/bin/soffice"

If the environment variable is not set, the tool will automatically search these default paths:

  • Windows: C:\Program Files\LibreOffice\program\soffice.exe
  • Windows: C:\Program Files (x86)\LibreOffice\program\soffice.exe
  • Linux: /usr/bin/libreoffice, /usr/bin/soffice
  • Mac: /Applications/LibreOffice.app/Contents/MacOS/soffice

Usage

Command-line Tool

# Basic usage
cargo r input.docx output.pdf pdf

# Or use the compiled binary
./libreoffice_convert.exe input.xlsx output.pdf pdf

# Run with environment variable set
$env:LIBRE_OFFICE_EXE = "E:/DATA/libreeoffice/program/soffice.exe"
cargo r E:/Downloads/demo.xlsx E:/Downloads/demo.pdf pdf

Arguments

  • input_path: Input file path
  • output_path: Output file path
  • format: Target format (pdf, docx, txt, etc.)

As a Library

Basic Conversion

use libreoffice_convert_rust::{convert, ConvertOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Read document
    let document = std::fs::read("document.docx")?;
    
    // Convert to PDF
    let pdf_data = convert(&document, "pdf", None).await?;
    
    // Save result
    std::fs::write("output.pdf", pdf_data)?;
    
    Ok(())
}

With Custom Options

use libreoffice_convert_rust::{convert_with_options, ConvertOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let document = std::fs::read("document.docx")?;
    
    let options = ConvertOptions::new()
        .with_async_times(5)
        .with_async_interval(300)
        .with_input_format("docx");
    
    let pdf_data = convert_with_options(&document, "pdf", None, options).await?;
    
    Ok(())
}

File to File Conversion

use libreoffice_convert_rust::convert_file;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    convert_file("input.docx", "output.pdf", "pdf", None).await?;
    Ok(())
}

Convert Excel Files

use libreoffice_convert_rust::convert_file;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Convert Excel to PDF
    convert_file("demo.xlsx", "demo.pdf", "pdf", None).await?;
    Ok(())
}

API Documentation

ConvertOptions

Configuration options for document conversion.

pub struct ConvertOptions {
    pub async_times: u32,           // Maximum retry attempts (default: 3)
    pub async_interval: u64,         // Retry interval in milliseconds (default: 200)
    pub tmp_prefix: Option<String>,   // Temporary directory prefix
    pub soffice_binary_paths: Option<Vec<String>>, // Custom LibreOffice paths
    pub input_format: Option<String>, // Input file format
}

Main Functions

convert

Simple document conversion function with default options.

pub async fn convert(
    document: &[u8],
    format: &str,
    filter: Option<&str>,
) -> ConvertResult<Vec<u8>>

convert_with_options

Document conversion with custom options.

pub async fn convert_with_options(
    document: &[u8],
    format: &str,
    filter: Option<&str>,
    options: ConvertOptions,
) -> ConvertResult<Vec<u8>>

convert_file

Convert a file to another format.

pub async fn convert_file(
    input_path: &str,
    output_path: &str,
    format: &str,
    filter: Option<&str>,
) -> ConvertResult<()>

Supported Formats

Input Formats

  • Word: .doc, .docx, .odt, .rtf
  • Excel: .xls, .xlsx, .ods, .csv
  • PowerPoint: .ppt, .pptx, .odp
  • Others: .txt, .html, .xml

Output Formats

  • PDF: pdf
  • Word: docx
  • Excel: xlsx
  • PowerPoint: pptx
  • Text: txt
  • HTML: html

Note: Image files (png, jpg, gif, etc.) conversion is not supported because LibreOffice's --convert-to command is designed for document formats.

Error Handling

The library provides comprehensive error types:

pub enum ConvertError {
    InvalidInput(String),
    FileReadFailed(String),
    FileWriteFailed(String),
    ProcessExecutionFailed(String),
    ProcessFailed { code: Option<i32>, stderr: String },
    ConversionFailed(String),
    SofficeNotFound,
    TempDirCreationFailed(String),
    InvalidPath(String),
}

Example Project

Check src/main.rs for a complete command-line tool implementation example.

Dependencies

System Requirements

  • Rust 1.70 or higher
  • LibreOffice 7.0 or higher (must be installed)

Troubleshooting

LibreOffice Not Found

If you see a "LibreOffice not found" error:

  1. Ensure LibreOffice is properly installed
  2. Set the LIBRE_OFFICE_EXE environment variable to point to the soffice.exe path
  3. Or use the with_soffice_paths method in ConvertOptions to specify the path

Conversion Failed

  • Ensure the input file format is supported
  • Check if the file is corrupted
  • View stderr output for detailed error information
  • Try increasing the retry count and interval time

License

MIT License

Contributing

Issues and Pull Requests are welcome!

Authors

LibreOffice Convert Rust Contributors

Commit count: 0

cargo fmt