Skip to content

Instantly share code, notes, and snippets.

@phpfour
Created November 27, 2015 08:48
Show Gist options
  • Select an option

  • Save phpfour/95c0417bfc2d91cf503d to your computer and use it in GitHub Desktop.

Select an option

Save phpfour/95c0417bfc2d91cf503d to your computer and use it in GitHub Desktop.
Pdf Creator
<?php
namespace Docudex\Bundle\DocumentBundle\Staged;
use Docudex\Bundle\CoreBundle\Helper\File;
use Docudex\Bundle\CoreBundle\Services\PathConfigurator;
class PdfCreator
{
protected $repositoryKey;
protected $sourceDirectory;
protected $destinationDirectory;
/**
* @var PathConfigurator
*/
private $configManager;
public function __construct(PathConfigurator $configManager)
{
$this->configManager = $configManager;
}
public function create($sourceFiles = array(), $title)
{
$files = array();
foreach ($sourceFiles as $file) {
$files[] = $this->sourceDirectory . DIRECTORY_SEPARATOR . $file;
}
$extension = 'pdf';
// TODO: Sanitize the document title and add some other constant to generate a name
$filename = md5(time() . mt_rand()) . '.%s';
$time = new \DateTime('now');
$path = $this->configManager->getTimedSuffixedPath($time, $this->destinationDirectory);
if (!is_dir($path)) {
@mkdir($path, 0777, TRUE);
}
$outputFile = $path . DIRECTORY_SEPARATOR . $filename;
if (count($files) == 1 && ($this->isPdf($files[0]) || !$this->isMergeable($files[0]))) {
$extension = File::getExtension($files[0]);
$outputFile = sprintf($outputFile, $extension);
File::move($files[0], $outputFile, true);
} else {
$outputFile = sprintf($outputFile, $extension);
if (!$this->convert($files, $outputFile)) {
return false;
}
}
return array(
'filename' => sprintf($filename, $extension),
'extension' => $extension,
'mimeType' => $extension == 'pdf' ? 'application/pdf' : File::getMimeType($outputFile),
'userFileName' => $title . "." . $extension,
'createTime' => $time,
'size' => filesize($outputFile),
'meta' => array('files' => $sourceFiles),
'repositoryKey' => $this->repositoryKey
);
}
protected function isMergeable($file)
{
$type = File::getType($file);
return ($type == 'image' || $type == 'pdf');
}
protected function containNonMergeableFile($files)
{
foreach ($files as $file) {
if (!$this->isMergeable($file)) {
return true;
}
}
return false;
}
protected function isPdf($file)
{
return File::getExtension($file, true) == 'pdf';
}
/**
* Set source directory
*
* @param mixed $sourceDirectory
* @return PdfCreator
*/
public function setSourceDirectory($sourceDirectory)
{
$this->sourceDirectory = $sourceDirectory;
return $this;
}
/**
* Set destination directory
*
* @param mixed $destinationDirectory
* @return PdfCreator
*/
public function setDestinationDirectory($destinationDirectory)
{
$this->destinationDirectory = $destinationDirectory;
return $this;
}
/**
* Set repository key
*
* @param mixed $repositoryKey
* @return PdfCreator
*/
public function setRepositoryKey($repositoryKey)
{
$this->repositoryKey = $repositoryKey;
return $this;
}
/**
* @param $files
* @param $outputFile
*
* @return bool
*/
protected function convert($files, $outputFile)
{
if ($this->containNonMergeableFile($files)) {
return false;
}
$output = new \Imagick();
foreach ($files as $file) {
$source = new \Imagick($file);
$watermark = new \Imagick($_SERVER['DOCUMENT_ROOT'] . "/watermark.png");
list ($x, $y) = $this->getWatermarkCoordinates($source, $watermark);
$source->compositeImage($watermark, \Imagick::COMPOSITE_OVER, $x, $y);
$output->addImage($source);
}
$output->resetiterator();
$output->setimageformat('pdf');
if ($output->writeimages(sprintf($outputFile, 'pdf'), TRUE) === TRUE) {
foreach ($files as $file) {
unlink($file);
}
return true;
}
return false;
}
private function getWatermarkCoordinates(\Imagick $image, \Imagick $watermark)
{
$iWidth = $image->getImageWidth();
$iHeight = $image->getImageHeight();
$wWidth = $watermark->getImageWidth();
$wHeight = $watermark->getImageHeight();
// calculate the position
$x = ($iWidth - $wWidth) / 2;
$y = ($iHeight - $wHeight) / 2;
return [$x, $y];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment