How can I enable PDF preview

Hello,

Thank you for your support.

Here are my installation info:

Nextcloud version: 19.0.1.1 (installed on user account via a PHP script from nextcloud website.)
OS: Ubuntu 20.04.1
Control Panel: Hestia CP v1.2.3
Apache version: 2.4.46
PHP version: 7.3

APCu Support: Enabled
memcache.local:none
memcache.distributed:none
memcache.locking: none

I have also installed the following requirements:
*imagemagick
*ghostscript

I am trying to activate PDF previews. Its looks like I have a multiple issues regarding cash, security and setting up the config.php file.

I tried to edit the config.php file by myself but I failed.
https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/config_sample_php_parameters.html?highlight=pdf#previews

For more details check the logs below.

The output of my Nextcloud log

    [core] Error: ImagickException: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408 at <<closure>>

     0. /home/XXXX/web/XXXX/public_html/lib/private/Preview/Bitmap.php line 87
    Imagick->readimage("/home/XXXX/w ... ]")
     1. /home/XXXX/web/XXXX/public_html/lib/private/Preview/Bitmap.php line 50
    OC\Preview\Bitmap->getResizedPreview("/home/XXXX/w ... f", 4096, 4096)
     2. /home/XXXX/web/XXXX/public_html/lib/private/Preview/GeneratorHelper.php line 63
    OC\Preview\Bitmap->getThumbnail(OC\Files\Node\File {}, 4096, 4096)
     3. /home/XXXX/web/XXXX/public_html/lib/private/Preview/Generator.php line 235
    OC\Preview\GeneratorHelper->getThumbnail(OC\Preview\PDF {}, OC\Files\Node\File {}, 4096, 4096)
     4. /home/XXXX/web/XXXX/public_html/lib/private/Preview/Generator.php line 141
    OC\Preview\Generator->getMaxPreview(OC\Files\SimpleFS\SimpleFolder {}, OC\Files\Node\File {}, "application/pdf", "")
     5. /home/XXXX/web/XXXX/public_html/lib/private/Preview/Generator.php line 107
    OC\Preview\Generator->generatePreviews(OC\Files\Node\File {}, [{width: 256,hei ... }], "application/pdf")
     6. /home/XXXX/web/XXXX/public_html/lib/private/PreviewManager.php line 190
    OC\Preview\Generator->getPreview(OC\Files\Node\File {}, 256, 256, true, "fill", null)
     7. /home/XXXX/web/XXXX/public_html/apps/files/lib/Controller/ApiController.php line 128
    OC\PreviewManager->getPreview(OC\Files\Node\File {}, 256, 256, true)
     8. /home/XXXX/web/XXXX/public_html/lib/private/AppFramework/Http/Dispatcher.php line 170
    OCA\Files\Controller\ApiController->getThumbnail(256, 256, OC\Files\Node\File {})
     9. /home/XXXX/web/XXXX/public_html/lib/private/AppFramework/Http/Dispatcher.php line 100
    OC\AppFramework\Http\Dispatcher->executeController(OCA\Files\Controller\ApiController {}, "getThumbnail")
    10. /home/XXXX/web/XXXX/public_html/lib/private/AppFramework/App.php line 137
    OC\AppFramework\Http\Dispatcher->dispatch(OCA\Files\Controller\ApiController {}, "getThumbnail")
    11. /home/XXXX/web/XXXX/public_html/lib/private/AppFramework/Routing/RouteActionHandler.php line 47
    OC\AppFramework\App::main("APIController", "getThumbnail", OC\AppFramework\ ... {}, {x: "256",y: "25 ... "})
    12. <<closure>>
    OC\AppFramework\Routing\RouteActionHandler->__invoke({x: "256",y: "25 ... "})
    13. /home/XXXX/web/XXXX/public_html/lib/private/Route/Router.php line 297
    call_user_func(OC\AppFramework\ ... {}, {x: "256",y: "25 ... "})
    14. /home/XXXX/web/XXXX/public_html/lib/base.php line 1007
    OC\Route\Router->match("/apps/files/api ... f")
    15. /home/XXXX/web/XXXX/public_html/index.php line 37
    OC::handleRequest()

    GET /index.php/apps/files/api/v1/thumbnail/256/256/1_5118352478428987514.pdf
    from 78.95.238.102 by XXXX at 2020-08-20T20:54:30+00:00

The output of my config.php:

    <?php
    $CONFIG = array (
      'instanceid' => 'XXXX',
      'passwordsalt' => 'XXXX',
      'secret' => 'XXXX',
      'trusted_domains' => 
      array (
        0 => 'XXXX.com',
      ),
      'datadirectory' => '/home/XXXX/web/XXXX.com/public_html/data',
      'dbtype' => 'mysql',
      'version' => '19.0.1.1',
      'overwrite.cli.url' => 'https://XXXX.com',
      'dbname' => 'XXXX',
      'dbhost' => 'localhost',
      'dbport' => '',
      'dbtableprefix' => 'oc_',
      'mysql.utf8mb4' => true,
      'dbuser' => 'XXXX',
      'dbpassword' => 'XXXX',
      'installed' => true,
    );

Can you kindly assist me fixing the issue and getting the PDF previews to work.

Best regards,
Nasser

1 Like

You find all information in the documentation, so please describe IN DETAIL what changes you’ve tried to apply to your configuration and what the DETAILED result was of your tests.

Hello,

Thank you for your support.

I manage to fix the PDF preview by editing the config.php file to the following:

<?php
$CONFIG = array (
  'instanceid' => 'XXXX',
  'passwordsalt' => 'XXXX',
  'secret' => 'XXXX',
  'trusted_domains' => 
  array (
    0 => 'XXXX',
  ),
  'datadirectory' => '/home/XXXX/web/XXXX/public_html/data',
  'dbtype' => 'mysql',
  'version' => '19.0.1.1',
  'overwrite.cli.url' => 'XXXX',
  'dbname' => 'XXXX',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'XXXX',
  'dbpassword' => 'XXXX',
  'installed' => true,
  'memcache.local' => '\OC\Memcache\APCu',
  'enable_previews' => true,
  'preview_max_x' => 4096,
  'preview_max_y' => 4096,
  'preview_max_filesize_image' => 50,
  'enabledPreviewProviders' => [
    'OC\Preview\PNG',
    'OC\Preview\JPEG',
    'OC\Preview\GIF',
    'OC\Preview\HEIC',
    'OC\Preview\BMP',
    'OC\Preview\XBitmap',
    'OC\Preview\MP3',
    'OC\Preview\TXT',
    'OC\Preview\MarkDown',
    'OC\Preview\OpenDocument',
    'OC\Preview\Krita',
    'OC\Preview\PDF',
  ],
);

I also edited “Imagemagick security policy” just before the end by wrapping it :

  <!-- <policy domain="coder" rights="none" pattern="PS" />
  <policy domain="coder" rights="none" pattern="PS2" />
  <policy domain="coder" rights="none" pattern="PS3" />
  <policy domain="coder" rights="none" pattern="EPS" />
  <policy domain="coder" rights="none" pattern="PDF" /> 
  <policy domain="coder" rights="none" pattern="XPS" /> -->

The file I edited is loacted at /etc/ImageMagick-6/policy.xml . Hope this solution help other members with the same issue.

Regards,
Nasser

4 Likes

To give rights for PDF in the policy, you can run, e.g. in Dockerfile:

sed -i 's/\(^ *<policy.*rights="\)\([^"]*\)\(".*PDF.*\/>\)/\1read|write\3/1' /etc/ImageMagick-6/policy.xml

Of course, sed is bad for XML…

6 Likes

Thanks - worked for me.