📜 ⬆️ ⬇️

Generating documents in doc, excel, pdf and other formats on the server

Downloading reports in various formats is a typical task for many projects. And now there are a lot of tools for this. Among them there is an interesting option that is used, it seems to me, not often, but it is definitely worth the attention. Because it allows you to get the document in the right format with just one command. About him and tell.

image


I will not be wordy and immediately say that this is a converter built into the LibreOffice package. You can run the conversion from the console to see how it works:

libreoffice --headless --writer --convert-to pdf html.html 

This command converts the html.html file to a pdf file. The number of formats supported is impressive .
')
The benefit of using such a tool is obvious. Instead of writing code to generate documents in each of the required formats, we simply create a regular html representation. Next, run the generated page through the converter.

Running conversion from PHP


To install the converter on the server, you will have to install the libreoffice-core package:

 sudo apt-get install libreoffice-core --no-install-recommends 

That it was convenient to work with the utility from PHP, I wrote a wrapper .

The wrapper allows you not to think about working with temporary files, inserts some default parameters into the command, contains constants describing the available formats, and also gives you the opportunity to set a timeout for conversion.

To work with the wrapper we connect it to your project via composer:

 composer require mnvx/lowrapper 

You can use it like this:

 use Mnvx\Lowrapper\Converter; use Mnvx\Lowrapper\LowrapperParameters; use Mnvx\Lowrapper\Format; //    $converter = new Converter(); //     $parameters = (new LowrapperParameters()) //      HTML ->setInputData('<html>My html file</html>') //      ->setOutputFormat(Format::TEXT_DOCX) //     ->setOutputFile('path-to-result-docx.docx'); //   $converter->convert($parameters); 

As a result, a docx file will be generated. More examples can be found on the githaba .

Of course, as a bonus, you can run the conversion in the other direction - from doc to html and display the contents of office documents in the browser. The quality of the conversion will not always be at its best, but for some cases it is quite suitable.

Few rakes


It will be useful to tell about several features that I encountered when working with this utility.

1. Apply CSS styles. When converting html to the required format, keep in mind that such a record is perceived correctly:

 <p class=”someclass”></p> 

And such records will be processed in the same way as if the class we did not specify at all:

 <p class=”some_class”>Some text</p> <p class=”class1 class2”>Some text</p> 

2. When converting html to the desired format, style sheets do not always work and sometimes you have to experiment to make it work. For example, this does not work:

 td, th { border: 1px solid black; } 

But it works like this:

 .td { border: 1px solid black; } ... <td class=”td”>...</td> 

3. The same conversion can be performed using different converters. The result will be significantly different. If your output is a not very nice document, try forcing the module to be used, for example:

 $parameters = (new LowrapperParameters()) ->setDocumentType(DocumentType::WRITER); 

4. Is it possible to adjust the width of the rows in the table - for me it's still a mystery. And in general, with the stylization of the table when converting html to docx or pdf, I had difficulty. Therefore, in my opinion, the approach will be difficult to apply for the generation of complex printing forms, such as an invoice.

Conclusion


The tool is universal and very good if you have documents that are not very complex in layout. Then at the exit you get documents in the right formats by writing just a few lines of code.

Source: https://habr.com/ru/post/320268/


All Articles