Skip to content

Instantly share code, notes, and snippets.

@jeffreyvr
Created September 9, 2020 14:49
Show Gist options
  • Save jeffreyvr/07e59dbe3444371fadf97db449bcdaa8 to your computer and use it in GitHub Desktop.
Save jeffreyvr/07e59dbe3444371fadf97db449bcdaa8 to your computer and use it in GitHub Desktop.
Concept using Poppler pdftoppm for PDF image generation
<?php
/**
* Concept class.
*/
class PdfTopPm
{
public $popplerPath;
public $dpi = 72;
public $quality = 100;
public $singleFile = false;
public $index = 1;
public $file;
public $imagePrefix;
public function __construct()
{
return $this;
}
public function setFile($file)
{
$this->file = $file;
return $this;
}
public function setImagePrefix($prefix)
{
$this->imagePrefix = $prefix;
return $this;
}
public function setPopplerPath($path)
{
$this->popplerPath = $path;
return $this;
}
public function setDpi($dpi)
{
$this->dpi = $dpi;
return $this;
}
public function setQuality($quality)
{
$this->quality = $quality;
return $this;
}
public function setIndex($index)
{
$this->index = $index;
return $this;
}
public function isSingleFile($page)
{
$this->singleFile = true;
$this->index = $page;
return $this;
}
public function execute()
{
$single = null;
if ($this->singleFile ) {
$single = '-singlefile -f ' . $this->index;
}
$dpis = (array) $this->dpi;
$prefix_dpi = count($dpis) > 1;
foreach ( $dpis as $dpi ) {
$filename = $prefix_dpi ? $dpi . '_' . $this->imagePrefix : $this->imagePrefix;
shell_exec("{$this->popplerPath}pdftoppm {$single} -r {$dpi} -jpeg -jpegopt quality={$this->quality} {$this->file} {$filename}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment