Ajax Has Error When Call Pdf

hi,

please help me,

I have code in view like here :




echo CHtml::ajaxButton ("Cetak", array('anggota/cetakDetailLaporanKPL'),

                  array('type'=>"POST",

		'update'=>'#laporan_kpl_data',

		'data'=> array(

'bulan_kpl' => 'js:document.getElementById("bulan_kpl").value',

'tahun_kpl' => 'js:document.getElementById("tahun_kpl").value',

'cabang_kpl' => 'js:document.getElementById("cabang_kpl").value',

'lingkungan_kpl' => 'js:document.getElementById("lingkungan_kpl").value',

),

));



and action in in controller :




public function actionCetakDetailLaporanKPL(){

	$html2pdf = Yii::app()->ePdf->HTML2PDF();

       	$html2pdf->WriteHTML("testing");

        $html2pdf->Output('LaporanKPL.pdf'); 

	}



if I test the action by url http://localhost/cumandiri/index.php?r=anggota/cetakDetailLaporanKPL (it works as expected)

but if I test by ajax button, I got error contains strange characters as below:

[color="#FF0000"]%PDF-1.7 3 0 obj <> /Resources 2 0 R /Contents 4 0 R>> endobj 4 0 obj <> stream x�3�35W(W0R�R0P�R��R)@��g``�������f�h�+��)��p�� $RN��B�bd�(]h(Ѐ"�����������������BH�B�FIjqIf^�f�B�HQ W!6��.X�A� endstream endobj 1 0 obj <> endobj 5 0 obj << /Type /OCG /Name (��print) /Usage << /Print <> /View <> >> >> endobj 6 0 obj << /Type /OCG /Name (��view) /Usage << /Print <> /View <> >> >> endobj 7 0 obj <> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 7 0 R >> /XObject << >> /Properties <> /ExtGState << >> >> endobj 8 0 obj << /Creator (��HTML2PDF - TCPDF) /Producer (��TCPDF 5.0.002 \(http://www.tcpdf.org\) \(TCPDF\)) /CreationDate (D:20140705031659+00’00’) /ModDate (D:20140705031659+00’00’) >> endobj 9 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /FitH null] /PageLayout /SinglePage /PageMode /UseNone /Names << >> /ViewerPreferences << /Direction /L2R >> /OCProperties <> <>]>>>> >> endobj xref 0 10 0000000000 65535 f 0000000377 00000 n 0000000779 00000 n 0000000009 00000 n 0000000175 00000 n 0000000437 00000 n 0000000556 00000 n 0000000673 00000 n 0000000938 00000 n 0000001187 00000 n trailer << /Size 10 /Root 9 0 R /Info 8 0 R >> startxref 1552 %%EOF [/color]

I really don’t understand what is that…

thank you in advance…

You’re outputting a application/pdf mime type in an ajax call that expects to get an application/json mime type, which is wrong. You need to pack your PDF in that ajax call and instead of echoing the output, write it into a file and return that file url so that on click it will open the file directly in the browser and read the PDF.

thank you twisted1919 for your reply,

would you like to show me some part of codes how to solve it?

because I still don’t understand how to pack PDF in my ajax call

thank you

Well, your ajax call goes to:




public function actionCetakDetailLaporanKPL(){

        $html2pdf = Yii::app()->ePdf->HTML2PDF();

        $html2pdf->WriteHTML("testing");

        $html2pdf->Output('LaporanKPL.pdf'); 

        }



which calls


$html2pdf->Output('LaporanKPL.pdf'); 

This library must have a method to write the file, something like:


$html2pdf->Save('/var/www/html/public/docs/LaporanKPL.pdf'); 

And then your php code would become something like this:




public function actionCetakDetailLaporanKPL(){

        $html2pdf = Yii::app()->ePdf->HTML2PDF();

        $html2pdf->WriteHTML("testing");

        $html2pdf->Save('/var/www/html/public/docs/LaporanKPL.pdf');

        exit(json_encode(

           'url' => 'http://www.your-site.com/public/docs/LaporanKPL.pdf',

        ));

        }



Then in your ajax call, you will look in the response for the ‘url’ key and maybe construct a link based on it, then output that link.

The above is almost pseudo-code, you’ll have to figure out the rest :)

thank you twisted1919,

but I don’t need to save the PDF file as result, I just want the file PDF show in browser.

or

is it possible to ignore the ajax header? because I don’t need to return any value from the ajax after file PDF created.

thank you.

I guess you could try :




public function actionCetakDetailLaporanKPL(){

        header('Content-Type: application/pdf'); // see this

        $html2pdf = Yii::app()->ePdf->HTML2PDF();

        $html2pdf->WriteHTML("testing");

        $html2pdf->Output('LaporanKPL.pdf'); 

        }



But i doubt it will work if you insert it into html after the ajax call, you can’t just take the pdf content and spit it in html and expect to be fine.

Just give up on using ajax for this, maybe use an iframe to load the target page, would be simpler and would work.

actually I used ajax to get some values from view to be passed to action.

or

do you have any idea how to get some values from the view textField after I clicked button ?

so in this case, is possible to use Chtml:button/link than Chtml:ajaxButton.

please help me,

pass the values as $_GET params in your url that loads the iframe…




CHtml::link(array('controller/action', 'bulan_kpl' => '...', 'tahun_kpl' => '...'), 'Whatever');



I am still new in iframe, I never use it.

would you like to show me code that usage iframe ?

here is my complete code in view :




<table>

	<tr>

		<td><?php echo CHtml::label('Periode', '');?></td>

		<td>:</td>

		<td><?php echo CHtml::dropDownList('bulan_kpl','', $modelAnggota->getPilihanBulan());?>

		</td>

		<td>-</td>

		<td><?php echo CHtml::dropDownList('tahun_kpl','', $modelAnggota->getPilihanTahun());?>

		</td>

	</tr>

	<tr>

		<td><?php echo CHtml::label('Cabang', '');?></td>

		<td>:</td>

		<td><?php	//echo CHtml::activeDropDownList($modelCabang,'Kd_Cabang'));?>

			<?php

				echo CHtml::dropDownList(

										'cabang_kpl','cabang',

										CHtml::listData(Cabang::model()->findAll(), 'Kd_Cabang', 'concate'),

										array(

												'empty'=>'Pilih Cabang',

												'ajax' => array(

													'type'=>'POST', //request type

													'url'=>CController::createUrl('anggota/getLingkungan'), //url to call.

													'update'=>'#lingkungan_kpl', //selector to update

													'data'=>array('cabang'=>'js:this.value'), 

													)

										)

									);

			?>

		

		</td>

	</tr>

	<tr>

		<td><?php echo CHtml::label('Komisariat', '');?></td>

		<td>:</td>

		<td><?php echo CHtml::dropDownList('lingkungan_kpl','', array());?></td>

		<td></td>

		<td><?php echo CHtml::CheckBox('semuaLingkungan',false, array('value'=>'Semua'));echo "Semua Lingkungan"; ?></td>

	</tr>

	<tr>

		<td colspan="2"><?php 

					echo CHtml::ajaxButton ("Cetak", array('anggota/cetakDetailLaporanKPL'),

                  array('type'=>"POST",

						 'update'=>'#laporan_kpl_data',

						 'data'=> array('bulan_kpl' => 'js:document.getElementById("bulan_kpl").value',

										'tahun_kpl' => 'js:document.getElementById("tahun_kpl").value',

										'cabang_kpl' => 'js:document.getElementById("cabang_kpl").value',

										'lingkungan_kpl' => 'js:document.getElementById("lingkungan_kpl").value',

										

									),

															

									));

						

						?>

		</td>

		<td></td>

		<td></td>

	</tr>

</table>

<div id="laporan_kpl_data"></div>




thank you in advance for your help