CakePHP 3+ issue with DomPdf - php

I currently have a function in a project controller class that I am calling to export a specific project to a pdf. I am running into problems when I try to pass that single project page that I am pulling from. If I call function from my view and pass in a string of valid html from my export() function it will create a pdf correctly. I am just wondering how I can get it from that ctp template to my controller to be created as a pdf. Thanks.
In my ProjectsController.php
public function view($id)
{
$creator = $this->Auth->user();
$project = $this->Projects->get($id, [
'contain' => [
'Countries', 'Languages', 'Tags',
'ProjectsLanguages', 'Students'
]
]);
$languages = $this->__getLanguageReqs($id);
$tags = $this->__getTagReqs($id);
$projSupervisors = $this->__getSupervisorsProjects($id);
$this->set('locations',$this->__getLocations($id,"project"));
$this->set('projSupervisors',$projSupervisors);
if($creator['role_id'] == 2){
$this->set('is_owner',in_array($creator['id'],array_keys($projSupervisors)));
}
else{
$this->set('is_owner', false);
}
$this->set('languages',$languages);
$this->set('tags',$tags);
$this->set('project', $project);
$this->set('_serialize', ['project']);
}
public function export($id = null) {
$dompdf = new Dompdf();
$dompdf->loadHtmlFile('/projects/view/' . $id);
$dompdf->render();
$dompdf->output();
$dompdf->stream('project');
}
In my view.ctp
<button class = 'project_edit' onclick = "location.href='/projects/export/<?= h($project->id) ?>'">Export this Project</button>
Update
I got it figured out. Configured a new .ctp with the same information from my view.ctp and called an export there with the populated data in a php script at the end of my file.

You can use a plugin like cakephp-dompdf, your code will be cleaner.

Related

Retrieve data from database in codeigniter

I am new to Codeigniter so I'm having some difficulties I want to retrieve data from the database and load it in the view but I couldn't figure out how.
Here is my model:
public function viewClientWaterwell(){
$userid = get_client()->userid;
$waterwells = $this->db->select('*')
->from($this->table)
->where('ww_client_id', $userid)
->get()->result_array();
return $waterwells;
}
Here is my clientController:
public function viewClient()
{
$data['waterwells'] = $this->waterwell_model->viewClientWaterwell();
$this->data($data);
$this->view('waterwells/clients/viewClient');
$this->layout();
}
I want to pass some data in a view to the client side but I can't figure out how. I think there is some problem with my model but I cannot figure out what. I'm new to this and will appreciate your feedback. Thanks!
You just need to use second argument to pass data to your template or view file
Here is example to start
Inside your controller function
public function viewClient()
{
$data = array( 'myvar' => 'I love stackoverflow' );
$this->load->view('waterwells/clients/viewClient', $data);
}
And inside your template or view file you can access them by their name.
<?php echo $myvar; ?>

Too few arguments to function when downloading a PDF

I am trying to generate a PDF with some details of an individual user using the Barryvdh DomPDF library but having some problems trying to generate it.
Controller method:
public function downloadPDF(Card $card)
{
$user = User::find($card->user_id);
$pdf = (new \Barryvdh\DomPDF\PDF)->loadView('pdf/cardsReport', $user);
return $pdf->download('cards.pdf');
}
Here is how I am referencing the route.
Download PDF
Route:
$router->get(
'/downloadPDF/{card}',
[
'as' => 'get::admin.download-pdf',
'uses' => 'EditCardController#downloadPDF',
]
);
I get this error:
Type error: Too few arguments to function
Barryvdh\DomPDF\PDF::__construct(), 0 passed in EditCardController.php
and exactly 4 expected.
I'm confused by this as I've seen many samples of using pdf and laravel where you don't need to pass four arguments so was wondering why this would be?
According to documentation, you should use facade instead of constructor:
public function downloadPDF(Card $card)
{
$user = User::find($card->user_id);
$pdf = PDF::loadView('pdf/cardsReport', $user);
return $pdf->download('cards.pdf');
}
Kindly use below format
$data = [
'title' => 'Welcome to ItSolutionStuff.com',
'date' => date('m/d/Y'),
];
$pdf = App::make('dompdf.wrapper');
$pdf->loadView('bill',$data);
return $pdf->stream();

ZendForm: move a picture into a folder

I'm working on a project with ZF2 and Zend Form. I'd like to add an avatar into a user profile.
The problem is that I only get the file name and save it in the DB. I would like to insert it into a folder so I'll be able to get it and display it. The rest of the form is working.
My guess is that I have to get information from $FILES, but I have no idea how to do this. I've read the documentation but can't see how to apply this to my project.
Thank you in advance!
Here's my Controller Action
public function signinAction()
{
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$form = new SignupForm($this->em);
$model = new ViewModel(array("form" => $form));
$url = $this->url()->fromRoute("signin");
$prg = $this->prg($url, true);
if($prg instanceof \Zend\Http\PhpEnvironment\Response){
return $prg;
}
else if($prg === false){
return $model;
}
$user = new User();
$form->bind($user) ;
$form->setData($prg) ;
if($form->isValid()){
$bcrypt = new Bcrypt() ;
$pwd = $bcrypt->create($user->getPassword());
$user->setPassword($pwd);
$this->em->persist($user) ;
$this->em->flush() ;
return $this->redirect()->toRoute('login');
}
return $model ;
}
Here's my form file :
class SignupForm extends Form
{
private $em = null;
public function __construct($em = null) {
$this->em = $em;
parent::__construct('frm-signup');
$this->setAttribute('method', 'post');
$this->setHydrator(new DoctrineEntity($this->em, 'Application\Entity\User'));
//Other fields
...
//File
$this->add(array(
'type' => "File",
'name' => 'avatar',
'attributes' => array(
'value' => 'Avatar',
),
));
//Submit
...
}
}
And the form in my view :
$form = $this->form;
echo $this->form()->openTag($form);
//other formRow
echo $this->formFile($form->get('avatar'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
There are two things you could look at for getting your avatar to work:
Using the Gravatar view helper (uses gravatar.com service that automatically links images to email addresses)
documentation on using the gravatar service can be found here
Upload images yourself with the file upload classes that are shipped with ZF2:
form class for file upload can be found here
input filter class documentation can be found here
If you follow those docs you should be able to manage what you want.
Note: check especially the use of the Zend\Filter\File\RenameUpload filter in the example in the input filter documentation. This filter renames/moves the uploaded avatar file to the desired location.

How to generate PDF from an active View Laravel

I have a search box when user input roll no, it returns the specific student result. I want to generate PDF of that searched data, that data is in table.
Main Question : I want to generate PDF for the result of student who has searched for his/her data, so how to generate PDF for that current student result.
Print/PDF Generator Button:
Print
PDFController:
class PDFController extends Controller
{
public function getPDF(Request $request){
// I want some code here to get the current student result so that I can generate pdf for the current student
$pdf = PDF::loadView('results.single');
return $pdf->stream('result.pdf');
}
}
SearchController:
class ResultsSearchController extends Controller
{
public function search()
{
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
return View::make('results.single')
->with('search',Student::where('rollno',$keyword)
->get())->with('keyword',$keyword);
}else{
return view('errors.404');
}
}
}
Routes.php:
Route::get('/getPDF', 'PDFController#getPDF');
PS : I am using https://github.com/barryvdh/laravel-dompdf
Try this
First of all change route to
Route::get('/getPDF/{id}', 'yourController#getPDF');
Pass the Searched Student id from single view to PDF view like this
Print
and in your PDF Controller
public function getPDF(Request $request,$id){
$student = Student::findOrFail($id);
$pdf = PDF::loadView('pdf.result',['student' => $student]);
return $pdf->stream('result.pdf', array('Attachment'=>0));
}
and get the object in your view like
{!! $student->property !!}
When you call PDF::loadView() I think you need to include the search results, just as you do in search()
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
$results = Student::where('rollno',$keyword)->get();
$pdf = PDF::loadView('results.single', [
'search' => $results,
'keyword' => $keyword
]);
return $pdf->stream('result.pdf');
}

How to Retrieve Image Files from Database Using Yii Framework?

Our Yii Framework application has the following defined as part of the UserProfileImages model:
public function getProfileImages($param, $user_id) {
if(isset($param['select']) && $param['select']=='all'){
$profile_images = UserProfileImages::model()->findAllByAttributes( array( 'user_id'=>$user_id) );
} else {
$profile_images = UserProfileImages::model()->findByAttributes( array( 'user_id'=>$user_id) );
}
return $profile_images;
}
How would I wire up the above snippet to a widget in my view to return all the images for a given user?
Bonus Question: Which image rotator do you suggest to render the above?
In your view file, add something like this, assuming that your controller specified $user_id:
$this->widget('UserProfileImagesWidget', array(
"userProfileImages" => UserProfileImages::getProfileImages(
array("select" => "all"),
$user_id
),
"user_id" => $user_id
));
Depending on your MVC philosophy, you could also retrieve the userProfileImages data in the controller and pass that data to your view.
Define a widget like this:
class UserProfileImagesWidget extends CWidget {
public $user_id;
public $userProfileImages = array();
public function run() {
$this->render("userProfileImage");
}
}
Finally, in the userProfileImages.php view file, you can do something like this:
if(!empty($this->userProfileImages)) {
// Your display magic
// You can access $this->user_id
}
As a side note: You might want to change the order of your parameters in getProfileImages. If $user_id is the first parameter, you can leave out $params completely in case you don't want to specify any.

Categories