OfficeConverter
repository·master·Indexed 42 days ago
https://github.com/ncjoes/office-converterA 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.
What's inside OfficeConverter
- OfficeConverter is a PHP wrapper for LibreOffice. To use this package, you must have LibreOffice installed on your system.
Convert office files using OfficeConverter
masterUse the
NcJoes\OfficeConverter\OfficeConverterclass to convert files. You can specify the input file in the constructor and the desired output format in theconvertTo()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'); ?>Install OfficeConverter via Composer
masterInstall the
ncjoes/office-converterpackage using Composer within your project directory.composer require ncjoes/office-converterSupported conversion formats
masterOfficeConverter supports the following conversions using LibreOffice or OpenOffice:
Input Format Supported Output Formats pptxpdfpptpdfpdfpdfdocxpdf,odt,htmldocpdf,odt,htmlxlsxpdfxlspdfpngpdfjpgpdfjpegpdfInitialize the OfficeConverter class
masterTo use the
OfficeConverter, instantiate theNcJoes\OfficeConverter\OfficeConverterclass 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. Ifnullor 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): Iftrue(default), it attempts to prefix the command withexport HOMEon 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');Convert a file using convertTo()
masterThe
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, ornullif 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; }Set a conversion filter with setFilter()
masterYou 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-tocommand.
Returns:
OfficeConverter: Returns$thisto allow method chaining.
$converter = new OfficeConverter('document.docx'); $converter->setFilter('HTML:HTML_Export')->convertTo('output.html');Supported file conversions
masterThe
OfficeConvertersupports 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, orhtml. - PPTX/PPT: Can be converted to
pdf. - XLSX/XLS: Can be converted to
pdf. - RTF: Can be converted to
docx,txt, orpdf. - TXT/CSV: Can be converted to
pdf,odt,doc,docx, orhtml. - Images (PNG/JPG/JPEG/JFIF): Can be converted to
pdf.
Note: The library is case-insensitive regarding many extensions (e.g.,
DOCXanddocxare 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']- DOCX/DOC/WPS/DOTX/etc.: Can be converted to
Handle OfficeConverter errors with OfficeConverterException
masterWhen using the
NcJoes\OfficeConverterlibrary, any errors encountered during the conversion process will throw anOfficeConverterException. You should wrap your conversion logic in atry-catchblock 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(); }