Skip to content

Commit 6cfcc04

Browse files
Merge pull request #5 from tanelt/master
Readme update and data parsing fix
2 parents a4170c0 + c685409 commit 6cfcc04

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# pdfgeneratorapi-php
2-
PHP API wrapper for pdfgeneratorapi.com
1+
# REST API wrapper for pdfgeneratorapi.com
2+
REST API wrapper for [PDF Generator API](https://pdfgeneratorapi.com).
3+
4+
### Install
5+
Require this package with composer using the following command:
6+
```bash
7+
composer require actualreports/pdfgeneratorapi-php
8+
```
9+
10+
### Usage
11+
```php
12+
$client = new \ActualReports\PDFGeneratorAPI\Client($token, $secret);
13+
$client->setWorkspace('[email protected]');
14+
```
15+
16+
```php
17+
$data = [
18+
"DocNumber" => "12312",
19+
"TotalAmt" => 1231.12
20+
];
21+
$content = $client->output(21650, $data);
22+
```
23+
24+
```php
25+
header('Content-type: '.$content->meta->{'content-type'});
26+
header('Cache-Control: private, must-revalidate, post-check=0, pre-check=0, max-age=1');
27+
header('Pragma: public');
28+
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
29+
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
30+
header('Content-Disposition: inline; filename="'.$content->meta->name.'"');
31+
die(base64_decode($content->response));
32+
```

src/Client.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,25 @@ public function setBaseUrl($url)
8787
}
8888

8989
/**
90-
* @param array|string $data
90+
* Turns data into string,
9191
*
92-
* @return array|mixed
92+
* @param array|string|\stdClass $data
93+
*
94+
* @return string
9395
*/
9496
protected static function data($data)
9597
{
96-
if (is_string($data))
98+
if (!is_string($data))
9799
{
98100
try
99101
{
100-
$data = \GuzzleHttp\json_decode($data);
102+
$data = \GuzzleHttp\json_encode($data);
101103
}
102104
catch (\InvalidArgumentException $e)
103105
{
104106

105107
}
106108
}
107-
else
108-
{
109-
$data = (array) $data;
110-
}
111109

112110
return $data;
113111
}
@@ -192,16 +190,17 @@ public function request($method = self::REQUEST_POST, $resource, array $params =
192190
])
193191
];
194192

195-
$params['data'] = isset($params['data']) ? self::data($params['data']) : null;
193+
if ($params['data'])
194+
{
195+
$params['data'] = self::data($params['data']);
196+
}
196197

197-
if ($method === self::REQUEST_POST)
198+
if ($method === self::REQUEST_POST && $params['data'])
198199
{
199-
if ($params['data'] && is_array($params['data']))
200-
{
201-
$options['body'] = \GuzzleHttp\json_encode($params['data']);
202-
unset($params['data']);
203-
}
200+
$options['body'] = $params['data'];
201+
unset($params['data']);
204202
}
203+
205204
$options['query'] = $params;
206205
return $this->handleRequest($method, $resource, $options);
207206
}
@@ -399,8 +398,7 @@ public function editor($template, $data = null, array $params = [])
399398

400399
if ($data)
401400
{
402-
$data = self::data($data);
403-
$params['data'] = is_array($data) ? \GuzzleHttp\json_encode($data) : $data;
401+
$params['data'] = self::data($data);
404402
}
405403

406404
return $this->baseUrl.$resource.'?'.http_build_query($params);

tests/Client.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ public function testOutputDataUrl()
5959
$this->assertEquals('application/pdf', $result->meta->{'content-type'});
6060
}
6161

62+
public function testOutputDataString()
63+
{
64+
$result = $this->client->output($this->templateId, \GuzzleHttp\json_encode(['DocNumber' => 1123123123]));
65+
$this->assertEquals('application/pdf', $result->meta->{'content-type'});
66+
}
67+
6268
public function testEditorDataUrl()
6369
{
6470
$url = $this->client->editor($this->templateId, $this->host.'/assets/web/data/qbo_invoice.json');

0 commit comments

Comments
 (0)