| Crates.io | libreoffice_convert_rust |
| lib.rs | libreoffice_convert_rust |
| version | 0.1.0 |
| created_at | 2026-01-02 18:23:32.723411+00 |
| updated_at | 2026-01-02 18:23:32.723411+00 |
| description | A Rust library for converting documents using LibreOffice |
| homepage | |
| repository | https://gitcode.com/dnrops/libreoffice_convert_rust |
| max_upload_size | |
| id | 2019015 |
| size | 51,926 |
A Rust library for converting documents using LibreOffice. Supports converting various document formats to PDF and other formats.
Add dependency to your Cargo.toml:
[dependencies]
libreoffice_convert_rust = "0.1.0"
tokio = { version = "1", features = ["full"] }
cargo build --release
The compiled binary will be located at target/release/libreoffice_convert.exe (Windows) or target/release/libreoffice_convert (Linux/Mac).
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:
C:\Program Files\LibreOffice\program\soffice.exeC:\Program Files (x86)\LibreOffice\program\soffice.exe/usr/bin/libreoffice, /usr/bin/soffice/Applications/LibreOffice.app/Contents/MacOS/soffice# 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
input_path: Input file pathoutput_path: Output file pathformat: Target format (pdf, docx, txt, etc.)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(())
}
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(())
}
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(())
}
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(())
}
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
}
convertSimple document conversion function with default options.
pub async fn convert(
document: &[u8],
format: &str,
filter: Option<&str>,
) -> ConvertResult<Vec<u8>>
convert_with_optionsDocument conversion with custom options.
pub async fn convert_with_options(
document: &[u8],
format: &str,
filter: Option<&str>,
options: ConvertOptions,
) -> ConvertResult<Vec<u8>>
convert_fileConvert a file to another format.
pub async fn convert_file(
input_path: &str,
output_path: &str,
format: &str,
filter: Option<&str>,
) -> ConvertResult<()>
.doc, .docx, .odt, .rtf.xls, .xlsx, .ods, .csv.ppt, .pptx, .odp.txt, .html, .xmlpdfdocxxlsxpptxtxthtmlNote: Image files (png, jpg, gif, etc.) conversion is not supported because LibreOffice's --convert-to command is designed for document formats.
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),
}
Check src/main.rs for a complete command-line tool implementation example.
If you see a "LibreOffice not found" error:
LIBRE_OFFICE_EXE environment variable to point to the soffice.exe pathwith_soffice_paths method in ConvertOptions to specify the pathMIT License
Issues and Pull Requests are welcome!
LibreOffice Convert Rust Contributors