From 78bafbbf0971e9eef5814707452548b7203992f3 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 1 Jun 2016 12:56:15 +0300 Subject: [PATCH] Support downloading PDF through the API --- app/Http/Controllers/InvoiceApiController.php | 13 +++++++++++++ app/Http/routes.php | 1 + 2 files changed, 14 insertions(+) diff --git a/app/Http/Controllers/InvoiceApiController.php b/app/Http/Controllers/InvoiceApiController.php index 520b5b09acac..c508b790d4c4 100644 --- a/app/Http/Controllers/InvoiceApiController.php +++ b/app/Http/Controllers/InvoiceApiController.php @@ -358,4 +358,17 @@ class InvoiceApiController extends BaseAPIController return $this->itemResponse($invoice); } + public function download(InvoiceRequest $request) + { + $invoice = $request->entity(); + $pdfString = $invoice->getPDFString(); + + header('Content-Type: application/pdf'); + header('Content-Length: ' . strlen($pdfString)); + header('Content-disposition: attachment; filename="' . $invoice->getFileName() . '"'); + header('Cache-Control: public, must-revalidate, max-age=0'); + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); + + return $pdfString; + } } diff --git a/app/Http/routes.php b/app/Http/routes.php index e96aebb7ea54..f4e65d0af180 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -270,6 +270,7 @@ Route::group(['middleware' => 'api', 'prefix' => 'api/v1'], function() //Route::get('quotes', 'QuoteApiController@index'); //Route::resource('quotes', 'QuoteApiController'); Route::get('invoices', 'InvoiceApiController@index'); + Route::get('download/{invoice_id}', 'InvoiceApiController@download'); Route::resource('invoices', 'InvoiceApiController'); Route::get('payments', 'PaymentApiController@index'); Route::resource('payments', 'PaymentApiController');