Skip to content

Commit 0e0728e

Browse files
committed
implement GotenbergConverter
1 parent 15248fc commit 0e0728e

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

src/Converters/GotenbergConverter.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Petecoop\ODT\Converters;
44

55
use Gotenberg\Gotenberg;
6+
use Gotenberg\Stream as GotenbergStream;
7+
use GuzzleHttp\Psr7\Stream;
68
use Petecoop\ODT\Files\OdtFile;
79
use Petecoop\ODT\Files\PdfFile;
810

@@ -25,10 +27,24 @@ public function url(): string
2527

2628
public function convert(OdtFile $file): PdfFile
2729
{
28-
//
29-
30-
Gotenberg::libreOffice($this->url);
31-
32-
return new PdfFile($pdfPath, $tmp);
30+
$handle = fopen('php://temp', 'r+b');
31+
try {
32+
$file->toStream($handle);
33+
$stream = new Stream($handle);
34+
$request = Gotenberg::libreOffice($this->url)->convert(new GotenbergStream($file->getName(), $stream));
35+
$response = Gotenberg::send($request);
36+
} finally {
37+
fclose($handle);
38+
}
39+
40+
$stream = fopen('php://temp', 'r+b');
41+
$response->getBody()->rewind();
42+
while (!$response->getBody()->eof()) {
43+
fwrite($stream, $response->getBody()->read(1024));
44+
}
45+
46+
$pdf = new PdfFile();
47+
$pdf->name(basename($file->getName(), '.odt'));
48+
return $pdf->fromStream($stream);
3349
}
3450
}

src/Facades/ODT.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
use Illuminate\Support\Facades\Facade;
66

7-
87
/**
98
* @method static \Petecoop\ODT\Files\OdtFile open(string $path)
109
* @method static \Petecoop\ODT\ODT officeBinary(string $binaryPath)
10+
* @method static \Petecoop\ODT\ODT gotenberg(string $url)
1111
* @method static \Petecoop\ODT\Compiler compiler()
12-
* @method static \Petecoop\ODT\OfficeConverter converter()
12+
* @method static \Petecoop\ODT\Converters`Coverter converter()
1313
*/
1414
class ODT extends Facade
1515
{

0 commit comments

Comments
 (0)