OfficeConverter

repository·master·Indexed 42 days ago

https://github.com/ncjoes/office-converter

A PHP wrapper for LibreOffice that converts office document formats (DOCX, XLSX, PPTX) and images into PDF, HTML, or ODT files. It provides the NcJoes\OfficeConverter\OfficeConverter class to manage file conversions, custom filters via setFilter(), and error handling through OfficeConverterException.

Tokens
1.7K
Snippets
7
Records
9
Agent score
52%

What's inside OfficeConverter

  1. Convert office files using OfficeConverter

    master

    Use the NcJoes\OfficeConverter\OfficeConverter class to convert files. You can specify the input file in the constructor and the desired output format in the convertTo() method.

    By default, the output file is generated in the same directory as the input file. To specify a different output directory, pass the directory path as the second argument to the constructor.

    <?php
    use NcJoes\OfficeConverter\OfficeConverter;
    
    // Basic usage: converts file to PDF in the same directory
    $converter = new OfficeConverter('test-file.docx');
    $converter->convertTo('output-file.pdf');
    
    // Converts file to HTML in the same directory
    $converter->convertTo('output-file.html');
    
    // Usage with a specific output directory
    $converter = new OfficeConverter('test-file.docx', 'path-to-outdir');
    $converter->convertTo('output-file.pdf');
    ?>
  2. Initialize the OfficeConverter class

    master

    To use the OfficeConverter, instantiate the NcJoes\OfficeConverter\OfficeConverter class by providing the path to the file you wish to convert. You can optionally configure the temporary directory, the LibreOffice binary path, environment variable handling, and log paths.

    Constructor Parameters:

    • $filename (string): The absolute or relative path to the source file.
    • $tempPath (string|null): The directory where converted files will be stored. If null or invalid, it defaults to the directory of the source file, or the system temp directory.
    • $bin (string): The path to the LibreOffice binary. Defaults to 'libreoffice'.
    • $prefixExecWithExportHome (bool): If true (default), it attempts to prefix the command with export HOME on non-Windows systems to ensure a writable home directory for LibreOffice.
    • $logPath (string|null): The path to a file where conversion logs/errors will be appended.
    use NcJoes\OfficeConverter\OfficeConverter;
    
    $converter = new OfficeConverter('/path/to/document.docx', '/tmp/output', 'libreoffice');
  3. Convert a file using convertTo()

    master

    The convertTo($filename) method performs the actual conversion. It determines the supported output formats based on the input file's extension and executes the conversion via LibreOffice.

    Parameters:

    • $filename (string): The desired name (or path) for the output file.

    Returns:

    • string|null: The full path to the converted file if successful, or null if the file could not be located/renamed.

    Throws:

    • OfficeConverterException: If the output extension is not supported for the given input type, or if the conversion process fails.
    $converter = new OfficeConverter('document.docx');
    // This will convert the docx to a pdf named 'document.pdf' in the same directory
    $outputPath = $converter->convertTo('document.pdf');
    
    if ($outputPath) {
        echo "Converted file is at: " . $outputPath;
    }
  4. Set a conversion filter with setFilter()

    master

    You can refine the conversion process by applying a specific LibreOffice filter using the setFilter($filter) method. This is useful for fine-tuning how specific formats (like HTML or PDF) are generated.

    Parameters:

    • $filter (string): The filter string to be passed to the --convert-to command.

    Returns:

    • OfficeConverter: Returns $this to allow method chaining.
    $converter = new OfficeConverter('document.docx');
    $converter->setFilter('HTML:HTML_Export')->convertTo('output.html');
  5. Supported file conversions

    master

    The OfficeConverter supports a wide range of input and output formats. The available output formats depend on the input file extension.

    Common conversion mappings:

    • DOCX/DOC/WPS/DOTX/etc.: Can be converted to pdf, odt, or html.
    • PPTX/PPT: Can be converted to pdf.
    • XLSX/XLS: Can be converted to pdf.
    • RTF: Can be converted to docx, txt, or pdf.
    • TXT/CSV: Can be converted to pdf, odt, doc, docx, or html.
    • Images (PNG/JPG/JPEG/JFIF): Can be converted to pdf.

    Note: The library is case-insensitive regarding many extensions (e.g., DOCX and docx are treated similarly).

    // Example of supported mappings extracted from the source:
    // 'docx' => ['pdf', 'odt', 'html']
    // 'xlsx' => ['pdf']
    // 'rtf'  => ['docx', 'txt', 'pdf']
    // 'txt'  => ['pdf', 'odt', 'doc', 'docx', 'html']
  6. Handle OfficeConverter errors with OfficeConverterException

    master

    When using the NcJoes\OfficeConverter library, any errors encountered during the conversion process will throw an OfficeConverterException. You should wrap your conversion logic in a try-catch block targeting this specific exception class to handle library-specific failures gracefully.

    use NcJoes\OfficeConverter\OfficeConverterException;
    
    try {
        // Your conversion logic here
    } catch (OfficeConverterException $e) {
        // Handle conversion errors
        echo 'Error: ' . $e->getMessage();
    }