Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit b37298a

Browse files
committed
Added custom PDF object resource
1 parent 45077fe commit b37298a

File tree

4 files changed

+152
-5
lines changed

4 files changed

+152
-5
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,53 @@ $bee = new Bee(new \GuzzleHttp\Client);
2626
$bee->setApiToken('your-api-token-here');
2727
```
2828

29+
### Using Message Services API: HTML
30+
31+
This API allows you to post the JSON content generated on the Bee editor, and it will return the rendered HTML of the JSON. Reference: https://docs.beefree.io/message-services-api-reference/#html
32+
33+
```php
34+
$beeEditorData = []; // this will be the JSON generated using the editor
35+
$html = $bee->html($beeEditorData);
36+
```
37+
38+
### Using Message Services API: PDF
39+
40+
This API allows you to post the HTML content and get an URL with a PDF created from the HTML. Reference: https://docs.beefree.io/message-services-api-reference/#pdf
41+
42+
```php
43+
$pdf = $bee->pdf([
44+
'html' => $html, // HTML generated using the HTML API (required)
45+
'page_size' => 'Letter', // (optional)
46+
'page_orientation' => 'Landscape', // (optional)
47+
]);
48+
49+
// then, you can call these methods:
50+
$pdf->getUrl();
51+
$pdf->getFilename();
52+
$pdf->getPageSize();
53+
$pdf->getPageOrientation();
54+
$pdf->getContentType();
55+
```
56+
57+
### Using Message Services API: Image
58+
59+
This API allows you to post the HTML content and get an URL with a PDF created from the HTML. Reference: https://docs.beefree.io/message-services-api-reference/#image
60+
61+
```php
62+
$image = $bee->image([
63+
'html' => $html, // HTML generated using the HTML API (required)
64+
'width' => 500, // (required)
65+
'height' => 400, // (optional)
66+
'file_type' => 'jpg',
67+
]);
68+
69+
// then, you can save the image content somewhere, some examples on how you may do this:
70+
file_put_contents('/path/to/storage/image.jpg', $image);
71+
72+
// on Laravel with Storage object:
73+
Storage::put('/path/to/storage/image.jpg', $image);
74+
```
75+
2976
### Testing
3077

3178
``` bash

src/Bee.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace KirschbaumDevelopment\Bee;
44

5+
use Exception;
56
use GuzzleHttp\Client as GuzzleClient;
7+
use KirschbaumDevelopment\Bee\Resources\Pdf;
68

79
class Bee
810
{
@@ -68,6 +70,10 @@ public function html(array $json)
6870
*/
6971
public function pdf(array $payload)
7072
{
73+
if (! isset($payload['html'])) {
74+
throw new Exception('"html" parameter is required to generate PDFs');
75+
}
76+
7177
$response = $this->httpClient->post(
7278
sprintf('%s/pdf', static::API_BASE_URL),
7379
[
@@ -78,7 +84,7 @@ public function pdf(array $payload)
7884
]
7985
);
8086

81-
return json_decode($response->getBody()->getContents(), true);
87+
return new Pdf(json_decode($response->getBody()->getContents(), true));
8288
}
8389

8490
/**

src/Resources/Pdf.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace KirschbaumDevelopment\Bee\Resources;
4+
5+
class Pdf
6+
{
7+
/**
8+
* @var string
9+
*/
10+
protected $url;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $filename;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $pageSize;
21+
22+
/**
23+
* @var string
24+
*/
25+
protected $pageOrientation;
26+
27+
/**
28+
* @var string
29+
*/
30+
protected $contentType;
31+
32+
public function __construct($pdfData)
33+
{
34+
$this->url = $pdfData['url'] ?? null;
35+
$this->filename = $pdfData['filename'] ?? null;
36+
$this->pageSize = $pdfData['page_size'] ?? null;
37+
$this->pageOrientation = $pdfData['page_orientation'] ?? null;
38+
$this->contentType = $pdfData['content_type'] ?? 'application/pdf';
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getUrl()
45+
{
46+
return $this->url;
47+
}
48+
49+
/**
50+
* @return string
51+
*/
52+
public function getFilename()
53+
{
54+
return $this->filename;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getPageSize()
61+
{
62+
return $this->pageSize;
63+
}
64+
65+
/**
66+
* @return string
67+
*/
68+
public function getPageOrientation()
69+
{
70+
return $this->pageOrientation;
71+
}
72+
73+
/**
74+
* @return string
75+
*/
76+
public function getContentType()
77+
{
78+
return $this->contentType;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
public function __toString()
85+
{
86+
return $this->getUrl();
87+
}
88+
}

tests/BeeTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
use GuzzleHttp\HandlerStack;
77
use GuzzleHttp\Psr7\Response;
88
use PHPUnit\Framework\TestCase;
9-
use GuzzleHttp\Handler\MockHandler;
109
use KirschbaumDevelopment\Bee\Bee;
10+
use GuzzleHttp\Handler\MockHandler;
11+
use KirschbaumDevelopment\Bee\Resources\Pdf;
1112

1213
/**
1314
* @coversDefaultClass \KirschbaumDevelopment\Bee\Bee
@@ -46,9 +47,14 @@ public function testPdf()
4647
$guzzleClient = new Client(['handler' => HandlerStack::create($mock)]);
4748

4849
$beeClient = new Bee($guzzleClient);
49-
$this->assertEquals($response, $beeClient->pdf([
50-
'html' => $html,
51-
]));
50+
$pdf = $beeClient->pdf(['html' => $html]);
51+
52+
$this->assertInstanceOf(Pdf::class, $pdf);
53+
$this->assertEquals($response['url'], $pdf->getUrl());
54+
$this->assertEquals($response['filename'], $pdf->getFilename());
55+
$this->assertEquals($response['page_size'], $pdf->getPageSize());
56+
$this->assertEquals($response['page_orientation'], $pdf->getPageOrientation());
57+
$this->assertEquals($response['content_type'], $pdf->getContentType());
5258
}
5359

5460
/**

0 commit comments

Comments
 (0)