Skip to content

Instantly share code, notes, and snippets.

@vuthaihoc
Last active July 8, 2017 02:55
Show Gist options
  • Save vuthaihoc/18f6495becf981e258047609b1603531 to your computer and use it in GitHub Desktop.
Save vuthaihoc/18f6495becf981e258047609b1603531 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: hocvt
* Date: 6/6/17
* Time: 15:01
*/
namespace App\Colombo;
use Symfony\Component\Process\Process;
abstract class CanRunCommand {
protected $process;
protected $process_options = [];
private $command;
protected $bin;
protected $timeout = 300;
/**
* CanRunCommand constructor.
*
* @param string $bin
*/
public function __construct($bin = '') {
$this->bin($bin);
$this->process = new Process('');
$this->process->setTimeout($this->timeout);
}
protected function validateRun()
{
$status = $this->process->getExitCode();
$error = $this->process->getErrorOutput();
if ($status !== 0 and $error !== '') {
throw new \RuntimeException(
sprintf(
"The exit status code %s says something went wrong:\n stderr: %s\n stdout: %s\ncommand: %s.",
$status,
$error,
$this->process->getOutput(),
$this->command
)
);
}
}
protected function buildCommand($append = ''){
$command = $this->bin;
$command .= " " . $this->buildOptions();
return $command . $append;
}
protected function buildOptions(){
$options = ' ';
foreach($this->process_options as $k => $v){
if($v !== false){
$options .= $k . " ";
}else{
continue;
}
if($v !== true){
$options .= $v . " ";
}
}
return $options;
}
public function run($command)
{
$this->command = escapeshellcmd($command);
$this->process->setCommandLine($this->command);
$this->process->run();
$this->validateRun();
return $this;
}
public function bin($bin = ''){
if(!empty($bin)){
$this->bin = $bin;
}
return $this->bin;
}
public function timeout($timeout = ''){
if(!empty($timeout)){
$this->timeout = $timeout;
}
return $this->timeout;
}
public function options($key = null, $value = null){
if(is_array($key)){
if($value === true){
$this->process_options = $key;
}else{
$this->process_options = array_merge($this->process_options, $key);
}
}elseif ($key != null){
if($value !== null){
$this->process_options[$key] = $value;
}
return $this->process_options;
}
return $this->process_options;
}
public function output()
{
return $this->process->getOutput();
}
}
<?php
/**
* Created by PhpStorm.
* User: hocvt
* Date: 6/7/17
* Time: 09:40
*/
namespace App\Colombo\Converter;
use App\Colombo\CanRunCommand;
use Carbon\Carbon;
class PdfInfo extends CanRunCommand {
/**
* PdfInfo constructor.
*
* @param string $bin
*/
public function __construct($bin = 'pdfinfo') {
parent::__construct($bin);
}
public function read($path){
$command = $this->buildCommand($path);
$this->run($command);
return $this->parseResult();
}
private function parseResult(){
$output = $this->output();
$lines = preg_split("/\n/", $output);
$output = [];
foreach ($lines as $line){
$exploded = preg_split("/\:\s+/", $line);
if(count($exploded) == 2){
if(stripos($exploded[0], 'date')){
$value = Carbon::parse($exploded[1]);
}else{
$value = $exploded[1];
}
$key = mb_strtolower(str_replace(" ", "_", $exploded[0]));
$output[$key] = $value;
}
}
return $output;
}
}
<?php
/**
* Created by PhpStorm.
* User: hocvt
* Date: 6/6/17
* Time: 14:45
*/
namespace App\Colombo\Converter;
use App\Colombo\CanRunCommand;
class PdftotextConverter extends CanRunCommand {
private $tmp_folder;
protected $process;
protected $process_options = [
'-nodrm' => true,
'-zoom' => 1,
'-i' => true,
'-c' => true,
'-l' => 2,
];
public function __construct($bin = 'pdfinfo', $cache = '') {
$this->tmp_folder = storage_path('framework/pdftotext');
parent::__construct($bin);
}
private function checkWritable(){
if(!file_exists($this->tmp_folder)){
@mkdir($this->tmp_folder);
}
$this->tmp_folder = tempnam($this->tmp_folder, 'pdftohtml_');
@unlink($this->tmp_folder);
@mkdir($this->tmp_folder);
if(!file_exists($this->tmp_folder)){
throw new \Exception("Can not read/write tmp folder");
}
}
/**
* Main feature
*
* @param $path
* @param int $page default convert 2 pages
*
* @return array
*/
public function getHtmlPages($path, $page = 3){
$this->checkWritable();
$this->options('-l', $page);
$tmp_path = $this->tmp_folder . DIRECTORY_SEPARATOR . "source.pdf";
copy($path, $tmp_path);
$command = $this->buildCommand($tmp_path);
$this->run($command);
$pages = [];
for($i = 1; $i <= $page; $i++){
$page_path = $this->tmp_folder . DIRECTORY_SEPARATOR . "source-" . $i . ".html";
if(file_exists($page_path)){
$pages[$i] = file_get_contents($page_path);
}else{
break;
}
}
return $pages;
}
/** Helper functions */
/**
* Delete cache file before this time
*
* @param PdftotextConverter $instant
* @param $time
*/
public static function clean(PdftotextConverter $instant = null, $time = null){
$instant = $instant == null ? new self() : $instant;
$folder = glob($instant->tmp_folder . DIRECTORY_SEPARATOR . "*");
foreach ($folder as $file){
@unlink($file);
}
@rmdir($instant->tmp_folder);
}
function __destruct() {
self::clean($this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment