Collect multiple files into single PDF - laravel

I use in my project barryvdh laravel-dompdf package.
Users might generate a PDF with some personal information, till here everything is ok:
$pdf = PDF::loadView('personalInfo', $userData);
$pdfFileName = "personal-info-{$userData['username']}.pdf";
return $pdf->download($pdfFileName);
Admin should be able to download information of users, and in case of selecting several users, instead of generating several files I would like to collect all users PDFs into a single file.
How can I do it?

You can create like this below
For example you have personalInfo blade template which is now for particular user .So loop that view and store it in array like below
$pages=[];
foreach($userDataList as $key=>$userData){
$pages[]=(string)view('personalInfo', $userData);
}
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('index', ['pages' => $pages]);
return $pdf->stream();
then you can create seperate blade template to show these views.Here i will name it as index.blade.php
<style type="text/css" media="print">
div.page
{
page-break-after: always;
page-break-inside: avoid;
}
</style>
#foreach($pages as $page)
<div class="page">
{!! html_entity_decode($page) !!}
</div>
#endforeach
Method:2
loop all user detail inside personalInfo blade template and at the end of each loop add below page break style
<p style="page-break-before: always;"></p>
Example inside personalinfo blade
#foreach($pages as $page)
personal information content
<p style="page-break-before: always;"></p>
#endforeach
Also i personally prefer laravel snappy for multiple page because it render faster than laravel dompdf.Only thing is to server configuration bit head ache if you are using windows
Ref:https://github.com/barryvdh/laravel-snappy

Related

laravel blade append to #section in #forelse

My main template blade has a #yield('section_name') in the <head> tag to append css/ js.
Now I have a forelse like this:
#forelse($collection as $item)
some text
#section('section_name')
// link to stylesheet
#endsection
#empty
// something went wrong
#endforelse
When this loops for like 5 times, only one stylesheet is added. After that, it seems like the #yield is not working anymore. Is there a way to make this work so I can keep appending items to that section?
Use #append instead of #endsection
#forelse($collection as $item)
some text
#section('section_name')
// link to stylesheet
#append
#empty
// something went wrong
#endforelse

How to remove link when used to create PDF in laravel dompdf?

I just make a simple html page and write content to it. I created a link when I click on that link the content of the page will converted into PDF file, I have implemented this using laravel dompdf -> https://github.com/barryvdh/laravel-dompdf . Now, My problem is that all things doing good, but the PDF that is generated after click will shows that link in the PDF file. How to remove that button from PDF file.
My code :
My controller : ItemController.php
class ItemController extends Controller
{
public function pdfview(Request $request)
{
if($request->has('download')){
$pdf = \PDF::loadView('pdfview');
return $pdf->download('pdfview.pdf');
//return $pdf->stream();
}
return view('pdfview');
}
}
My route: web.php
Route::get('/pdfview',array('as'=>'pdfview','uses'=>'ItemController#pdfview'));
My view : pdfview.blade.php
<body>
<div class="container">
<div class="rows"><br/>
Click to PDF
<h2 align="Center"> What is PDF ? </h2><br/><br/>
<h4> Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system.</h4>
</div>
</div>
</body>
Help me, If you understand my problem. Thanks
Pass a variable to the view, i.e:
$pdf = \PDF::loadView('pdfview', array('pdf' => true));
...
return view('pdfview', array('pdf' => false));
and check the variable in the template:
#if(! $pdf )
Click to PDF
#endif

Parameter in Route::get caused style to disappear?

I'm still new to Laravel. Anyway, I'm making a small system of viewing and creating articles to be shown in the main site. I wanted to create a page that displays an article from the database, using a parameter from the URL, like this:
http://localhost:8000/read/1
The above, for example, will display the article with 'id' value of 1 in the database.
So it works just fine, but problem is, after I got it to work (which took me some time since I'm a newbie), the whole style just disappears from the page. I tried to rewrite everything but it still didn't work. New pages that I create include the style just fine.
This is my route line:
Route::get('read/{id}', array('as' => 'read', 'uses' => 'NewsController#readArticle'));
NewsController readArticle function:
public function readArticle($id) {
$article = NewsMessage::where('id', $id) -> first();
return View::make('news.read', array('article' => $article));
}
And the file read.blade.php (located in views/news/read.blade.php)
#extends('layouts.main')
#section('content')
{{ $article -> title }}
#stop
So the whole PHP code works fine and I manage to get the title. But for some reason, the whole style disappears and this is what I see:
http://puu.sh/ctO6J/db30fbe102.png
So any idea, what have I dont wrong that caused this? The other pages work just fine with the style included.
Thank you!
The issue is that the path to the image is incorrectly specified.
You can either use the built in asset methods or something like {{ URL::to('/')/imagepathhere/filename.jpg }}
Assuming you have your styles links wrote as:
#extends('layouts.main')
#section('styles')
<link href="css/custom.css" rel="stylesheet" />
<link href="css/styles.css" rel="stylesheet" />
#endsection
#section('content')
{{ $article -> title }}
#endsection
Correct Syntax:
#extends('layouts.main')
#section('styles')
<link href="{{ asset('css/custom.css') }}" rel="stylesheet" />
<link href="{{ asset('css/styles.css') }}" rel="stylesheet" />
#endsection
#section('content')
{{ $article -> title }}
#endsection
And make sure your css folder is located at public folder when calling with asset() helper.
This issues of disappearing the whole page styles is causing by accessing the image or css file or any other resources, and the solution is to use asset() helper function.
NOTE: YOUR ROUTES AND CONTROLLER HAS NO ISSUE, YOU DON'T NEED TO TOUCH EITHER.

How to grab content from section within blade template

I have written a training application with each page/slide of the training workbook as a seperate blade template file named as "page1.blade.php", "page2.blade.php" and so on. Each of these files has content of the kind:
#extends('en/frontend/layouts/training_modulename')
{{-- Page title --}}
#section('title')
Page Title
#parent
#stop
{{-- Page content --}}
#section('pageContent')
<div class="pageContentContainer">
<h2>Page Title</h2>
...
</div>
#stop
This works really well when being viewed page by page within the browser. However I also wish to automatically compile all pages into a PDF document. This is being done via dompdf which works amazingly well when I pass each pages html to it manually. However I wish to condense the #section('pageContent') section of each page into one large section which extends a different layout for passing to dompdf.
Given the above context my question is this:
Is there a method in Laravel's blade parser which would allow me to pass it a blade file and just get the rendered html from a particular section? The below pseudo-code demonstrates what I would like to be able to do.
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage = Blade::render($page);
$title = $renderedPage->title;
$pageContent = $renderedPage->pageContent;
}
Instead of doing the normal return of view
return View::make('page');
You can instead store the view in a string
$view = View::make('page');
So then you can do your code something like this (not tested - but you get the idea):
$pages = array(...); // content of the directory
foreach ($pages as $page)
{
$renderedPage[] = view::make($page);
}

Load a view inside another view

I've been using django for some time and I decided to start a new project but this time in Codeigniter, I used to extend the template file in my views and put content inside the {% block content %} block but it seens to be different in CodeIgniter.
In CodeIgniter I have something like:
<?php
$this->load->view('header');
$this->load->view('form_add_customer');
$this->load->view('footer');
?>
But is there a way to have an unique file with header, content and footer like this?
<html>
<head><title>Test</title></head>
<body>
<div id="header">Welcome</div>
<div id="content">
</div>
<div id="footer"> 2013 - Tectcom Telecom</div>
</body>
</html>
And put a view file with a form inside the content div?
Update your html (layout) file like this:
<div id="content"><?php $this->load->view($content) ?></div>
In your controller, call the view like this:
$view_data = array();
$view_data['content'] = 'form_add_customer';
$this->load->view('path/to/layout', $view_data);
I've used the "Most Simple Template Library for CodeIgniter" in the past with success for smaller projects. I believe it'll provide with the functionality that you require allowing you to have placeholders in a 'template' which you can update in your controller logic.

Resources