Laravel: DOMPDF error when trying to get PDF - php

I've been trying to get the PDF of a view using DOMPDF with a table full of ingredients populated from a database table.
I created a function in my Controller:
public function createPDF() {
// retreive all records from db
$data = Ingredients::all();
// share data to view
view()->share('ingredients', $data);
$pdf = PDF::loadView('printableingredientslist', $data);
// download PDF file with download method
return $pdf->download('printableingredientslist.pdf');
}
And the route in web.php is defined as:
Route::get('/printpreviewingredients/pdf', [MealPlanDisplayController::class, 'createPDF']);
However, I keep getting this error:
fopen(D:\mealplan\storage\fonts//nunito_normal_46b9b3b48cc3ddbae60da82f1c1c5d29.ufm):
failed to open stream: No such file or directory
It must be something to do with the 'printableingredientslist' as when I replaced it with a plain view page with simple text that worked. I am struggling to see how there's a path issue however, as printableingredientslist.blade.php is simply under the views folder. What can the issue be? I am using tailwind.css, could that be a problem?

setOptions method to use default font.For example like below
public function pdf()
{
$pdf = \PDF::loadView('contact')->setOptions(['defaultFont' => 'sans-serif']);
return $pdf->download('invoice.pdf');
}
or
Create the directory fonts in storage directory
Ref: https://github.com/barryvdh/laravel-dompdf/issues/724
Ref:https://github.com/barryvdh/laravel-dompdf/issues/176
Ref:https://github.com/barryvdh/laravel-dompdf/issues/269

Related

Laravel: Generate PDF of Blade View and Store it in specified folder

I have a Laravel Blade View, which is fetching data from Database. It is working perfectly.
Here is my Controller:
$model = new outbound_detail;
$data = $model->where('reference', $reference)->get();
$sku_count = $data->count();
$model2 = new outbounds;
$data_model2 = $model2->where('reference',$reference)->first();
$date = $data_model2->created_at;
return view('ims.release.release_invoice',compact('data'))->with('sku_count',$sku_count)->with('reference',$reference)->with('date',$date)->with('warehouse',$warehouse);
Now instead of returning this view I want to save this view as PDF in my folder: public/uploads
I have already installed the composer for DOMPDF.
How can I convert this page to pdf with passing all variables to view file without returning the view.
i use this libraries, it's depends on your needs:
https://github.com/dompdf/dompdf
https://github.com/barryvdh/laravel-dompdf
https://github.com/niklasravnsborg/laravel-pdf
https://packagist.org/packages/carlos-meneses/laravel-mpdf

How to print some part of blade file in Laravel with domPDF

I have to create a pdf file with Laravel. So I install in my composer domPDF. But I have to generate the pdf with only some parts of my blade file. I've already tried to create a new blade file and include it in the old blade file, but this return to me errors as the new blade file can't get the data I passed with the controller, so what can I do?
This is the function I wrote
public function printPDF(){
$pdf = PDF::loadView('pages.newblade');
return $pdf->download('document.pdf');
}
Thanks
I am suggesting an option to refactor your code like this
public function printPDF(){
$printthis = true;
$pdf = PDF::loadView('pages.newblade', compact('printthis'));
return $pdf->download('document.pdf');
}
on pages.newblade check printthis variable if present means show the printable content else show all like this
#if(isset($printthis))
//printable code goes here
#else
//General code goes here
#endif

Template in codeigniter

I am trying to develop a website using a templating method I have created different files and would like to run a query and that query would be available on all templates is it possible to do that? Like I would like to give you an example what I am doing here in the controller I have created a variable like this
$data['navigation'] = 'templates/main_menu';
$this->load->view('main');
Okay so now here what I did in the view of main is I included header and footer there and called the navigation file dynamically like this
$this->load->view($navigation);
Okay so now What I am trying to do is want to get an icons stored in database and other settings as well so I placed a query in the header file
<?php $settings = $this->db->get_where('settings', array('id' => 2));\\ This query is just placed above the doctype in the header file and I would like to be called every where ?>
templates/main_menu.php
$settings->row()->header_bg_color
But I am getting an error as Message: Undefined variable: settings and if i place this settings query within the main_menu.php file it works so like what should be the way i mean it does not make sense to call the query again and again if I would be requiring what will be the best way to do and what is tthe use of model would it be used for the queries if it is then if I place this settings query within the model how would I access it is it possible please advise me
Thank you
hi below is code of controller. i have created the common_library.php file in which i have written all common function that I want to use all views.
class Maintenance extends CI_Controller {
function Maintenance() {
parent::__construct();
// file for all common function
include('application/controllers/common/common_library.php');
$this->load->model('maintenance_model');
$this->load->library('session');
$this->load->library('form_validation');
}
function index(){
/*
* # desc : default function that loads the maintenance_view.
*
*
*/
$data['common_query'] = commonQuery() // define this function in common_library file
$data['model_data'] = $this->maintenance_model->projectdetail();
$data['formtitle'] = 'Maintenance List | BMS';
$this->load->view('maintenance_view',$data);
}
}

CI load properties by default

I use Codeigniter Framework, and I set my program to load header and footer by default when i load the view method. In the header file I have properties like: site name, description etc.. Those proprieties fetch from the DB. Now the problem is that I need to set them every time I call the view method.
How can I set them by default correctly?
As someone said above, you should extend the Controller. I do something very simular to this, and this is my code, which is found on "MY_Controller.php" within the ./application/core directory.
public function show_view($view, $data = array())
{
// Database connection here
// add anything to the $data array
$this->load->view('header', $data);
$this->load->view($view, $data);
$this->load->view('footer', $data);
}
Now, instead of loading a view from the controller like this;
$this->load->view('view', $data);
I do this;
$this->show_view('view', $data);
FYI. I call the function "show_view" to avoid name conflicts.

yii set log path dynamically

friends i am using yii log to genrate log file for each user on the base of its id for its actions but when i am trying to set log file path dynamically then its not working please help me to find the way that how i can set dynamically log file path for logged in user.
$logger = new CFileLogRoute();
$logger->setLogPath("/app/protected/runtime/user-log/");
$logger->setLogFile("kumar-121.log");
$message='langusge changed from ';
Yii::log($message, 'CsvError', 'system.*');
buts it not working at all. please help me in this
Thanks friends but i got the answer what is did i firstly created my custom class and extand CFileLogRoute
then i just overwrite some parent file functions
class MyFileLogRoute extends CFileLogRoute{
public function formatLogMessage($message,$level,$category,$time)
{
return #date('Y/m/d H:i:s',$time)." - $message\n";
}
public function getLogFile()
{
return 'user-'.(int)Yii::app()->user->id.'.log';
}
public function setLogFile($value)
{
parent::setLogFile($value . (int)Yii::app()->user->id);
}
and this works perfect for me.
my first method i have created to remove level category from log file so it just simple log file
anyhow thanks

Categories