FloFaber.com

Nextcloud PDF preview

A couple minutes ago I enabled the PDF preview on my nextcloud instance only to find out that it doesn't work. The nextcloud documentation only states:

"The following providers are disabled by default due to performance or privacy concerns:
OC\Preview\PDF,..."

as well as:

"Technically Nextcloud can also generate the previews of other file types such as PDF, SVG or various office documents. Due to security concerns those providers have been disabled by default and are considered unsupported. While those providers are still available, we discourage enabling them, and they are not documented."

Great.

The cause

When looking through the nextcloud source code it seems that Imagick is used to generate PDF thumbnails:

[spoiler] Code stuff

PreviewManager.php:

$imagickProviders = [
  ...
  'PDF' => ['mimetype' => '/application\/pdf/', 'class' => Preview\PDF::class],
  ...
];

PDF.php:

class PDF extends Bitmap {

Bitmap.php:

public function getThumbnail(File $file, int $maxX, int $maxY): ?IImage {
...
  // Creates \Imagick object from bitmap or vector file
  try {
    $bp = $this->getResizedPreview($tmpPath, $maxX, $maxY);
  } catch (\Exception $e) {}
...
}

How to fix it

Beware this might introduce a security vulnerability!

Looking further into it, it seems that ImageMagick has disabled PDF conversion by default: askubuntu.com/questions/1127260/imagemagick-convert-not-allowed

So we need to edit /etc/ImageMagick-6/policy.xml in order to enable PDF conversion.

nano /etc/ImageMagick-6/policy.xml

and change this line from:

<policy domain="coder" rights="none" pattern="PDF" />

to

<policy domain="coder" rights="read" pattern="PDF" />

Then restart apache or php-fpm (depending on your setup) and see your working PDF previews.