Laravel EXCEL and PDF export - laravel

I am new in Laravel and I am using Laravel 4.2
I like to export some data in PDF and excel.
Is there any way in Laravel?

Use FPDF to do what u need. You must create a pdf file from scratch and fill it in the way u want.
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage(); // add page to PDF
$pdf->SetFont('Arial','B',16); // Choose a font and size
$pdf->Cell(40,10,'Hello World!'); // write anything to any line you want
$pdf->Output("your_name.pdf"); // Export the file and send in to browser
?>
And for Excel a simple way is to add PHPExcel to laravel. Add this line to your composer.json:
"require": {
"phpexcel/phpexcel": "dev-master"
}
then run a composer update. So use it like this:
$ea = new PHPExcel();
$ea->getProperties()
->setCreator('somebody')
->setTitle('PHPExcel Demo')
->setLastModifiedBy('soembody')
->setDescription('A demo to show how to use PHPExcel to manipulate an Excel file')
->setSubject('PHP Excel manipulation')
->setKeywords('excel php office phpexcel')
->setCategory('programming')
;
$ews = $ea->getSheet(0);
$ews->setTitle('Data');
$ews->setCellValue('a1', 'ID'); // Sets cell 'a1' to value 'ID
$ews->setCellValue('b1', 'Season');

Use maatwebsite to Create and import Excel, CSV and PDF files
Add this lines to your composer.json:
"require": {
"maatwebsite/excel": "~2.1.0"
}
After updating composer, add the ServiceProvider to the providers array in config/app.php
Maatwebsite\Excel\ExcelServiceProvider::class,
You can use the facade for shorter code. Add this to your aliasses:
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
To publish the config settings in Laravel 5 use:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
simple use of this package:
$users = User::select('id','name' ,'username')->get();
$Info = array();
array_push($Info, ['id','name' ,'username']);
foreach ($users as $user) {
array_push($Info, $user->toArray());
}
Excel::create('Users', function($excel) use ($Info) {
$excel->setTitle('Users');
$excel->setCreator('milad')->setCompany('Test');
$excel->setDescription('users file');
$excel->sheet('sheet1', function($sheet) use ($Info) {
$sheet->setRightToLeft(true);
$sheet->fromArray($Info, null, 'A1', false, false);
});
})->download('xls'); // or download('PDF')

Related

"next" page problem with Illuminate\Pagination\LengthAwarePaginator in the last page

I am trying to use Illuminate\Pagination\LengthAwarePaginator to paginate a Laravel Collection within a Livewire component in the following way:
public function render(): Factory|View|Application
{
$perPage = 3;
$items = $this->myCollection->forPage($this->page, $perPage);
$paginator = new Illuminate\Pagination\LengthAwarePaginator($items, $this->myCollection->count(), $perPage, $this->page);
return view('livewire.listing', [
'paginatedMyCollection' => $paginator,
]);
}
and in the listing.blade.php:
#foreach ($paginatedMyCollection as $element)
#livewire('row', ['element' => $element], key($element->id))
#endforeach
{{ $paginatedMyCollection->links() }}
It works fine(*)! for example:
except when the last page of the collection is reached. In this case, the «next page» button is erroneously constructed, resulting in the following presentation:
I have checked the Illuminate package and it seems that instead of using 'pagination.next' is taking this 'Showing.next' that does not work.
Can anyone help with this? Thanks and regards.
(*) The small buttons at the beginning and end of the page numbers could be filled with any character...
I think this is CSS issue.
Laravel Pagination is using tailwind per default.
If you are using Bootstrap, you can add Paginator::useBootstrapFive(); in app\Providers\AppServiceProvider boot() function
I am not sure how many libraries is supported, but you can always publish vendor view for pagination and edit it
php artisan vendor:publish --tag=laravel-pagination

Class not found after ajax request - October cms

For my application in october cms I'd like to be able to send a mail with the click of a button. I call the php file with an ajax request, however when the button gets clicked I get the error 'class not found' for whichever class I use, doesn't matter which. I already added the file to .htaccess to be able to run it on the button click. I included all classes at the top of the file. Also when I turn it into an artisan command and run it with php artisan now:send:mail it works without any issues. I already tried to run composer dump autoload and composer dump autoload -o. Code is down below, Any idea what I can do to make this work, or in what other way it could be done?
Thanks in advance!
Part of theme.js:
$.ajax({
url : '/plugins/test/notifications/ondemand/OnDemand.php'
}).done(function(data) {
console.log(data);
});
OnDemand.php:
<?php
namespace Test\Notifications\OnDemand;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Test\Poles\Components\Metrics;
use October\Rain\Argon\Argon;
class OnDemand
{
public function send()
{
$date = Argon::now(config('app.timezone'))->format('Y-m-d');
// get some data
$array = ['date' => $date, 'score' => $score, 'CO2' => $CO2, 'scorecar' => $scorecar, 'scorebike' => $scorebike];
$email = "test#test.nl";
Mail::sendTo($email, 'daily.mail', $array);
}
}
$mail = new OnDemand;
$mail->send();
I'm not sure whether you want to do this as part of a custom October plugin you've developed or simply inside a regular October template. However, the absolutely simplest way of having an ajax button to send an email would be as follows:
1) Create a new mail template in the October backend in Settings | Mail Templates
2) In the "CMS" section of October, create a new blank Page
3) For the "Markup" section of the new page, the button HTML:
<button data-request="onPressButton">Send</button>
4) For the "Code" section of the new page, the following PHP:
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Test\Poles\Components\Metrics;
use October\Rain\Argon\Argon;
function onPressButton()
{
$date = Argon::now(config('app.timezone'))->format('Y-m-d');
// get some data
$array = ['date' => $date, 'score' => $score, 'CO2' => $CO2, 'scorecar' => $scorecar, 'scorebike' => $scorebike];
$email = "test#test.nl";
Mail::sendTo($email,'daily.mail', $array);
}
That's it. As long as you include JQuery and {% framework extras %} in your October page layout, the above will work.
The principal is the same if you're adding this within a custom plugin that you've developed, but the HTML and PHP would obviously go into their individual files within a component if you did it that way.

Adding 'Read More' link in Laravel 4

I have trying to excerpt some text and add Read More link. So far I was able to cut the text and add Read More but now I can to make few lines between text and read more link.
Here is how I truncated the text
{{ str_limit($product['description_small'], $limit = 250, $end = ' Read More') }}
How can I put this $end = ' Read More' few lines under the text and to make it link?
Laravel 4 + blade template
The best thing you can do is create your own function to create the excerpt and add the Read More link.
First of all in your composer.json file, in the autolaod section add a files array:
{
"autoload": {
"files": [
"app/helpers.php"
]
}
}
Now in your app/ folder create the helpers.php file.
Then add in app\start\global.php: ( NB: this is required only for Laravel 4 )
// at the bottom of the file
require app_path().'/helpers.php';
Then:
composer dump-autoload
In this way Laravel will load the file for you.
Now in this file you can create your onw function to manage the excerpt:
<?php
if( ! function_exists('createExcerptAndLink')){
function createExcerptAndLink($text, $limit, $url, $readMoreText = 'Read More') {
$end = "<br><br>$readMoreText";
return str_limit($text, $limit, $end);
}
}
Now in every view you can use your new helper:
{{ createExcerptAndLink($product['description_small'], 250, "http://www.google.it", 'Read more here.') }}

Laravel 5.1 - barryvdh/laravel-dompdf PDF Generation Error

In my composer:
"barryvdh/laravel-dompdf": "0.6.*",
Here is My config/app Setup:
Providers Array: 'Barryvdh\DomPDF\ServiceProvider',
Aliases Array: 'PDF' => 'Barryvdh\DomPDF\Facade',
Here is My route Setup:
$pdf = App::make('dompdf.wrapper');
Route::get('/dompdf', function() {
$html='<p>Foo Bar</p>';
PDF::loadHTML($html)->setPaper('a4')
->setOrientation('landscape')
->setWarnings(false)
->save('myfile.pdf');
});
While I hit /dompdf url. I got this Error.
My another Question is if I want to use DOM PDF library in My Controller Method. What setup or Step I Need To Follow??
You can run the below command and try
composer require barryvdh/laravel-dompdf

How to convert full excel to PDF with maatwebsite?

I make a report with maatwebsite and download it to xls and works fine, but now I need to export it to PDF, so when it exports to PDF only returns the first page, even if has multiple sheets.
Excel::create('name', function ($excel) {
foreach ($categories as $value) {
$excel->sheet($value['name'], function($sheet) {
...
});
}
})->download('pdf');
How can I see all pages?
I'm using laravel 4.2, maatwebsite/excel 1.3.0 and mpdf 6.0.0
Fetch $data and put $filename = "new.pdf"
$filename = "new.pdf";
Excel::download( new ExportOrder( $data ), $filename);
If you read the documentation you should use ->export('pdf') instead of ->download('pdf').
Please try it and return with the result.
Text from the documentation:
To export files to pdf, you will have to include "dompdf/dompdf": "~0.6.1", "mpdf/mpdf": "~5.7.3" or "tecnick.com/tcpdf": "~6.0.0" in your composer.json and change the export.pdf.driver config setting accordingly.
I generate the same question on maatwebsite github. I did this and it works.
https://github.com/Maatwebsite/Laravel-Excel/issues/386

Resources