namespace Cubist\Util\CommandLine;
+use Cubist\PDF\PDFTools;
use Cubist\Util\CommandLine;
use Cubist\Util\Files\Files;
use Cubist\Util\Math;
'quality' => '95',
'antialias' => true,
'format' => 'jpeg',
- 'internalformat' => 'png');
+ 'internalformat' => 'png',
+ 'texts' => true,
+ 'background' => true,
+ );
+
$options = array_merge($defaultOptions, $options);
$options['factor'] = $options['resolution'] / self::RESOLUTION_FACTOR;
-
- if(isset($options['texts']) && $options['texts']){
- unset($options['texts']);
- }
-
- if ($options['format'] === 'jpeg') {
- $extension = 'jpg';
- } else {
- $extension = $options['format'];
+ if ($options['format'] === 'svg') {
+ $options['internalformat'] = 'svg';
}
+ $extension = self::_format2ext($options['format']);
if (null !== $to) {
$res = $to . '.' . $extension;
Files::getLock($lockFile);
}
- $pdftoppm = new CommandLine('pdftoppm');
- $pdftoppm->setArg('r', $options['resolution']);
- if ($extension === 'png') {
- $pdftoppm->setArg('-png');
- } elseif ($extension === 'jpg') {
- $pdftoppm->setArg('-jpeg');
- $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"');
+ if ($options['format'] === 'svg') {
+ $pdftocairo = new CommandLine('pdftocairo');
+ $pdftocairo->setArg('-svg');
+ $pdftocairo->setArg('-paperw',$rect['width']);
+ $pdftocairo->setArg('-paperh',$rect['height']);
+ self::_crop($pdftocairo, $page, $rect, 1, 1);
+ self::_exec($pdftocairo, $file, $dest . '.' . $extension);
+ if (!$options['background']) {
+ PDFTools::makeTextSVGFile($dest . '.' . $extension, $dest . '.' . $extension);
+ }
+ } else {
+ $pdftoppm = new CommandLine('pdftoppm');
+ if ($extension === 'png') {
+ $pdftoppm->setArg('-png');
+ } elseif ($extension === 'jpg') {
+ $pdftoppm->setArg('-jpeg');
+ $pdftoppm->setArg('-jpegopt', '"quality=' . $options['quality'] . '"');
+ }
+ $aa = $options['antialias'] ? 'yes' : 'no';
+ $pdftoppm->setArg('-aa', $aa);
+ $pdftoppm->setArg('-aaVector', $aa);
+ $pdftoppm->setArg('-cropbox');
+ $pdftoppm->setArg('-freetype', (!isset($options['texts']) || $options['texts']) ? 'yes' : 'no');
+ $pdftoppm->setArg('-singlefile');
+ self::_crop($pdftoppm, $page, $rect, $options['resolution'], $options['factor']);
+ self::_exec($pdftoppm, $file, $dest);
}
- $aa = $options['antialias'] ? 'yes' : 'no';
- $pdftoppm->setArg('-aa', $aa);
- $pdftoppm->setArg('-aaVector', $aa);
- $pdftoppm->setArg('-cropbox');
- $pdftoppm->setArg('-freetype', (!isset($options['texts']) || $options['texts']) ? 'yes' : 'no');
- $pdftoppm->setArg('-singlefile');
- $pdftoppm->setArg('-f', $page);
- $pdftoppm->setArg('-l', $page);
- $pdftoppm->setArg('-x', round($rect['x'] * $options['factor']));
- $pdftoppm->setArg('-y', round($rect['y'] * $options['factor']));
- $pdftoppm->setArg('-W', round($rect['width'] * $options['factor']));
- $pdftoppm->setArg('-H', round($rect['height'] * $options['factor']));
- $pdftoppm->setArg(null, $file);
- $pdftoppm->setArg(null, $dest);
- $pdftoppm->execute();
if (isset($lockFile)) {
Files::releaseLock($lockFile);
return $res;
}
+ /**
+ * @param $cli CommandLine
+ * @param $page int
+ * @param $rect array
+ * @param $resolution float
+ * @param $factor float
+ * @return void
+ */
+ protected static function _crop($cli, $page, $rect, $resolution, $factor = 1)
+ {
+ $cli->setArg('-f', $page);
+ $cli->setArg('-l', $page);
+ $cli->setArg('-r', $resolution);
+ $cli->setArg('-x', round($rect['x'] * $factor));
+ $cli->setArg('-y', round($rect['y'] * $factor));
+ $cli->setArg('-W', round($rect['width'] * $factor));
+ $cli->setArg('-H', round($rect['height'] * $factor));
+ }
+
+
+ /**
+ * @param $cli CommandLine
+ * @param $file string
+ * @param $dest string
+ * @return void
+ */
+ protected static function _exec($cli, $file, $dest, $debug = false)
+ {
+ $cli->setArg(null, $file);
+ $cli->setArg(null, $dest);
+ $cli->execute();
+ if ($debug) {
+ $cli->debug();
+ }
+ }
+
protected static function _pdftojpeg($file, $to, $nbpages, $options = array())
{
/*