I'm trying to send a Json array over using an AJAX call to a controller function which processes the data using a FPDF library which is already implemented into Codeigniter. I can see the PDF data being returned, but how would I go about opening this PDF data in a new window so the PDF can be saved. It doesn't matter if the PDF cant be viewed i just need to save the generated file.
Here's the code I have so far:
Jquery Code
<script defer="defer" type="text/javascript">
$(document).ready(function() {
$('a#export').click(function(exportRecord) {
var postData = {
'record_type' : 1,
'title' : 'Some Title Here',
'content' : 'Some content here',
};
$.ajax({
url: "<?php echo base_url().'admin/pdf/createPDF';?>",
type:'POST',
data: postData,
dataType: 'json',
success: function(data){
window.open(
'data:application/pdf,'+encodeURIComponent(data),
'Batch Print',
'width=600,height=600,location=_newtab'
);
} // End of success function of ajax form
}); // End of ajax call
return false;
});
});
</script>
The Controller Function
<?php
function createPDF(){
$content = $this->input->post('content');
$font_directory = './assets/fpdf_fonts/';
set_realpath($font_directory);
define('FPDF_FONTPATH',$font_directory);
$data = $this->fpdf->Open();
$data = $this->fpdf->AddPage();
$data = $this->fpdf->SetFont('Arial','',8);
$data = $this->fpdf->Cell(80);
$data = $this->fpdf->Cell(0,0,$content,0,1,'R');
$data = $this->fpdf->Output();
}
The JSON Response
%PDF-1.3
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode /Length 72>>
stream
x�3R��2�35W(�r
Q�w3T��30PISp
��陛)X��(��(hx����+���i*�d�����F
endstream
endobj
1 0 obj
<</Type /Pages
/Kids [3 0 R ]
/Count 1
/MediaBox [0 0 595.28 841.89]
>>
endobj
5 0 obj
<</Type /Font
/BaseFont /Helvetica
/Subtype /Type1
/Encoding /WinAnsiEncoding
>>
endobj
2 0 obj
<<
/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
/Font <<
/F1 5 0 R
>>
/XObject <<
>>
>>
endobj
6 0 obj
<<
/Producer (FPDF 1.7)
/CreationDate (D:20120309201958)
>>
endobj
7 0 obj
<<
/Type /Catalog
/Pages 1 0 R
>>
endobj
xref
0 8
0000000000 65535 f
0000000228 00000 n
0000000411 00000 n
0000000009 00000 n
0000000087 00000 n
0000000315 00000 n
0000000515 00000 n
0000000590 00000 n
trailer
<<
/Size 8
/Root 7 0 R
/Info 6 0 R
>>
startxref
639
%%EOF
""