I am new to Laravel. I want to convert my HTML page into PDF therefore I am using:
https://github.com/barryvdh/laravel-snappy
Now from my controller I am using the following code:
$data = $this->getdata();
$html = view('myview', [ 'data' => $data] )->render();
$pdf = \App::make('snappy.pdf.wrapper');
$pdf->loadHtml($html);
return $pdf->inline();
When I dump or return the $html variable, it's giving the desire view on the browser but when I convert it its not giving that output. My HTML page is also using Bootstrap but I think after rendering its just a normal HTML string.
Can anyone tell what I am missing here?
I have also tried $pdf->loadView() but it's not working.
Take a look at the loadView function.
Try this:
$data = $this->getdata();
$pdf = \App::make('snappy.pdf.wrapper');
$pdf->loadView('my.view', $data);
return $pdf->inline();
And print what's returned by the laodView to see if it get your view correctly.
Related
I'm brand new to CodeIgniter, so apologies if I'm missing something obvious here.
I'm comfortable with sending data from a controller to a view file using return view('default/blog/index', $data);. My issue is accessing the same data in a layout file which is extended by the view file using <?= $this->extend('layouts/default'); ?>.
For example, if I insert <?= $data['content'] ?> in my view file, it displays as expected. If I insert the same code in the layout file that is extended by my view file, I get the "Trying to access array offset on value of type null" exception.
What am I missing that will allow me to access my data from within the layout file?
Thanks in advance.
Update:
So in my BlogController I've got
class BlogController extends BaseController
{
public function index()
{
$model = new Blog();
$data = $model->getBlog();
return view('default/blog/index', ['data' => $data]);
}
public function item($slug = null){
$model = new Blog();
$data = $model->getBlog($slug);
return view('default/blog/item', ['data' => $data]);
}
}
And then in my item.php and index.php files, I have
<?= $this->extend('layouts/default', ['data' => $data]); ?>
My Blog Model's getBlog() method:
public function getBlog($slug = false)
{
if ($slug === false) {
return $this->orderBy('bs_created_dt', 'desc')->findAll();
}
return $this->where(['bs_slug' => $slug])->first();
}
When I use the debug toolbar to inspect the data, it is showing as expected, and I can display it in the view files, but not in the layout file.
In Codeigniter, you need to pass data also in an extended file called layouts.
Because you want to access data inside the extended file and for that, you just need to pass data to that file.
So replace the line of code of extended view with this :
$this->extend('layouts/default', ['data' => $data]);
Figured this out - absolute rookie mistake.
I'm working off of a pre-existing template, and the previous developer had overwritten the $data variable in layout file before where I was trying to use it.
I'm off to stand in the corner for a while.
hello i'm newbie and need your help buddy... please kindly help for a minute
So, i want to generate PDF but i dont know what should i do, please look at this
public function pdfview($id){
$halaman="tindaklayanan";
$keluhan = keluhan::findOrFail($id);
$tindak = DB::table('tindakans')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('keluhans.id, perbaikan_sementara, ttd_tanggung1, ttd_tanggung2, revisi_dokumen,
target_verifikasi'))->get();
//dd($tindak);
$analisa = DB::table('analisas')
->join('tindakans','tindakans.id','=','analisas.id_tindakan')
->join('keluhans','keluhans.id','=','tindakans.id_keluhan')
->select(DB::raw('id_tindakan, analisa, tindakan, pic, tanggal_pelaksanaan'))->get();
return view('Laporan.pdfview',compact('keluhan','tindak','analisa','halaman'));
//$pdf = PDF::loadHTML('<h1>Test</h1>');
//return $pdf->stream();
}
I've an HTML view and it's finished but how to generate as PDF ? see the command line its work, but i want to load my view that compact('keluhan','tindak','analisa','halaman')
$pdf = \PDF::loadView('Laporan.pdfview',compact('keluhan','tindak','analisa','halaman'));
return $pdf->stream();
Its work now thank you.
I'm using Code Igniter framework, and tcpdf third-parties
I have a global variable in controller :
public $pdf_content = '';
here is some of my function to book :
$data['success'] = $this->hotel_model->set_hotel(); //get the data from database
//view reservation detail
$this->load->view('hotel/header');
$this->load->view('hotel/success', $data );
$this->load->view('hotel/footer');
//assign the HTML page into global variable
$this->pdf_content = $this->load->view('hotel/success', $data , TRUE);
In my view file I'm using a button to download the HTML as pdf file, here is the download function :
public function download(){
$this->load->library("Pdf");
$this->load->helper('pdf_helper');
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
// Add a page
$pdf->AddPage();
$pdf->writeHTMLCell(0, 0, '', '', $this->pdf_content , 0, 1, 0, true, '', true);
$pdf->Output('invoice.pdf', 'I');
}
Both function located inside the same controller.
I think there is nothing wrong in my code based on any tutorial I ever read.
But it downloaded some blank page instead, and when I try to assign string value into $pdf_content, it works fine, The string value is written into the downloaded PDF file.
Anybody know what I missed there?
Or there is something wrong in my code?
Any help is appreciated.
There are multiple ways. The easiest would be to save the page as pdf manually (ctrl+p) and then make it available as download. OR, you can show the page and run the following script in the bottom of the body.
<script>
var yourName = "someName';
document.title = yourName;
window.print();
</script>
This fires the ctrl+p command and allows users to either print or save the page.
i have my template just in string "<html>..</html>"
that were responded by an request;
now i want generate an pdf using DOMpdf
$pdf = new PdfModel();
$pdf->setOption('filename', 'monthly-report'); // Triggers PDF download, automatically appends ".pdf"
$pdf->setOption('paperSize', 'a4'); // Defaults to "8x11"
$pdf->setOption('paperOrientation', 'landscape'); // Defaults to "portrait"
// To set view variables
$pdf->setVariables(array(
'message' => 'Hello'
));
$pdf->setTemplate("path/to/something.html");
return $pdf;
instead of $pdf->setTemplate("path/to/something.html") how i can give to $pdf(ViewModel) my string template.
Looks like you need implement your own StringRenderer implementing Zend/View/Renderer/RendererInterface and also extend DOMPDFModule/Mvc/Service/ViewPdfRendererFactory to use your renderer.
This is because, PdfRenderer is hardcoded to use the default ViewManager Renderer and there is no StringRenderer in Zend.
Instead of returning a view model you could return a response:
$string = "<h1>hello</h1>";
$response = $this->getResponse();
$response->setContent($string)->setStatusCode(200);
$response->getHeaders()->addHeaderLine('Content-Type', 'text/html');
return $response;
You could use different template engine like mustache or twig ,compile php and then pass to loadHtml method from DOMpdf.
Just a quick question. Anyone know if its possible to load multiple views from codeigniter to mpdf controller and convert it to pdf?
my controller :
`
<?php
class C_Pdf extends CI_Controller {
private $params = array();
function __construct() {
parent::__construct();
$this->params['set_tag'] = array();
$this->params['set_css'] = array();
$this->params['set_js'] = array();
$this->params['title_auto'] = true;
$this->params['title'] = '';
}
public function index(){
//this data will be passed on to the view
$data['the_content']='mPDF and CodeIgniter are cool!';
$data['other']="siema heniu cos przeszlo";
//load the view, pass the variable and do not show it but "save" the output into $html variable
$this->load->view('common/default_header',$this->params);
$html=$this->load->view('reservation/complete', $data,true);
$this->load->view('common/default_footer');
//this the the PDF filename that user will get to download
//load mPDF library
$this->load->library('m_pdf');
//actually, you can pass mPDF parameter on this load() function
$pdf = $this->m_pdf->load();
//generate the PDF!
$pdf->WriteHTML($html);
//offer it to user via browser download! (The PDF won't be saved on your server HDD)
$pdf->Output();
}
}?>
i want to add footer and header from other view.
Just prefetch the header and footer templates into variables and pass their parsed strings to the main view ,like this:
$data['header'] = $this->load->view('common/default_header',$this->params, true);
$data['footer'] = $this->load->view('common/default_footer', true);
$html = $this->load->view('reservation/complete', $data, true);
Notice the third parameter to true so that you get the view in a string instead of sending it to output (most commonly the client's browser).
Then in your main template, place the $header and $footer variables wherever you need.