diff --git a/README.md b/README.md index 7c021b8..889570c 100755 --- a/README.md +++ b/README.md @@ -63,15 +63,32 @@ class CustomFormatter implements IFormatter { ```php $invoice = new WebChemistry\Invoice\Invoice($company); -header('Content-Type: application/pdf'); +header('Content-Type: application/pdf; charset=utf-8'); echo $invoice->create($customer, $order); ``` +shorter +```php +$invoice = new WebChemistry\Invoice\Invoice($company); + +$invoice->send($customer, $order); +``` + +nette framework way +```php +$invoice = new WebChemistry\Invoice\Invoice($company); + +$this->sendResponse($invoice->createResponse($customer, $order)); +``` + ## Generating preview ```php -header('Content-Type: application/pdf'); -echo $invoice->generatePreview(); +$invoice->send(WebChemistry\Invoice\Preview\PreviewFactory::createCustomer(), WebChemistry\Invoice\Preview\PreviewFactory::createOrder()); +``` + +```php + ``` ## Nette DI diff --git a/composer.json b/composer.json index ee46700..fb9920b 100755 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ }, "require-dev": { "nette/di": "^3.0", + "nette/application": "^3.0", "codeception/codeception": "^2.4" }, "autoload": { diff --git a/src/Invoice.php b/src/Invoice.php index 4225380..23b41dc 100755 --- a/src/Invoice.php +++ b/src/Invoice.php @@ -4,6 +4,7 @@ namespace WebChemistry\Invoice; +use Nette\Application\IResponse; use Nette\SmartObject; use WebChemistry\Invoice\Calculators\FloatCalculator; use WebChemistry\Invoice\Calculators\ICalculator; @@ -14,6 +15,7 @@ use WebChemistry\Invoice\Data\PaymentInformation; use WebChemistry\Invoice\Renderers\IRenderer; use WebChemistry\Invoice\Renderers\PDFRenderer; +use WebChemistry\Invoice\Responses\PdfResponse; use WebChemistry\Invoice\Templates\DefaultTemplate; use WebChemistry\Invoice\Templates\ITemplate; @@ -40,24 +42,18 @@ public function __construct(Company $company, ?ITemplate $template = null, ?IRen $this->calculator = $calculator ?: new FloatCalculator(); } - public function generatePreview(): string { - $tax = $this->company->hasTax() ? 0.21 : null; - $customer = new Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '08304431', 'CZ08304431'); - $account = new Account('2353462013/0800', 'CZ4808000000002353462013', 'GIGACZPX'); - $paymentInfo = new PaymentInformation('$', '0123456789', '1234', $tax); + public function create(Customer $customer, Order $order): string { + return $this->template->build($this->calculator, $this->renderer, $customer, $order, $this->company); + } - $order = new Order(date('Y') . '0001', new \DateTime('+ 7 days'), $account, $paymentInfo); - $order->addItem('Logitech G700s Rechargeable Gaming Mouse', 1790, 4); - $order->addItem('ASUS Z380KL 8" - 16GB, LTE, bílá', 6490, 1); - $order->addItem('Philips 48PFS6909 - 121cm', 13990, 1); - $order->addItem('HP Deskjet 3545 Advantage', 1799, 1); - $order->addItem('LG 105UC9V - 266cm', 11599, 2); + public function send(Customer $customer, Order $order): void { + header('Content-type: application/pdf'); - return $this->create($customer, $order); + echo $this->create($customer, $order); } - public function create(Customer $customer, Order $order): string { - return $this->template->build($this->calculator, $this->renderer, $customer, $order, $this->company); + public function createResponse(Customer $customer, Order $order): IResponse { + return new PdfResponse($this->create($customer, $order)); } } diff --git a/src/Preview/PreviewFactory.php b/src/Preview/PreviewFactory.php new file mode 100644 index 0000000..073bfdd --- /dev/null +++ b/src/Preview/PreviewFactory.php @@ -0,0 +1,31 @@ +addItem('Logitech G700s Rechargeable Gaming Mouse', 1790, 4); + $order->addItem('ASUS Z380KL 8" - 16GB, LTE, bílá', 6490, 1); + $order->addItem('Philips 48PFS6909 - 121cm', 13990, 1); + $order->addItem('HP Deskjet 3545 Advantage', 1799, 1); + $order->addItem('LG 105UC9V - 266cm', 11599, 2); + + return $order; + } + +} diff --git a/src/Responses/PdfResponse.php b/src/Responses/PdfResponse.php new file mode 100644 index 0000000..015aa04 --- /dev/null +++ b/src/Responses/PdfResponse.php @@ -0,0 +1,23 @@ +content = $content; + } + + public function send(IRequest $httpRequest, NetteIResponse $httpResponse): void { + $httpResponse->setContentType('application/pdf', 'utf-8'); + echo $this->content; + } + +}