garant/file-preview-generator-bundle

This Symfony bundle provides classes to generate preview image for office files like .doc, .docx, .xls and others. It contains client and server that lets use it both locally and remotely

v2.0-RC1 2017-08-17 16:51 UTC

README

This bundle provides classes to generate preview image for office files like .doc, .docx, .xls and others. It contains client and server that let use the bundle both locally and remotely.

System requirements (for Microsoft Windows)

A) Microsoft Office must be installed. Also you need to configure COM-objects. From the Start menu, click Run and type Dcomcnfg.exe. In Component Services, click Console root, expand Component Services, expand Computers, then My Computer, then DCOM Configuration Find entity named Microsoft Word 97-2003 Document. Right click on it, then Properties. In the tab Identity choose This user and enter correct admin login and password. Save changes.

B) Install GhostScript

C) Install ImageMagic All *.dll file in folder modules/coders inside your ImageMagic directory copy to ImageMagic directory and C:\Windows\System32 and C:\Windows\SysWOW64 All *.dll file in folder modules/filters inside your ImageMagic directory copy to ImageMagic directory C:\Windows\System32 and C:\Windows\SysWOW64

D) Find and download archive php_imagick-3.4.1-7.0-ts-vc14-x64

Copy all CORE.dll's from it to C:\Windows\System32 and C:\Windows\SysWOW64

Copy php_imagick.dll to ext folder in your php directory

E) Add MAGICK_HOME to your environment PATH

F) Edit php.ini file in your php directory

Uncomment or add next lines in Windows extensions section:

extension=php_curl.dll
extension=php_fileinfo.dll
extension=php_gd2.dll
extension=php_mbstring.dll
extension=php_exif.dll
extension=php_openssl.dll
extension=php_com_dotnet.dll
extension=php_imagick.dll

;...

memory_limit = 1024M

G) Reboot (here it's really important!)

Installation

With composer

This bundle can be installed using composer:

composer require garant/file-preview-generator-bundle

Register the bundle

<?php

// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(

        // ...
        new Liip\ImagineBundle\LiipImagineBundle(),
        // ...
        new Garant\GarantFilePreviewGeneratorBundle\GarantFilePreviewGeneratorBundle(),
    );

	// ...
}

Configuration

You can add remote servers:

garant_file_preview_generator:
    #
    # Available algorithms: random, round_robin
    #server_select_algorithm: random
    #
    # Remote servers are used to generate file preview
    # To start generator server use "garant:file-preview-generator:server-start <server_name>" command
    #servers:
    #    local_unix:
    #        host: 0.0.0.0
    #        port: 9010
    #
    #    remote_windows:
    #        host: 192.168.10.201
    #        port: 9010
    #    
    #    remote_windows_secure:
    #        protocol: https 
    #        host: 192.168.10.201
    #        port: 9011

Optionally you can configure Liip imagine filters to post process preview images:

liip_imagine:
    filter_sets:
        avatar_square:
            filters:
                # Transforms 150x140 to 120x120, while cropping the width
                thumbnail: { size: [120, 120], mode: outbound }

Console commands

If remote server is configured you can start it. Call this command on remote server:

bin/console garant:file-preview-generator:server-start <server_name>

Also you can start server with -vvv flag to check memory usage and get other information about conversion process.

You can start some generators simultaneously and automatically restart processes using embedded supervisor:

bin/console garant:file-preview-generator:supervisor-start

Services

To generate preview you can use specific generator. Use the generator factory to create a generator that you need:

garant_file_preview_generator.remote_client - Remote client. 
garant_file_preview_generator.generator_factory - Factory service.

Example

$temp_file = new \SplFileObject('test.docx');

$factory = $this->container->get('garant_file_preview_generator.generator_factory');
$generator = $factory->get($temp_file, AbstractGenerator::PREVIEW_FORMAT_JPEG);
$jpeg_preview_file = $generator->generate($temp_file, AbstractGenerator::PREVIEW_FORMAT_JPEG);
if(!$jpeg_preview_file){
    $this->get('logger')->err('Preview attachment: Preview generation error');
    throw new \RuntimeException('Preview generation error');
}

dump($jpeg_preview_file);

// Generate filtered preview
$generator->setFilter('avatar_square');
$jpeg_preview_file = $generator->generate($temp_file, AbstractGenerator::PREVIEW_FORMAT_JPEG);

dump($jpeg_preview_file);

// Generate preview in PDF or other format
// see AbstractGenerator class constants
$generator = $factory->get($temp_file, AbstractGenerator::PREVIEW_FORMAT_PDF);
$pdf_preview_file = $generator->generate($temp_file, AbstractGenerator::PREVIEW_FORMAT_PDF);

// Optionally, you can convert range of pages
// $generator->setPageRange('0-2');
// Or set number of pages
// $generator->setPageCount(3);

dump($pdf_preview_file);