Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Installing and Using LibreOffice for Document Processing

Tech 1

Overview of LibreOffice

LibreOffice is a free, open-source office suite compatible with Windows, Linux, and macOS. It includes applications for word processing (Writer), spreadsheets (Calc), presentations (Impress), vector graphics (Draw), formula editing (Math), and database management (Base). The software supports multiple document formats, including OpenDocument Format (ODF) and Microsoft Office formats (DOCX, XLSX, PPTX).

Windows Installation

  1. Download the installer from the official website
  2. Run the installer and follow the setup wizard
  3. Add the installation directory to your system PATH (e.g., D:\LibreOffice\program)
  4. Verify installation with:
soffice --version

Document Conversion Testing

To test Chinese character rendering:

  1. Create a document with mixed Chinese/English content
  2. Convert to PDF:
soffice --headless --convert-to pdf test.doc
  1. Check the output PDF for rendering issues

Common solutions for character issues:

  1. Ensure proper encoding (GBK/UTF-8)
  2. Install Chinese font packages:
apt install ttf-wqy-zenhei fonts-wqy-microhei

Linux Installation Methods

YUM Installation

yum install -y libreoffice

RPM Package Installation

  1. Download required packages:
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm.tar.gz
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm_helppack_zh-CN.tar.gz
  1. Install main package:
tar -zxvf LibreOffice_7.5.4_Linux_x86-64_rpm.tar.gz
cd LibreOffice_7.5.4.2_Linux_x86-64_rpm/RPMS/
rpm -ivh *.rpm
  1. Install language pack and help files

Command Line Usage

Basic Conversion

soffice --headless --convert-to pdf input.doc

Batch Conversion

soffice --headless --convert-to pdf *.doc --outdir /output/path

Python Integration

Simple Python Conversion

import subprocess

def convert_to_pdf(input_file, output_dir):
    cmd = [
        'soffice',
        '--headless',
        '--convert-to', 'pdf',
        input_file,
        '--outdir', output_dir
    ]
    subprocess.run(cmd, check=True)

Advanced Python API Usage

  1. Start LibreOffice service:
soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &
  1. Python script example:
import uno
from com.sun.star.beans import PropertyValue

# Connect to running instance
local_context = uno.getComponentContext()
resolver = local_context.ServiceManager.createInstanceWithContext(
    "com.sun.star.bridge.UnoUrlResolver", local_context)
ctx = resolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)

# Document operations
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
text = doc.Text
cursor = text.createTextCursor()
text.insertString(cursor, "Sample Content", 0)

# Save options
pdf_args = (PropertyValue(Name="FilterName", Value="writer_pdf_Export"),)
doc.storeToURL("file:///tmp/output.pdf", pdf_args)
doc.close(True)

Troubleshooting

Common issues and solutions:

  1. Missing GLIBC versions - Upgrade system libraries
  2. Font rendering issues - Install additional font packages
  3. Conversion failures - Ensure all required components are installed

For headless server operation, install:

yum install libreoffice-headless libreoffice-writer

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.