Skip to content

Commit

Permalink
add v2
Browse files Browse the repository at this point in the history
  • Loading branch information
initred committed Feb 8, 2020
1 parent ba11297 commit 09033e8
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 44 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ composer require initred/laravel-tabula
[Windows]

http:https://www.oracle.com/technetwork/java/javase/downloads/index.html.
Please System Path Adding.

[Mac os]

Expand All @@ -39,10 +40,18 @@ sudo dnf install java-latest-openjdk

```
$tabula = new Tabula('/usr/bin/');
$tabula->parse(
'json',
$tabula->convertInto(
storage_path('app/public/pdf/test.pdf'),
storage_path('app/public/json/test.csv'),
'csv',
'all'
);
$tabula->convertIntoByBatch(
storage_path('app/public/pdf'),
storage_path('app/public/json')
'json',
'all'
);
```

Expand Down
290 changes: 249 additions & 41 deletions src/InitRed/Tabula/Tabula.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace InitRed\Tabula;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
Expand All @@ -24,6 +22,12 @@

class Tabula
{
public $os;
public $encoding = 'utf-8';
public $javaOptions = [];
public $options = [];
public $input = null;

/**
* Additional dir to check for java executable
* @var
Expand All @@ -39,13 +43,47 @@ class Tabula
* Tabula constructor.
* @param null $binDir
*/
public function __construct($binDir = null)
public function __construct($binDir = null, $encoding = 'utf-8')
{
$this->osCheck();
$this->encoding = $encoding;

if ($binDir) {
$this->binDir = is_array($binDir) ? $binDir : [$binDir];
}
}

public function osCheck()
{
if (stripos(PHP_OS, 'win') === 0) {
$this->os = 'Window';
} elseif (stripos(PHP_OS, 'darwin') === 0) {
$this->os = 'Mac';
} elseif (stripos(PHP_OS, 'linux') === 0) {
$this->os = 'Linux';
}
}

public function isEncodeUTF8()
{
return $this->encoding === 'utf-8';
}

public function isOsWindow()
{
return $this->os === 'Window';
}

public function isOsMac()
{
return $this->os === 'Mac';
}

public function isLinux()
{
return $this->os === 'Linux';
}

/**
* @return string
*/
Expand Down Expand Up @@ -78,78 +116,248 @@ public function setBinDir($binDir)
$this->binDir = $binDir;
}

/**
* @param null $format
* @param null $target
* @param null $output
*/
public function parse($format = null, $target = null, $output = null)
public function buildJavaOptions()
{
$parameters = [];
$javaOptions = ['java'];

$finder = new ExecutableFinder();
$binary = $finder->find('java', null, $this->binDir);

if ($binary === null) {
throw new RuntimeException('Could not find java on your system.');
}

array_push($javaOptions, '-Xmx256m');

if($this->isOsMac()) {
array_push($javaOptions, '-Djava.awt.headless=true');
}

if($this->isEncodeUTF8()) {
array_push($javaOptions, '-Dfile.encoding=UTF8');
}

array_push($javaOptions, '-jar');
array_push($javaOptions, $this->getJarArchive());

$this->javaOptions = $javaOptions;
}

public function extractFormatForConversion($format)
{
if(empty($format)) {
throw new InvalidArgumentException('Convert format does not exist.');
}

$format = strtoupper($format);

if($format === 'CSV' || $format === 'TSV' || $format === 'JSON') {
$parameters = array_merge($parameters, ['-f', $format]);
if ($format === 'CSV' || $format === 'TSV' || $format === 'JSON') {
return $format;
} else {
throw new InvalidArgumentException('Invalid Format. ex) CSV, TSV, JSON');
}
}

if(!is_dir($target)) {
public function existFileCheck($path)
{
if(!file_exists($path)) {
throw new InvalidArgumentException('File does not exist.');
}

return true;
}

public function existDirectoryCheck($path)
{
if(!is_dir($path)) {
throw new InvalidArgumentException('Folder to target Pdf does not exist.');
}
}

public function addInputOption($input)
{
$this->input = $input;
}

/**
* @param null $pages
* @param bool $guess
* @param null $area
* @param bool $relativeArea
* @param bool $lattice
* @param bool $stream
* @param null $password
* @param bool $silent
* @param null $columns
* @param null $format
* @param null $batch
* @param null $outputPath
* @param string $options
*/
public function buildOptions(
$pages = null,
$guess = true,
$area = null,
$relativeArea = false,
$lattice = false,
$stream = false,
$password = null,
$silent = false,
$columns = null,
$format = null,
$batch = null,
$outputPath = null,
$options = ''
) {
$this->options = [];

$buildOptions = [];

if(!is_null($this->input)) {
if($this->existFileCheck($this->input)) {
$buildOptions = array_merge($buildOptions, [$this->input]);
}
}

if(Str::endsWith($target, '/')) {
$target = Str::replaceLast('/','', $target);
if(!is_null($pages)) {
if($pages === 'all') {
$buildOptions = array_merge($buildOptions, ['--page', 'all']);
} else {
$area = implode( ',', $pages);
$buildOptions = array_merge($buildOptions, ['--page', $pages]);
}
}

$parameters = array_merge($parameters, ['-b', $target]);
$multipleArea = false;

if(!is_dir($output)) {
throw new InvalidArgumentException('Folder to output Pdf does not exist.');
if(!is_null($area)) {
$guess = false;

foreach($area as $key => $value) {
if(substr( $value, 0, 1 ) === '%') {
if($relativeArea) {
$area[$key] = str_replace('%', '', $area[$key]);
}
}
}

$area = implode( ',', $area);

$buildOptions = array_merge($buildOptions, ["--area", $area]);
}

if(Str::endsWith($output, '/')) {
$output = Str::replaceLast('/','', $output);
if($lattice) {
$buildOptions = array_merge($buildOptions, ["--lattice"]);
}

$parameters = array_merge($parameters, ['-g', '-p', 'all']);
if($stream) {
$buildOptions = array_merge($buildOptions, ["--stream"]);
}

$finder = new ExecutableFinder();
$binary = $finder->find('java', null, $this->binDir);
if($guess && !$multipleArea) {
$buildOptions = array_merge($buildOptions, ["--guess"]);
}

if ($binary === null) {
throw new RuntimeException('Could not find java on your system.');
if(!is_null($format)) {

$format = $this->extractFormatForConversion($format);
$buildOptions = array_merge($buildOptions, ["--format", $format]);
}

$arguments = array_merge(
['java', '-Xmx256m', '-Djava.awt.headless=true', '-Dfile.encoding=UTF8', '-jar', $this->getJarArchive()],
$parameters
);
if(!is_null($outputPath)) {
$buildOptions = array_merge($buildOptions, ["--outfile", $outputPath]);
}

if(!is_null($columns)) {
$columns = implode( ',', $columns );
$buildOptions = array_merge($buildOptions, ["--columns", $columns]);
}

if(!is_null($password)) {
$buildOptions = array_merge($buildOptions, ["--password", $password]);
}

$process = new Process($arguments);
if(!is_null($batch)) {

if(!is_dir($batch)) {
throw new InvalidArgumentException('Folder to output Pdf does not exist.');
}

$buildOptions = array_merge($buildOptions, ['--batch', $batch]);
}

if($silent) {
$buildOptions = array_merge($buildOptions, ["--silent"]);
}

$this->options = $buildOptions;
}

public function run()
{
$parameters = array_merge($this->javaOptions, $this->options);

$process = new Process($parameters);
$process->run();

if (!$process->isSuccessful()) {
throw new RuntimeException($process->getErrorOutput());
}

$process->getOutput();
}

if($output) {
$fileLists = File::allFiles($target);

foreach($fileLists as $file) {
$basename = File::basename($file);
/**
* @param $input
* @param $output
* @param string $format
* @param string $pages
*/
public function convertInto($input, $output, $format = 'csv', $pages = 'all')
{
self::buildJavaOptions();
self::addInputOption($input);
self::buildOptions(
$pages,
true,
null,
false,
false,
false,
null,
false,
null,
$format,
null,
$output,
null
);
self::run();
}

if(Str::endsWith($basename,"." . strtolower($format))) {
File::move( File::dirname($file). '/' . $basename, $output.'/'.$basename);
}
}
}
/**
* @param $directory
* @param string $format
* @param string $pages
*/
public function convertIntoByBatch($directory, $format = 'csv', $pages = 'all')
{
self::buildJavaOptions();
self::buildOptions(
$pages,
true,
null,
false,
false,
false,
null,
false,
null,
$format,
$directory,
null,
null
);
self::run();
}
}
}

0 comments on commit 09033e8

Please sign in to comment.