1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Netgen\HtmlPdfApi; |
4
|
|
|
|
5
|
|
|
class HtmlPdfApi { |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Client for sending requests |
9
|
|
|
* |
10
|
|
|
* @var HttpClientInterface |
11
|
|
|
*/ |
12
|
|
|
protected $client; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validator of parameters |
16
|
|
|
* |
17
|
|
|
* @var ValidatorInterface |
18
|
|
|
*/ |
19
|
|
|
protected $validator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param HttpClientInterface $http_client |
25
|
|
|
* @param ValidatorInterface $validator |
26
|
|
|
*/ |
27
|
|
|
public function __construct(HttpClientInterface $http_client, ValidatorInterface $validator) |
28
|
|
|
{ |
29
|
|
|
$this->client = $http_client; |
30
|
|
|
$this->validator = $validator; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Generates PDF file from provided (relative) URL |
35
|
|
|
* |
36
|
|
|
* @param array $params Parameters for pdf generating (parameter 'url' must be set) |
37
|
|
|
* @return response |
|
|
|
|
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function generateFromURL($params) |
41
|
|
|
{ |
42
|
|
|
if(empty($params['url'])) |
43
|
|
|
throw new \Exception('Parameter \'url\' must be set' ); |
44
|
|
|
|
45
|
|
|
$params = $this->validator->validate($params); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
return $this->client->sendRequest('pdf', $params, 'POST'); |
49
|
|
|
} catch (\Exception $ex) { |
50
|
|
|
throw $ex; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Generates PDF file from provided HTML string |
57
|
|
|
* |
58
|
|
|
* @param array $params Parameters for pdf generating (parameter 'html' must be set) |
59
|
|
|
* @return response |
60
|
|
|
* @throws \Exception |
61
|
|
|
*/ |
62
|
|
|
public function generateFromHTML($params) |
63
|
|
|
{ |
64
|
|
|
if(empty($params['html'])) |
65
|
|
|
throw new \Exception('Parameter \'html\' must be set' ); |
66
|
|
|
|
67
|
|
|
$params = $this->validator->validate($params); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
return $this->client->sendRequest('pdf', $params, 'POST'); |
71
|
|
|
} catch (\Exception $ex) { |
72
|
|
|
throw $ex; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generates PDF file from provided file (html, zip, tar.gz...) |
78
|
|
|
* |
79
|
|
|
* @param array $params Parameters for pdf generating (parameter 'file' must be set) |
80
|
|
|
* @return response |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
|
public function generateFromFile($params) |
84
|
|
|
{ |
85
|
|
|
if(empty($params['file'])) |
86
|
|
|
throw new \Exception('Parameter \'file\' must be set' ); |
87
|
|
|
|
88
|
|
|
$params['file'] = '@'.$params['file']; |
89
|
|
|
$params = $this->validator->validate($params); |
90
|
|
|
$this->validator->validateHtmlFile($params['file']); |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
return $this->client->sendRequest('pdf', $params, 'POST'); |
94
|
|
|
} catch (\Exception $ex) { |
95
|
|
|
throw $ex; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Gets number of available credits |
101
|
|
|
* |
102
|
|
|
* @return response |
103
|
|
|
* @throws \Exception |
104
|
|
|
*/ |
105
|
|
|
public function getCredits() |
106
|
|
|
{ |
107
|
|
|
$params = array(); |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
return $this->client->sendRequest('credits', $params, 'GET'); |
111
|
|
|
} catch (\Exception $ex) { |
112
|
|
|
throw $ex; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Uploads new asset to HTMLPDFAPI server |
118
|
|
|
* |
119
|
|
|
* @param $filePath Path to the file to upload |
|
|
|
|
120
|
|
|
* @return response |
121
|
|
|
* @throws \Exception |
122
|
|
|
*/ |
123
|
|
|
public function uploadAsset($filePath) |
124
|
|
|
{ |
125
|
|
|
$params = array( 'file' => '@'.$filePath ); |
126
|
|
|
$params = $this->validator->validate($params); |
127
|
|
|
$this->validator->validateAssetFile($params['file']); |
128
|
|
|
|
129
|
|
|
try { |
130
|
|
|
return $this->client->sendRequest('assets', $params, 'POST'); |
131
|
|
|
} catch (\Exception $ex) { |
132
|
|
|
throw $ex; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Downloads asset from server by id |
138
|
|
|
* |
139
|
|
|
* @param string $id ID of the asset |
140
|
|
|
* @return response |
141
|
|
|
* @throws \Exception |
142
|
|
|
*/ |
143
|
|
|
public function getAsset($id) |
144
|
|
|
{ |
145
|
|
|
$params = array( 'id' => $id ); |
146
|
|
|
$params = $this->validator->validate($params); |
147
|
|
|
|
148
|
|
|
try { |
149
|
|
|
return $this->client->sendRequest('assets/{id}', $params, 'GET'); |
150
|
|
|
} catch (\Exception $ex) { |
151
|
|
|
throw $ex; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Deletes asset by ID |
157
|
|
|
* |
158
|
|
|
* @param string $id ID of the asset |
159
|
|
|
* @return response |
160
|
|
|
* @throws \Exception |
161
|
|
|
*/ |
162
|
|
|
public function deleteAsset($id) |
163
|
|
|
{ |
164
|
|
|
$params = array( 'id' => $id ); |
165
|
|
|
$params = $this->validator->validate($params); |
166
|
|
|
|
167
|
|
|
try { |
168
|
|
|
return $this->client->sendRequest('assets/{id}', $params, 'DELETE'); |
169
|
|
|
} catch (\Exception $ex) { |
170
|
|
|
throw $ex; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Gets the list of available assets |
176
|
|
|
* |
177
|
|
|
* @return response |
178
|
|
|
* @throws \Exception |
179
|
|
|
*/ |
180
|
|
|
public function getAssetList() |
181
|
|
|
{ |
182
|
|
|
$params = array(); |
183
|
|
|
|
184
|
|
|
try { |
185
|
|
|
return $this->client->sendRequest('assets', $params, 'GET'); |
186
|
|
|
} catch (\Exception $ex) { |
187
|
|
|
throw $ex; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Gets the asset ID by the name |
193
|
|
|
* |
194
|
|
|
* @param string $name Name of the asset |
195
|
|
|
* @return string $id ID of the asset |
196
|
|
|
* @throws \Exception |
197
|
|
|
*/ |
198
|
|
|
public function getAssetID($name) |
199
|
|
|
{ |
200
|
|
|
$list = $this->getAssetList(); |
201
|
|
|
|
202
|
|
|
$list_json = json_decode($list, true); |
203
|
|
|
|
204
|
|
|
foreach ($list_json as $asset) |
205
|
|
|
{ |
206
|
|
|
if ($asset['name'] == $name) |
207
|
|
|
{ |
208
|
|
|
return $asset['id']; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
throw new \Exception('No asset found by name'); |
212
|
|
|
} |
213
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths