Getting blank pages in laravel while using dom-pdf - laravel-4

I am using laravel 4.2 with dompdf.
My controller:
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
}
My view:
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
$html = '';
$html .= '<html><body>hi</body></html>';
echo $html;
?>
The code which gives a 'hi' in the Pdf file.
public function getLedgerReport() {
$pdf = PDF::loadView('sales.ledger_report')->setPaper('a4');
$pdf->stream();
$invoice_path = 'downloads/invoices/invoice.pdf';
$pdf->save($invoice_path);
}
I get a blank page instead of getting a 'hi' text.
But when I save the same as a pdf file I do see a 'hi' in the file.

Related

Try to use the codeigniter's file upload library as a general function from Helpers

Can anybody help as I am trying to use the codeigniter's upload library from the helpers folder but I keep getting the same error that I am not selecting an image to upload? Has any body tried this before?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
The Helper Function
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
There are some minor mistakes in your code, please fix it as below,
$result = doUpload($_FILES['image']);
here you should pass the form field name, as per your code image is the name of file input.
so your code should be like
$result = doUpload('image');
then, inside the function doUpload you should update the code
from
$CI->upload->do_upload($param['name'])
to
$CI->upload->do_upload($param)
because Name of the form field should be pass to the do_upload function to make successful file upload.
NOTE
Make sure you added the enctype="multipart/form-data" in the form
element

New Version of Chrome cannot render dd function in network preview window Laravel

I want to share live saver with people who debugging with dd() and have to refresh every time because it gets status 200 and stuck.
If you want to change status to 500 for your ajax requests you just need to update
project/vendor/symfony/var-dumper/Dumper/HtmlDumper.php
class on line 111 where dump() method exists.
Just add line http_response_code(500); at the beginning of function. It works for Laravel 5.6 version.
Answer I found in: https://github.com/laravel/framework/issues/21808
See my solution at
https://gist.github.com/fontenele/7625cb71c0a8356213abc727278b48d5
I created a new helper, get the content, replaced div for span because google does not permit <DIV> tags in DevTools.
use Illuminate\Support\Debug\Dumper;
if (!function_exists('_dd')) {
function _dd(...$args)
{
$content = '<span>';
ob_start();
foreach ($args as $x) {
(new Dumper)->dump($x);
}
$content .= ob_get_contents();
ob_end_clean();
$content.= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}
}
Updated
Lumen:
function _dd(...$args)
{
$content = '<span>';
ob_start();
dump(...$args);
$content .= ob_get_contents();
ob_end_clean();
$content .= '</span>';
$content = str_replace(['<div', '</div>'], ['<span', '</span>'], $content);
response()->make($content, 500, ['Content-Type' => 'text/html'])->send();
die(1);
}

How to save images url and images in laravel 5.4 with ajax?

I have connected on the page welcome.blade.php javascript cropbox that truncates the image. After click on button Crop i get the string like data:image/png;base64,ivBOrwqnmdIo.........................................................................................
I write my function to send with ajax sendAvatar(img) and adding to the bottom of klick event
$('#btnCrop').on('click', function(){
var img = cropper.getDataURL();
sendAvatar(img);
})
Next i try to send to my web.php with ajax
function sendAvatar(img){
var url = '{{ URL::to('getavatar') }}';
var token = '{!! csrf_token() !!}';
$.ajax({
method: 'POST',
url: url,
data: {_token: token, img: img},
success: function(){
alert(img);
}
});
}
I have a model Avatar.php with field avatar_url
class Avatar extends Model
{
protected $fillable=['id','avatar_url'];
}
Now i try in my web.php store the image in my db
But i don t know how to do this. Please help me
I am use laravel 5.4
In my case i used this approach
In my web.php
Route::post('/getavatar', 'AvatarController#saveAvatar');
In my AvatarController
use App\Avatar;
use Auth;
class AvatarController extends Controller
{
public function saveAvatar(Request $request)
{
$data = $request->get('img');
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$avatar_owner = Auth::user()->id;
$avatarName = rand(000000000, 999999999) . '-' . $avatar_owner .'.png';
$avatar_uri = file_put_contents(public_path() . '/images/' . $avatarName, $data);
$avatar = new Avatar();
$avatar->avatar_url = $avatarName;
$avatar->save();
}
}
The img field that you send to server is in string format and encoded as base64. you should convert base64 string to binary format via some php code like this; then store binary file and then put the path of the binary file in database.
cropper.getDataURL(); <-- this returns base_64 data so you need to store this data into file using php
here is php code sample how to do this
do this in your controller and store $logoName in your database
$logoName = null;
if( isset($_POST['imagebase64']) and strlen($_POST['imagebase64']) > 700 )
{
$data = $_POST['imagebase64'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
$logoName = rand(000000000, 999999999) . '.png';
file_put_contents(public_path() . '/some_path/' . $logoName, $data);
}

Creating a Dynamic PDF and Saving it in a folder using Loop with MYSQL and Codeigniter

This is the public function with looping (Edited)
public function generate_salary_slip(){
$this->load->helper('file');
$this->load->library('m_pdf');
$data = [];
$slip = $this->Admin_Login_Model->generate_salary_slip();
//$html = array();
$html='';
if(!empty($slip)){
foreach($slip as $slip_list){
$id = $slip_list->emp_id;
$data['attendance'] = $this->Admin_Login_Model->get_slip_attendance($id);
$data['emp_profile'] = $this->Admin_Login_Model->get_emp_details($id);
$data['late_day'] = $this->Admin_Login_Model->get_late_days($id);
$data['half_days'] = $this->Admin_Login_Model->get_half_days($id);
$data['salary'] = $this->Admin_Login_Model->get_salary_slip($id);
$data['second_half'] = $this->Admin_Login_Model->get_half_Second_days($id);
//$html=$this->load->view('admin/payslip',$data);
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html .= $this->load->view('admin/payslip', $data, true);
}
$htmlval = $html;
//echo $html;exit;
$this->m_pdf->pdf->WriteHTML($htmlval);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");
$this->session->set_flashdata('success','Salary slips saved in directory!');
redirect('admin_attendance_list');
}else{
$this->session->set_flashdata('message','Salary slips not available for this month!');
redirect('admin_attendance_list');
}
}
File is getting saved in the folder, but Only with the First Record. Also, Repeated loop is getting generated, but with same record.
How to create a HTML by loop.
PS: Using MPDF to generate PDF File
Any hep will be highly appreciated.
You need to take the render, writeHtml and Output calls outside the forech loop, there is you problem.
You could save all the data in a var and then render the HTML as you are doing now and then generate the PDF.
EDIT: Your code should look something like this (please read comments below):
$htmlData = array();
if(!empty($slip)){
foreach ($slip as $slip_list) {
//
$htmlData[] = $data;
}
$pdfFilePath = date("F_Y")."salary_slip.pdf";
$html = $this->load->view('admin/payslip', $htmlData, true);
$this->m_pdf->pdf->WriteHTML($html);
$this->m_pdf->pdf->Output('salary_slips/'.$pdfFilePath, "F");

Laravel5 vsmoraes/pdf-laravel5 errors

I'm using this library.So i have this errors when i download
Undefined variable: cv_info
Here is my controller
public function downloadPDF()
{
$url=Input::get('url');
$url=explode('/', $url);
$cv_id = substr($url[4], 0, -13);
$name=Auth::user()->username;
$cv_info=CV::where('id','=',$cv_id)->first();
$html = view('ui.userinfo.viewCV',$cv_info)->render();
return $this->pdf
->load($html)
->show();
}
I'm already pass the $cv_info to view why i have this erros.Thanks for help
$html = view('ui.userinfo.viewCV',$cv_info)->render();
You aren't passing it correctly.
$html = view('ui.userinfo.viewCV')->with('cv_info', $cv_info);
Or you could use
$html = view('ui.userinfo.viewCV', ['cv_info' => $cv_info])->render();

Resources