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
Related
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
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);
}
I have a project which is using laravel4 and its blade view engine. Occasionally I have had the need to call controller methods via a view file to output dynamic data; incidentally this time its a call to a method that generates javascript code for the page. Regardless of whether this is the best way to go about things is a moot point atm as I am simply upgrading from L3 to L4.
My View is similar to:
#extends('en/frontend/layouts/default')
{{-- Page title --}}
#section('title')
Page Title
#parent
#stop
{{-- Page content --}}
#section('pageContent')
...
#stop
{{-- Scripts --}}
#section('scripts')
<?php echo CaseStudy::script(); ?>
#stop
I have set up CaseStudy to load via the laravel facades and the class at current is simply:
class CaseStudy
{
public function display()
{
}
/**
* Returns the javascript needed to produce the case study
* interactivity
*
* #return \Illuminate\View\View|void
*/
public function script()
{
$language = \Config::get('app.locale');
$scriptElementView = $language . '.frontend.elements.casestudy_script';
if ( ! \View::exists($scriptElementView))
{
$scriptElementView = "Training::" . $scriptElementView;
}
return \View::make($scriptElementView, array());
}
}
It would appear that echoing the response of CaseStudy::script is what is causing the blank body; however with no further error message I do not know what is going on. I assume that this is because my static CaseStudy's instance of View is conflicting with the instance being used by the blade engine. How would I go about having CaseStudy::script() returning a string form of the rendered view?
Thank you.
In your view
{{-- Scripts --}}
#section('scripts')
{{ CaseStudy::script() }}
#stop
In your library
class CaseStudy
{
public function script()
{
$string = "your script here";
return $string;
}
}
Note - CaseStudy should really be a "library" or "helper" or something. Not a controller - that does not really conform to MVC approach.
I have a CakePHP script that prints a report with large blocks of conditional content like so:
<?php if(in_array('Medical', $filter) || in_array('All', $filter)){ ?>
<div class="title_col_cell" align="left">Medical</div>
<div class="title_col_cell1" align="right">Visits</div>
<div class="title_col_cell1" align="right">Unduplicated Clients</div>
<div class="title_col_cell1" align="right">Prescription Assistance</div>
<div class="title_col_cell1" align="right">Vaccinations</div>
<?php } ?>
When I create a print view of this report, dompdf seems to be rendering the conditional content as blank space. Is there a way to get dompdf not to render that stuff? There are too many possible combinations for me to make a separate view for each. How can I code this differently?
Thanks!
Anything that's displayed on the screen if you view the document will get to dompdf generated doc. If you can't see it in html document - then it will be hidden. View your document just before you pass it to dompdf in a standard browser to see whats going in there.
DOMPDF pdf will create pdf only for the code which has been sent to it. It's better you put the whole code in a string and then display it on screen to check whether all required html is there or not, before sending to dompdf object. For example :
$str="<div class="title_col_cell" align="left">Medical</div>
<div class="title_col_cell1" align="right">Visits</div>
<div class="title_col_cell1" align="right">Unduplicated Clients</div>
<div class="title_col_cell1" align="right">Prescription Assistance</div>
<div class="title_col_cell1" align="right">Vaccinations</div>";
$dompdf = new DOMPDF();
$dompdf->load_html($str);
$dompdf->set_paper('letter', 'landscape');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
i'm trying to learn codeigniter (following a book) but don't understand why the web page comes out empty.
my controller is
class Welcome extends Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$data['title'] = "Welcome to Claudia's Kids";
$data['navlist'] = $this->MCats->getCategoriesNav();
$data['mainf'] = $this->MProducts->getMainFeature();
$skip = $data['mainf']['id'];
$data['sidef'] = $this->MProducts->getRandomProducts(3, $skip);
$data['main'] = "home";
$this->load->vars($data);
$this->load->view('template');
}
the view is:
<--doctype declaration etc etc.. -->
</head>
<body>
<div id="wrapper">
<div id="header">
<?php $this->load->view('header');?>
</div>
<div id='nav'>
<?php $this->load->view('navigation');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('footer');?>
</div>
</div>
</body>
</html>
Now I know the model is passing back the right variables, but the page appears completely blank. I would expect at least to see an error, or the basic html structure, but the page is just empty. Moreover, the controller doesn't work even if I modify it as follows:
function index()
{
echo "hello.";
}
What am I doing wrong?
Everything was working until I made some changes to the model - but even if I delete all those new changes, the page is still blank.. i'm really confused!
thanks,
P.
I've isolated the function that gives me problems.
here it is:
function getMainFeature()
{
$data = array();
$this->db->select("id, name, shortdesc, image");
$this->db->where("featured", "true");
$this->db->where("status", "active");
$this->db->orderby("rand()");
$this->db->limit(1);
$Q = $this->db->get("products");
if ($Q->num_rows() > 0)
{
foreach($Q->result_arry() as $row)
{
$data = array(
"id" => $row['id'],
"name" => $row['name'],
"shortdesc" => $row['shortdesc'],
"image" => $row['image']
);
}
}
$Q->free_result();
return $data;
}
I'm quite convinced there must be a syntax error somewhere - but still don't understand why it doesn't show any error, even if I've set up error_reporting E_ALL in the index function..
First port of call is to run php -l on the command line against your controller and all the models you changed and then reverted.
% php -l somefile.php
It's likely that there is a parse error in one of the files, and you have Display Errors set to Off in your php.ini. You should set Display Errors on for development and off for production, in case you haven't already.
(Edit: in the example above you have missed off the closing } of the class. It might be that.)
Make sure error_reporting in index.php is set to E_ALL and post your code for the model in question.
After looking through your function I suspect it's caused by $this->db->orderby("rand()");
For active record this should be $this->db->order_by('id', 'random');
Note that orderby is deprecated, you can still use it for now but the new function name is order_by
Not sure, but it can be also caused by php's "display_errors" is set to false.
You can change it in your php.ini file.