<?php
 namespace App\Utils\Wkhtmltopdf; class CreatePdf { private $htmlContent; private $orientation = 'portrait'; public function __construct($htmlContent) { $this->htmlContent = $htmlContent; } public function setOrientation($orientation) { if ($orientation=='landscape' or $orientation=='portrait') { $this->orientation = $orientation; } else { throw new \Exception('Invalid orientation set for PDF - '.$orientation); } } public function writePdf($outputFile) { $pdfInstallerCfg = \App\Utils\Wkhtmltopdf\Installer::getInstance(); $outputPath = \Engine\File::getPath($outputFile); if (!\Engine\Dir::check($outputPath)) { throw new \Exception\IO\Base(sprintf("Could not check the path of the output file (%s)", $outputPath)); } $pdfInstallerCfg->install(); $tempFileName = 'wkhtmltopdf_src_'.uniqid().'.html'; $tempFileNameWithPath = realpath(\Engine\File::tempnam($tempFileName)); if (!$tempFileNameWithPath) { throw new \Exception\IO\Base(sprintf("Could not create temporary file with html content (%s)", $tempFileNameWithPath)); } if (!\Engine\File::create($tempFileNameWithPath, $this->htmlContent)) { throw new \Exception\IO\Base(sprintf("Could not copy HTML content onto temporary (%s)", $tempFileNameWithPath)); } $currentDir = getcwd(); chdir($pdfInstallerCfg->getLibFullPath()); $command = sprintf("./wkhtmltopdf --print-media-type --footer-right [page]/[topage] --orientation %s %s %s", $this->orientation, escapeshellarg($tempFileNameWithPath), escapeshellarg($outputFile)); $processObject = new \Engine\Os\Command\Process($command); $processObject->runAndCaptureErrors(); $outputObject = $processObject->getCommandOutputObj(); chdir($currentDir); \Engine\File::delete($tempFileNameWithPath); return $outputObject; } } 