Skip to main content

Crate libreoffice_convert_rust

Crate libreoffice_convert_rust 

Source
Expand description

§LibreOffice Convert Rust

一个用于将文档转换为各种格式的 Rust 库,基于 LibreOffice。

§功能特性

  • 支持多种文档格式转换(如 docx → pdf)
  • 自动检测 LibreOffice 安装路径
  • 支持自定义转换选项
  • 异步 API 设计
  • 完善的错误处理

§快速开始

use libreoffice_convert_rust::{convert, ConvertOptions};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 读取文档
    let document = std::fs::read("document.docx")?;
     
    // 转换为 PDF
    let pdf_data = convert(&document, "pdf", None).await?;
     
    // 保存结果
    std::fs::write("output.pdf", pdf_data)?;
     
    Ok(())
}

§使用自定义选项

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 {
        async_times: 5,
        async_interval: 300,
        tmp_prefix: Some("my_convert_".to_string()),
        soffice_binary_paths: Some(vec![
            "/custom/path/to/soffice".to_string()
        ]),
    };
     
    let pdf_data = convert_with_options(
        &document,
        "pdf",
        None,
        options
    ).await?;
     
    Ok(())
}

Structs§

ConvertOptions
转换选项配置

Enums§

ConvertError
文档转换过程中可能出现的错误类型

Functions§

convert
简化的文档转换函数(使用默认选项)
convert_file
将文件转换为另一种格式
convert_file_with_options
使用自定义选项将文件转换为另一种格式
convert_with_options
使用自定义选项进行文档转换
find_soffice
查找 LibreOffice 可执行文件路径

Type Aliases§

ConvertResult
转换操作的统一结果类型