-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tabula.php
155 lines (126 loc) · 3.84 KB
/
Tabula.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
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;
use Symfony\Component\Process\Process;
/**
* laravel-tabula
*
* laravel-tabula is a tool for liberating data tables trapped inside PDF files for the Laravel framework.
* This package was inspired by Python’s tabula-py package.
*
*
* Refer to this route for the original wrapped java source code.
* https://github.com/tabulapdf/tabula
* https://github.com/tabulapdf/tabula-java
*
**/
class Tabula
{
/**
* Additional dir to check for java executable
* @var
*/
private $binDir = [];
/**
* Path to jar file
*/
private $jarArchive = __DIR__ . '/../../../lib/tabula-1.0.4-jar-with-dependencies.jar';
/**
* Tabula constructor.
* @param null $binDir
*/
public function __construct($binDir = null)
{
if ($binDir) {
$this->binDir = is_array($binDir) ? $binDir : [$binDir];
}
}
/**
* @return string
*/
public function getJarArchive()
{
return $this->jarArchive;
}
/**
* @param string $jarArchive
*/
public function setJarArchive($jarArchive)
{
$this->jarArchive = $jarArchive;
}
/**
* @return array|null
*/
public function getBinDir()
{
return $this->binDir;
}
/**
* @param $binDir
*/
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)
{
$parameters = [];
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]);
} else {
throw new InvalidArgumentException('Invalid Format. ex) CSV, TSV, JSON');
}
if(!is_dir($target)) {
throw new InvalidArgumentException('Folder to target Pdf does not exist.');
}
if(Str::endsWith($target, '/')) {
$target = Str::replaceLast('/','', $target);
}
$parameters = array_merge($parameters, ['-b', $target]);
if(!is_dir($output)) {
throw new InvalidArgumentException('Folder to output Pdf does not exist.');
}
if(Str::endsWith($output, '/')) {
$output = Str::replaceLast('/','', $output);
}
$parameters = array_merge($parameters, ['-g', '-p', 'all']);
$finder = new ExecutableFinder();
$binary = $finder->find('java', null, $this->binDir);
if ($binary === null) {
throw new RuntimeException('Could not find java on your system.');
}
$arguments = array_merge(
['java', '-Xmx256m', '-Djava.awt.headless=true', '-Dfile.encoding=UTF8', '-jar', $this->getJarArchive()],
$parameters
);
$process = new Process($arguments);
$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);
if(Str::endsWith($basename,"." . strtolower($format))) {
File::move( File::dirname($file). '/' . $basename, $output.'/'.$basename);
}
}
}
}
}