From 4edb33c1a058d29e3b30d9b6de6f61e4ec034897 Mon Sep 17 00:00:00 2001 From: Rob Pungello Date: Mon, 18 Mar 2024 12:24:06 -0400 Subject: [PATCH] Add the ability to save PDFs to a Laravel disk --- src/PdfDocument.php | 19 +++++++++++++++++++ tests/SaveTest.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 tests/SaveTest.php diff --git a/src/PdfDocument.php b/src/PdfDocument.php index e5f2817..5378951 100644 --- a/src/PdfDocument.php +++ b/src/PdfDocument.php @@ -3,6 +3,7 @@ namespace Rpungello\LaravelLabels; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Support\Facades\Storage; use Rpungello\LaravelLabels\Enums\BarcodeType; use Rpungello\LaravelLabels\Models\Label; use Rpungello\LaravelLabels\Models\LabelBarcode; @@ -218,4 +219,22 @@ public function saveTemp($tempDir = null, string $prefix = 'labels-'): string return $path; } + + /** + * Uploads the PDF to a Laravel disk + * + * @param string $disk + * @param string $remotePath + * @param array $options + * @return bool + */ + public function saveToDisk(string $disk, string $remotePath, array $options = []): bool + { + return Storage::disk($disk) + ->writeStream( + $remotePath, + fopen($this->saveTemp(), 'r'), + $options + ); + } } diff --git a/tests/SaveTest.php b/tests/SaveTest.php new file mode 100644 index 0000000..5bc8f2d --- /dev/null +++ b/tests/SaveTest.php @@ -0,0 +1,45 @@ +create(); + $labels = []; + for ($i = 0; $i < 90; $i++) { + $labels[] = new ArrayLabel([ + 'value' => 'Label #' . $i + 1, + ]); + } + $pdf = $printer->getPdfFromArray($template, $labels); + $path = $pdf->saveTemp(); + assertFileExists($path); + assertGreaterThan(1000, filesize($path)); +}); + +it('can save files to Laravel disks', function () { + $this->instance(Storage::class, Storage::fake('test')); + + $printer = new LabelPrinter(); + $template = Label::factory()->create(); + $labels = []; + for ($i = 0; $i < 90; $i++) { + $labels[] = new ArrayLabel([ + 'value' => 'Label #' . $i + 1, + ]); + } + $pdf = $printer->getPdfFromArray($template, $labels); + assertTrue($pdf->saveToDisk('test', 'test.pdf')); + assertTrue(Storage::disk('test')->exists('test.pdf')); +});