use Cubist\PDF\PDFTools;
use Cubist\Util\Files\Files;
-class ProcessFile extends \Fluidbook\Tools\Jobs\ProcessFile {
- protected const DISTANT_ROOT = '/application/fluidbook/docs/';
- protected const LOCAL_ROOT = '/files/';
-
- protected const US_DISTANT_ROOT = '/application/usstorage/docs/';
- protected const US_LOCAL_ROOT = '/usfiles/';
-
- /**
- * @var string
- */
- protected $region = 'UE';
-
- protected $page;
- protected $out;
- protected $force = false;
- protected $mobileRatio = 1;
- protected $resolutionRatio = 1;
-
- protected $quality = 85;
- protected $filespath;
- protected $_job = null;
-
- public function __construct($out, $page, $resolutionRatio, $mobileRatio, $format = 'jpg', $resolution = 150, $quality = 85, $withGraphics = true, $withTexts = true, $version = 'html', $force = false) {
-
- error_log('Begin process file : [' . $out . ',' . $page . ']');
-
- if (stristr($out, 'usstorage')) {
- $this->setRegion('US');
- }
- $this->setOut($out);
- $this->setPage($page);
- $this->setForce(!!$force);
- $this->setResolutionRatio($resolutionRatio);
- $this->setMobileRatio($mobileRatio);
- $this->setQuality($quality);
- parent::__construct($format, $resolution, $quality, $withGraphics, $withTexts, $version);
- }
-
- /**
- * @param float $mobileRatio
- */
- public function setMobileRatio(float $mobileRatio): void {
- $this->mobileRatio = $mobileRatio;
- }
-
- /**
- * @param float $resolutionRatio
- */
- public function setResolutionRatio(float $resolutionRatio): void {
- $this->resolutionRatio = $resolutionRatio;
- }
-
- public function getMobileRatio(): float {
- return $this->mobileRatio;
- }
-
- public function getResolutionRatio(): float {
- return $this->resolutionRatio;
- }
-
-
- /**
- * @return bool
- */
- public function isForce(): bool {
- return $this->force;
- }
-
- /**
- * @param bool $force
- */
- public function setForce(bool $force): void {
- $this->force = $force;
- }
-
- /**
- * @param mixed $page
- */
- public function setPage($page): void {
- $this->page = $page;
- }
-
- public function getPage(): int {
- return $this->page;
- }
-
- public function getDistantOut() {
- return $this->out;
- }
-
- public function getLocalOut() {
- return $this->getOut();
- }
-
- public function getOut() {
- if ($this->getRegion() === 'UE') {
- return str_replace(self::DISTANT_ROOT, self::LOCAL_ROOT, $this->getDistantOut());
- } else {
- return str_replace(self::US_DISTANT_ROOT, self::US_LOCAL_ROOT, $this->getDistantOut());
- }
- }
-
- /**
- * @param mixed $out
- */
- public function setOut($out): void {
- $this->out = $out;
- }
-
- /**
- * @return string
- */
- public function getRegion(): string {
- return $this->region;
- }
-
- /**
- * @param string $region
- */
- public function setRegion(string $region): void {
- $this->region = $region;
- }
-
- public function process() {
- if ($this->getRegion() === 'UE') {
- return str_replace(self::LOCAL_ROOT, self::DISTANT_ROOT, $this->getPath($this->isForce()));
- } else {
- return str_replace(self::US_LOCAL_ROOT, self::US_DISTANT_ROOT, $this->getPath($this->isForce()));
- }
- }
-
- public function getPdf(): string|null {
- $res = Files::firstThatExists($this->getOut() . '/crop.pdf', $this->getOut() . '/fixed.pdf', $this->getOut() . '/original.pdf');
- if (null === $res) {
- echo 'no pdf in ' . $this->getOut();
- }
- return $res;
- }
-
- public function getSplittedPDFPage() {
- $res = $this->getOut() . 'pdf/p' . $this->getPage() . '.pdf';
- if (!file_exists($res)) {
- PDFTools::split($this->getPdf(), $this->getOut() . '/pdf');
- }
- return $res;
- }
+class ProcessFile extends \Fluidbook\Tools\Jobs\ProcessFile
+{
+ protected const DISTANT_ROOT = '/application/fluidbook/docs/';
+ protected const LOCAL_ROOT = '/files/';
+
+ protected const US_DISTANT_ROOT = '/application/usstorage/docs/';
+ protected const US_LOCAL_ROOT = '/usfiles/';
+
+ /**
+ * @var string
+ */
+ protected $region = 'UE';
+
+ protected $page;
+ protected $out;
+ protected $force = false;
+ protected $mobileRatio = 1;
+ protected $resolutionRatio = 1;
+
+ protected $quality = 85;
+ protected $filespath;
+ protected $_job = null;
+
+ public function __construct($out, $page, $resolutionRatio, $mobileRatio, $format = 'jpg', $resolution = 150, $quality = 85, $withGraphics = true, $withTexts = true, $version = 'html', $force = false)
+ {
+
+ error_log('Begin process file : [' . $out . ',' . $page . ']');
+
+ if (stristr($out, 'usstorage')) {
+ $this->setRegion('US');
+ }
+ $this->setOut($out);
+ $this->setPage($page);
+ $this->setForce(!!$force);
+ $this->setResolutionRatio($resolutionRatio);
+ $this->setMobileRatio($mobileRatio);
+ $this->setQuality($quality);
+ parent::__construct($format, $resolution, $quality, $withGraphics, $withTexts, $version);
+ }
+
+ /**
+ * @param float $mobileRatio
+ */
+ public function setMobileRatio(float $mobileRatio): void
+ {
+ $this->mobileRatio = $mobileRatio;
+ }
+
+ /**
+ * @param float $resolutionRatio
+ */
+ public function setResolutionRatio(float $resolutionRatio): void
+ {
+ $this->resolutionRatio = $resolutionRatio;
+ }
+
+ public function getMobileRatio(): float
+ {
+ return $this->mobileRatio;
+ }
+
+ public function getResolutionRatio(): float
+ {
+ return $this->resolutionRatio;
+ }
+
+ /**
+ * @return bool
+ */
+ public function isForce(): bool
+ {
+ return $this->force;
+ }
+
+ /**
+ * @param bool $force
+ */
+ public function setForce(bool $force): void
+ {
+ $this->force = $force;
+ }
+
+ /**
+ * @param mixed $page
+ */
+ public function setPage($page): void
+ {
+ $this->page = $page;
+ }
+
+ public function getPage(): int
+ {
+ return $this->page;
+ }
+
+ public function getDistantOut()
+ {
+ return $this->out;
+ }
+
+ public function getLocalOut()
+ {
+ return $this->getOut();
+ }
+
+ public function getOut()
+ {
+ if ($this->getRegion() === 'UE') {
+ return str_replace(self::DISTANT_ROOT, self::LOCAL_ROOT, $this->getDistantOut());
+ } else {
+ return str_replace(self::US_DISTANT_ROOT, self::US_LOCAL_ROOT, $this->getDistantOut());
+ }
+ }
+
+ /**
+ * @param mixed $out
+ */
+ public function setOut($out): void
+ {
+ $this->out = $out;
+ }
+
+ /**
+ * @return string
+ */
+ public function getRegion(): string
+ {
+ return $this->region;
+ }
+
+ /**
+ * @param string $region
+ */
+ public function setRegion(string $region): void
+ {
+ $this->region = $region;
+ }
+
+ public function process()
+ {
+ if ($this->getRegion() === 'UE') {
+ return str_replace(self::LOCAL_ROOT, self::DISTANT_ROOT, $this->getPath($this->isForce()));
+ } else {
+ return str_replace(self::US_LOCAL_ROOT, self::US_DISTANT_ROOT, $this->getPath($this->isForce()));
+ }
+ }
+
+ public function getPdf(): string|null
+ {
+ $res = Files::firstThatExists($this->getOut() . '/crop.pdf', $this->getOut() . '/fixed.pdf', $this->getOut() . '/original.pdf');
+ if (null === $res) {
+ echo 'no pdf in ' . $this->getOut();
+ }
+ return $res;
+ }
+
+ /**
+ * @return int
+ * @throws \Exception
+ */
+ public function getPDFPagesNumber()
+ {
+ $infos = PDFTools::infos($this->getPdf());
+ if ($infos['pages']) {
+ return (int)$infos['pages'];
+ }
+ return 0;
+ }
+
+ /**
+ * @return string
+ * @throws \Exception
+ */
+ public function getSplittedPDFPage()
+ {
+ $res = $this->getOut() . 'pdf/p' . $this->getPage() . '.pdf';
+ if (!file_exists($res)) {
+ if ($this->getPage() > $this->getPDFPagesNumber()) {
+ throw new \Exception('PDF document has ' . $this->getPDFPagesNumber() . ' and you are requestion the page ' . $this->getPage());
+ }
+ PDFTools::split($this->getPdf(), $this->getOut() . '/pdf');
+ }
+ return $res;
+ }
}
\ No newline at end of file