forked from netgen/HtmlPdfApi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlPdfApi.php
213 lines (185 loc) · 5.06 KB
/
HtmlPdfApi.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Netgen\HtmlPdfApi;
class HtmlPdfApi {
/**
* Client for sending requests
*
* @var HttpClientInterface
*/
protected $client;
/**
* Validator of parameters
*
* @var ValidatorInterface
*/
protected $validator;
/**
* Constructor
*
* @param HttpClientInterface $http_client
* @param ValidatorInterface $validator
*/
public function __construct(HttpClientInterface $http_client, ValidatorInterface $validator)
{
$this->client = $http_client;
$this->validator = $validator;
}
/**
* Generates PDF file from provided (relative) URL
*
* @param array $params Parameters for pdf generating (parameter 'url' must be set)
* @return response
* @throws \Exception
*/
public function generateFromURL($params)
{
if(empty($params['url']))
throw new \Exception('Parameter \'url\' must be set' );
$params = $this->validator->validate($params);
try {
return $this->client->sendRequest('pdf', $params, 'POST');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Generates PDF file from provided HTML string
*
* @param array $params Parameters for pdf generating (parameter 'html' must be set)
* @return response
* @throws \Exception
*/
public function generateFromHTML($params)
{
if(empty($params['html']))
throw new \Exception('Parameter \'html\' must be set' );
$params = $this->validator->validate($params);
try {
return $this->client->sendRequest('pdf', $params, 'POST');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Generates PDF file from provided file (html, zip, tar.gz...)
*
* @param array $params Parameters for pdf generating (parameter 'file' must be set)
* @return response
* @throws \Exception
*/
public function generateFromFile($params)
{
if(empty($params['file']))
throw new \Exception('Parameter \'file\' must be set' );
$params['file'] = '@'.$params['file'];
$params = $this->validator->validate($params);
$this->validator->validateHtmlFile($params['file']);
try {
return $this->client->sendRequest('pdf', $params, 'POST');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Gets number of available credits
*
* @return response
* @throws \Exception
*/
public function getCredits()
{
$params = array();
try {
return $this->client->sendRequest('credits', $params, 'GET');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Uploads new asset to HTMLPDFAPI server
*
* @param $filePath Path to the file to upload
* @return response
* @throws \Exception
*/
public function uploadAsset($filePath)
{
$params = array( 'file' => '@'.$filePath );
$params = $this->validator->validate($params);
$this->validator->validateAssetFile($params['file']);
try {
return $this->client->sendRequest('assets', $params, 'POST');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Downloads asset from server by id
*
* @param string $id ID of the asset
* @return response
* @throws \Exception
*/
public function getAsset($id)
{
$params = array( 'id' => $id );
$params = $this->validator->validate($params);
try {
return $this->client->sendRequest('assets/{id}', $params, 'GET');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Deletes asset by ID
*
* @param string $id ID of the asset
* @return response
* @throws \Exception
*/
public function deleteAsset($id)
{
$params = array( 'id' => $id );
$params = $this->validator->validate($params);
try {
return $this->client->sendRequest('assets/{id}', $params, 'DELETE');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Gets the list of available assets
*
* @return response
* @throws \Exception
*/
public function getAssetList()
{
$params = array();
try {
return $this->client->sendRequest('assets', $params, 'GET');
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Gets the asset ID by the name
*
* @param string $name Name of the asset
* @return string $id ID of the asset
* @throws \Exception
*/
public function getAssetID($name)
{
$list = $this->getAssetList();
$list_json = json_decode($list, true);
foreach ($list_json as $asset)
{
if ($asset['name'] == $name)
{
return $asset['id'];
}
}
throw new \Exception('No asset found by name');
}
}