A Laravel package for seamless document and image format conversions. Transform between various formats like DOCX -> PDF, HTML -> PDF, PNG -> WEBP, and more popular formats using a simple, elegant API. Powered by LibreOffice for documents and ImageMagick for image processing.
- π Multiple Format Support β Convert between documents (DOCX, XLSX, ODT) and images (PNG, JPG, WEBP) with ease
- π Simple API β Easy-to-use interface for all conversion operations
- πΎ Laravel Storage Integration β Works seamlessly with Laravel's filesystem drivers
- β‘ Efficient Processing β Optimized conversion using LibreOffice and ImageMagick engines
- π Conversion Tracking β Detailed results including duration and file paths
- π Secure File Handling β Safe and secure file processing with proper cleanup
- βοΈ Configurable Settings β Customize paths, storage disks, and conversion options
- π‘οΈ Error Handling β Robust exception handling for unsupported formats and conversions
You can install the package via composer:
composer require blaspsoft/doxswap
You can publish the config file with:
php artisan vendor:publish --tag="doxswap-config"
The config/doxswap.php
file includes:
input_disk
: Where to read files from (default: 'public')output_disk
: Where to save converted files (default: 'public')perform_cleanup
: Delete input files after conversion (default: false)
Configure how output files are named using different strategies:
'filename' => [
// Strategy: 'original', 'random', or 'timestamp'
'strategy' => 'original',
// Naming options
'options' => [
'length' => 24, // Length for random names
'prefix' => '', // Add prefix to filename
'suffix' => '', // Add suffix to filename
'separator' => '_', // Separator for components
'format' => 'YmdHis', // Format for timestamp strategy
],
]
Configure paths for conversion tools:
'drivers' => [
'libreoffice_path' => env('LIBRE_OFFICE_PATH', '/usr/bin/soffice'),
]
Default LibreOffice paths by OS:
- π§ Linux:
/usr/bin/soffice
- π macOS:
/Applications/LibreOffice.app/Contents/MacOS/soffice
- πͺ Windows:
C:\Program Files\LibreOffice\program\soffice.exe
Supports various document formats including:
- Documents: DOC, DOCX, ODT, RTF, TXT
- Spreadsheets: XLS, XLSX, ODS, CSV
- Presentations: PPT, PPTX, ODP
- Images: JPG, PNG, SVG, BMP, TIFF, WEBP, GIF
- Web: HTML, XML
- Other: PDF
$result = Doxswap::convert('sample.docx', 'pdf');
/**
* Returns a ConversionResult object with the following properties:
*
* @property string $inputFilename The original input filename
* @property string $inputFilePath The full path to the input file
* @property string $outputFilename The converted output filename
* @property string $outputFilePath The full path to the converted output file
* @property string $toFormat The format the file was converted to (e.g. 'pdf')
* @property string $duration The time taken for conversion (e.g. "2.21 sec")
* @property float $startTime Unix timestamp of when conversion started
* @property float $endTime Unix timestamp of when conversion completed
* @property string $inputDisk The Laravel storage disk used for input
* @property string $outputDisk The Laravel storage disk used for output
*/
This package requires LibreOffice, ImageMagick, and Potrace to be installed on your system. Here's how to install them:
sudo apt update
sudo apt install libreoffice imagemagick potrace
brew install libreoffice imagemagick potrace
choco install libreoffice imagemagick potrace
If you're using Docker, you can add the required dependencies to your container:
# Ubuntu/Debian based
RUN apt-get update && apt-get install -y libreoffice imagemagick potrace
# Alpine based
RUN apk add --no-cache libreoffice imagemagick potrace
- PHP >= 8.1
- ext-fileinfo
- ext-imagick
- Laravel >= 9.0
From | Supported Conversions |
---|---|
DOCX | PDF β β , ODT, RTF, TXT, HTML, XML, EPUB |
DOC | PDF β β , DOCX, ODT, RTF, TXT, HTML, XML, EPUB |
ODT | PDF, DOCX, RTF, TXT, HTML, XML |
RTF | PDF, DOCX, ODT, TXT, HTML, XML |
TXT | PDF, DOCX, ODT, HTML, XML |
HTML | PDF, ODT, TXT |
XML | PDF, DOCX, ODT, TXT, HTML |
From | Supported Conversions |
---|---|
XLSX | PDF β β , ODS, CSV |
XLS | PDF, XLSX, ODS, CSV |
ODS | PDF, XLSX, CSV |
CSV | PDF, XLSX, ODS |
From | Supported Conversions |
---|---|
PPTX | PDF β β , ODP |
PPT | PDF, PPTX, ODP |
ODP | PDF, PPTX |
From | Supported Conversions |
---|---|
PNG | PDF β , JPG, SVG, TIFF, WEBP, GIF, BMP |
JPG | PDF β , PNG, SVG, TIFF, WEBP, GIF, BMP |
SVG | PDF, PNG, JPG, TIFF, WEBP, GIF, BMP |
BMP | PDF, PNG, JPG, SVG, TIFF, WEBP, GIF |
TIFF | PDF, PNG, JPG, SVG, WEBP, GIF, BMP |
WEBP | PDF, PNG, JPG, SVG, TIFF, GIF, BMP |
GIF | PDF, PNG, JPG, SVG, TIFF, WEBP, BMP |
- β β = Common high-priority conversion
- β = Popular supported format
- (unlisted) = Conversion not supported
Note: Document conversions are performed using LibreOffice in headless mode, while image format conversions utilize ImageMagick π
If you find this package helpful, please consider sponsoring the development:
Blasp is open-sourced software licensed under the MIT license.