html is pass using jquery ajax but pdf is not generating in mpdf for codeigniter - codeigniter

I want to create a PDF in codeigniter using mPDF. My html is passed to the controller using jQuery AJAX. Data is coming to the $html But it is not working. It works fine when html is hard coded. Can any one help me please?
public function pdf($paper='A4')
{
$html = '';
$html = $this->input->POST('content');
$this->load->library('mpdf54/mpdf');
$CI->mpdf = new mPDF('utf-8',$paper);
$mpdf->debug = true;
$this->mpdf->WriteHTML($html);
$this->mpdf->Output();
exit;
}

Try grabbing all the POST vars by using
$html = $this->input->POST();
then echo those out to yourself before moving farther to be sure they are getting set.
public function pdf($paper='A4')
{
print_r($this->input->POST());
return;
}
This of course is only for testing but might help you to see why your $html var isn't getting set. Try that out and give us the results.

Related

How can I download pdf before loading another view in laravel?

after completing a registration formular, when user clicks submit button, I want to first download a pdf and then redirect user to a view. Here is my code :
public function formularSave(Request $request) {
if(!isset($_REQUEST['token'])) {
abort(404);
}
$token = $_REQUEST['token'];
$upd_app = Application::where('token', $token)->update([
'status' => 22
]);
$result = "Registration complete.";
$html .= 'Some test code here
<br>
<p>Date: '.date('d.m.Y H:i:s').'</p>
';
$pdf = PDF::loadHTML($html);
$filename = substr(md5(uniqid().time()), 0, 17) . '.pdf';
$pdf->save(storage_path().'/app/public/uploads/rezolutii/'.$filename);
//code for download pdf HERE!!!!
return view('site.pages.registercomplete', compact('result'));
}
How can I download the pdf, after I create it?
It's impossible on Laravel.
This will not work. Your browser operates on simple requests one goes out one comes in.
There is no way for browser to know if user finished downloading the file and saved it somewhere. The final response from browser if file to be downloaded, nothing can follow that as far as I understand.
Now I'm not sure how that can be handled in javascript but in pure html requests it will not work.
Check this https://laracasts.com/discuss/channels/laravel/redirect-after-download

cakephp ajax function issue

I have 2 functions in "events" (index, event_ajax)controller in my cakephp(2.5) web site. I'm trying to load HTML block to 'index.ctp' page by calling to 'event_ajax' function using ajax. When I call to this function it shows nothing. Look at 'net' tab in firebug it shows internal server error and 'net'->'Response' tab I can see whole layout is loaded.
I'm little confuse about in this scenario, can any one give a little explanation for following questions??? thanks in advance :)
Is it possible to call actions in same controller using ajax function ??
How 'Response' tab shows layout when '$this->layout' is set to NULL ??
when type url 'example.com/events/event_ajax', output data still '$this->autoRender=false'. how can this happen ??
this is my 'event_ajax' action.
public function event_ajax($x=1) {
$this->layout = NULL;
$this->autoRender = false ;
$contName = $this->Page->conName($x);
$latestContEvents = $this->Page->latestContEvent($x);
$internal = '';
if (!empty($latestContEvents)){
foreach ($latestContEvents AS $latestContEvent){
$internal .= '<li class="pull-left"> <div class="content-wrapper">'..... //do something
}
else {
$internal = '<p> No events found for this continent</p>';
}
$ContEvents = '<div class="carousel events-location-carousel">'.$internal.'</div> ';
return $ContEvents;
// return json_encode($ContEvents);
}
Try with
$this->layout = 'ajax';

codeigniter - How to get embed data in my case

i load data in my controller like
$name['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $name, TRUE);
$this->load->view('myView', $data);
and in my view file: myView.php
echo $header;
how can i get data in $name ? thanks
Try:
$data['name'] = "some text!";
$data['header'] = $this->load->view('header_view', $data['name'], TRUE);
$this->load->view('myView', $data);
You can't because when it comes to myView, the header view is allready processed and so data is allready incoroporated into the output string.
The only way is to copy (merge) the contents of $name into $data.
You can get embed data by parsing the content of $data['header'] but it is a really bad solution.
To solve this issue you can generate header and footer along the content inside your controller like that:
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
Or you can use a template engine to include header/footer automaticly with the content template.
Infortunately, there is no buildin way to add an header easily in codeigniter. You'll have to come up with your own functions to do that in a helper or library (but you'll not be able to share data between header and content).

Display Cross-domain feed RSS in Wordpress site

I need to display cross-domain feeds-rss (XML format)in my site, but i get a error because ajax cross-domain call are not allowed. I've been told about json-p...anyone knows how to use or have some good tutorial?
Thanks
the simplest way is just to create an widget for wordpress or download some kind of like your requirement.
Because json-p load data in JSON format if you want to get data from JSON format then the given link will help you :
getJSON
ajax
or you can access the rss feed with php like given example :
$xml = 'http://blog.webtech11.com/feed';
$doc = new DOMDocument();
$doc->load($xml);
$item = $doc->getElementsByTagName('item');
//$data = array();
for($i=0; $i<=3; $i++){
$title = $item->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$link = $item->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
echo '<h2>' . $title . '</h2>';
}
in this example i access latest 4 blog entries..
hope this will help you

CodeIgniter's url segmentation not working with my JSON

It's my first post in here and I haven't yet figured out to format my post properly yet, but here it goes.
So basically I can only get my code to work if i point directly to a php-file. If I try to call a method within my controller, nothing seems to happen.
My JavaScript:
$(document).ready(function() {
$(".guide_button").click(function(){
var id = $(this).text();
var data = {};
data.id = id;
$.getJSON("/guides/hehelol", data, function(response){
$('#test').text(response.id);
});
return false;
});
});
My markup:
<div id="content_pane">
<ul>
<li>RL</li>
<li>LG</li>
<li>RG</li>
<li>SG</li>
<li>GL</li>
<li>MG</li>
</ul>
</div>
<div class="description">
<h3>Description</h3>
<p id="test">This text area will contain a bit of text about the content on this section</p>
</div>
My Controller:
<?php
class Guides extends CI_Controller {
public function Guides()
{
parent::__construct();
$this->load->helper('url');
$this->load->helper('form');
}
public function index()
{
$this->load->view('guides_view');
$title = 'Some title';
}
public function hehelol() //The controller I am desperatly trying to call
{
$id = $_GET['id'];
$arr = array ('id'=>$id);
echo json_encode($arr);
}
}
It might be my controller I have done something wrong with. As it is the code only works if create a hehelol.php file and refer to it directly like this.
$.getJSON("hehelol.php", data, function(response){
$('#test').text(response.id);
});
Anyone who knows what I need to do to make my controller work properly? Help please! :)
i just put your exact code in its entirety in my codeigniter app and it worked for me. Meaning I used this: ...$.getJSON("/guides/hehelol",...
Because you are making a $_GET request, you have to enable query strings.
In your config.php file, make sure this line is set to TRUE:
$config['allow_get_array']= TRUE;

Resources