Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

A Laravel package for docs conversion using LibreOffice

Document conversion can be a pain when you’re dealing with multiple file formats. And if you’re building an application that involves document conversion, it becomes even more challenging.

If you’re dealing with multiple file formats like .docx, .pdf, .odt, etc., and associated conversions among them, you might need to use different packages. For instance, you can use the barryvdh/laravel-dompdf package for converting HTML to PDF, phpoffice/phpword for converting Word to PDF, and so on.

What if you want to convert all these formats using a single package? Well, that’s where a package called Doxswap can come in handy.

Essentially, Doxswap is a Laravel package that allows you to convert documents using LibreOffice. This means you can convert .docx, .pdf, .odt, etc. to any other format pretty effortlessly since all the heavy lifting is done by LibreOffice.

The only prerequisite for this package is that you need to have LibreOffice installed on your server. Once done, you can install the package using Composer.

composer require blaspsoft/doxswap:0.1.0-beta

You can publish the config file.

php artisan vendor:publish --tag="doxswap-config"

This will publish the doxswap.php config file in the config directory of your Laravel application. Here, you can configure the path to the LibreOffice binary that it uses for conversion.

'libre_office_path' => env('LIBRE_OFFICE_PATH', '/usr/bin/soffice')

Apart from this, you can also set input_disc and output_disc in the config file to read files and save converted files respectively.

Finally, here’s how you can use the package to convert, let’s say, a .docx file to a .pdf file.

use BlaspSoft\Doxswap\Doxswap;

$convertedFile = Doxswap::convert('file.docx', 'pdf');

Learn more about the package: blaspsoft/doxswap

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?