### Install Office Converter via Composer Source: https://context7.com/ncjoes/office-converter/llms.txt Use Composer to add the library to your project and verify the required LibreOffice installation. ```bash # Install the library via Composer composer require ncjoes/office-converter # Verify LibreOffice is installed (required dependency) libreoffice --version ``` -------------------------------- ### Install OfficeConverter via Composer Source: https://github.com/ncjoes/office-converter/blob/master/README.md Use this command to install the OfficeConverter package in your project using Composer. ```shell composer require ncjoes/office-converter ``` -------------------------------- ### Initialize OfficeConverter Source: https://context7.com/ncjoes/office-converter/llms.txt Configure the converter with source files, output directories, and custom binary paths. ```php convertTo('output-file.pdf'); //generates pdf file in same directory as test-file.docx $converter->convertTo('output-file.html'); //generates html file in same directory as test-file.docx //to specify output directory, specify it as the second argument to the constructor $converter = new OfficeConverter('test-file.docx', 'path-to-outdir'); ?> ``` -------------------------------- ### OfficeConverter Constructor Source: https://context7.com/ncjoes/office-converter/llms.txt Initializes the converter instance with the source file and optional configuration settings. ```APIDOC ## Constructor: OfficeConverter ### Description Initializes the converter with a source file and optional configuration for output directory, LibreOffice binary path, HOME environment prefix, and log file path. ### Parameters - **sourceFile** (string) - Required - Path to the source document. - **outputDirectory** (string) - Optional - Path to the output directory. - **binaryPath** (string) - Optional - Custom path to the LibreOffice binary (default: 'libreoffice'). - **prefixHome** (boolean) - Optional - Whether to prefix exec with export HOME (default: true). - **logPath** (string) - Optional - Path to the log file. ``` -------------------------------- ### Handle OfficeConverter Exceptions Source: https://context7.com/ncjoes/office-converter/llms.txt Demonstrates catching OfficeConverterException for file errors, unsupported formats, and execution failures. ```php getMessage(); // Output: File does not exist --/nonexistent/file.docx } // Handle unsupported input format try { $converter = new OfficeConverter('/files/video.mp4', '/output'); } catch (OfficeConverterException $e) { echo $e->getMessage(); // Output: Input file extension not supported -- mp4 } // Handle unsupported output format for given input try { $converter = new OfficeConverter('/images/photo.jpg', '/output'); $converter->convertTo('photo.html'); // Images can only convert to PDF } catch (OfficeConverterException $e) { echo $e->getMessage(); // Output: Output extension (html) not supported for input file(photo.jpg) } // Handle LibreOffice execution failure try { $converter = new OfficeConverter('/docs/corrupted.docx', '/output'); $converter->convertTo('output.pdf'); } catch (OfficeConverterException $e) { // Error message includes return code and stderr output echo $e->getMessage(); // Output: Convertion Failure! Contact Server Admin: code 1 error: ... } // Complete error handling example function safeConvert(string $inputFile, string $outputFile, string $outputDir): ?string { try { $converter = new OfficeConverter($inputFile, $outputDir); return $converter->convertTo($outputFile); } catch (OfficeConverterException $e) { error_log("Conversion failed for $inputFile: " . $e->getMessage()); return null; } } $result = safeConvert('/uploads/document.docx', 'document.pdf', '/converted'); if ($result) { echo "Success: $result"; } else { echo "Conversion failed - check logs"; } ``` -------------------------------- ### Convert Documents with convertTo() Source: https://context7.com/ncjoes/office-converter/llms.txt Convert various file types to supported output formats, handling potential exceptions during the process. ```php convertTo('report.pdf'); echo "PDF created at: $pdfPath"; // Output: PDF created at: /output/report.pdf } catch (OfficeConverterException $e) { echo "Conversion failed: " . $e->getMessage(); } // Convert Word document to HTML $converter = new OfficeConverter('/documents/report.docx', '/output'); $htmlPath = $converter->convertTo('report.html'); // Output: /output/report.html // Convert Word document to ODT (OpenDocument Text) $converter = new OfficeConverter('/documents/report.docx', '/output'); $odtPath = $converter->convertTo('report.odt'); // Output: /output/report.odt // Convert Excel spreadsheet to PDF $converter = new OfficeConverter('/documents/data.xlsx', '/output'); $pdfPath = $converter->convertTo('data.pdf'); // Output: /output/data.pdf // Convert PowerPoint to PDF $converter = new OfficeConverter('/presentations/slides.pptx', '/output'); $pdfPath = $converter->convertTo('slides.pdf'); // Output: /output/slides.pdf // Convert image to PDF $converter = new OfficeConverter('/images/photo.jpg', '/output'); $pdfPath = $converter->convertTo('photo.pdf'); // Output: /output/photo.pdf ``` -------------------------------- ### Convert HTML Files Source: https://context7.com/ncjoes/office-converter/llms.txt Convert HTML files to PDF or DOCX formats. ```php // HTML files // Supported outputs: pdf, docx $converter = new OfficeConverter('/pages/page.html', '/output'); $converter->convertTo('page.pdf'); $converter->convertTo('page.docx'); ``` -------------------------------- ### Perform Batch File Conversion Source: https://context7.com/ncjoes/office-converter/llms.txt Iterates through a directory to convert supported document types to PDF while tracking success and failure results. ```php [], 'failed' => []]; foreach (glob("$inputDir/*") as $file) { $extension = strtolower(pathinfo($file, PATHINFO_EXTENSION)); if (!in_array($extension, $supportedExtensions)) { continue; } $outputName = pathinfo($file, PATHINFO_FILENAME) . '.pdf'; try { $converter = new OfficeConverter($file, $outputDir); $outputPath = $converter->convertTo($outputName); $results['success'][] = [ 'input' => $file, 'output' => $outputPath ]; } catch (OfficeConverterException $e) { $results['failed'][] = [ 'input' => $file, 'error' => $e->getMessage() ]; } } echo "Converted: " . count($results['success']) . " files\n"; echo "Failed: " . count($results['failed']) . " files\n"; // Output: // Converted: 15 files // Failed: 2 files ``` -------------------------------- ### convertTo() Method Source: https://context7.com/ncjoes/office-converter/llms.txt Converts the source document to the specified output format. ```APIDOC ## Method: convertTo() ### Description Converts the source document to the specified output format. Returns the full path to the converted file on success. ### Parameters - **format** (string) - Required - The target output filename with extension (e.g., 'report.pdf', 'report.html'). ### Response - **Success** (string) - Returns the full path to the converted file. - **Error** (Exception) - Throws an OfficeConverterException if conversion fails or the format is unsupported. ``` -------------------------------- ### Convert Plain Text Source: https://context7.com/ncjoes/office-converter/llms.txt Convert plain text (txt) files to PDF, ODT, DOC, DOCX, or HTML formats. ```php // Plain Text (txt) // Supported outputs: pdf, odt, doc, docx, html $converter = new OfficeConverter('/docs/readme.txt', '/output'); $converter->convertTo('readme.pdf'); $converter->convertTo('readme.docx'); $converter->convertTo('readme.html'); ``` -------------------------------- ### Convert PowerPoint Presentations Source: https://context7.com/ncjoes/office-converter/llms.txt Convert PowerPoint presentations (pptx, ppt) to PDF format. ```php // PowerPoint Presentations (pptx, ppt) // Supported outputs: pdf $converter = new OfficeConverter('/slides/presentation.pptx', '/output'); $converter->convertTo('presentation.pdf'); ``` -------------------------------- ### Apply Export Filters with setFilter() Source: https://context7.com/ncjoes/office-converter/llms.txt Use setFilter() to specify LibreOffice export filters for controlling conversion output, such as PDF versions or compression settings. This method can be chained for concise syntax. ```php setFilter('writer_pdf_Export'); $pdfPath = $converter->convertTo('report.pdf'); // Apply specific PDF filter with options $converter = new OfficeConverter('/spreadsheets/data.xlsx', '/output'); $converter->setFilter('calc_pdf_Export'); $pdfPath = $converter->convertTo('data.pdf'); // Method chaining - setFilter returns the converter instance $pdfPath = (new OfficeConverter('/docs/file.docx', '/output')) ->setFilter('writer_pdf_Export') ->convertTo('output.pdf'); // Convert HTML to Word document with filter $converter = new OfficeConverter('/pages/webpage.html', '/output'); $converter->setFilter('MS Word 2007 XML'); $docxPath = $converter->convertTo('webpage.docx'); ``` -------------------------------- ### Convert Rich Text Format Source: https://context7.com/ncjoes/office-converter/llms.txt Convert Rich Text Format (rtf) files to DOCX, TXT, or PDF formats. ```php // Rich Text Format (rtf) // Supported outputs: docx, txt, pdf $converter = new OfficeConverter('/docs/document.rtf', '/output'); $converter->convertTo('document.docx'); $converter->convertTo('document.txt'); $converter->convertTo('document.pdf'); ``` -------------------------------- ### Convert Images Source: https://context7.com/ncjoes/office-converter/llms.txt Convert image files (png, jpg, jpeg, jfif) to PDF format. ```php // Images (png, jpg, jpeg, jfif) // Supported outputs: pdf $converter = new OfficeConverter('/images/photo.png', '/output'); $converter->convertTo('photo.pdf'); ``` -------------------------------- ### Convert Word Documents Source: https://context7.com/ncjoes/office-converter/llms.txt Convert Word documents (docx, doc, wps, dotx, docm, dotm, dot) to PDF, ODT, or HTML formats. ```php convertTo('document.pdf'); // Word to PDF $converter->convertTo('document.odt'); // Word to OpenDocument $converter->convertTo('document.html'); // Word to HTML ``` -------------------------------- ### Convert Excel Spreadsheets Source: https://context7.com/ncjoes/office-converter/llms.txt Convert Excel spreadsheets (xlsx, xls, csv) to PDF format. ```php // Excel Spreadsheets (xlsx, xls, csv) // Supported outputs: pdf $converter = new OfficeConverter('/sheets/spreadsheet.xlsx', '/output'); $converter->convertTo('spreadsheet.pdf'); ``` -------------------------------- ### Convert OpenDocument Text Source: https://context7.com/ncjoes/office-converter/llms.txt Convert OpenDocument Text (odt) files to PDF or HTML formats. ```php // OpenDocument Text (odt) // Supported outputs: pdf, html $converter = new OfficeConverter('/docs/document.odt', '/output'); $converter->convertTo('document.pdf'); $converter->convertTo('document.html'); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.