2012-03-10 107 views
1

我想通過使用AJAX調用發送Json數組到一個控制器函數,該函數使用已經在Codeigniter中實現的FPDF庫來處理數據。我可以看到正在返回的PDF數據,但是如何在新窗口中打開PDF數據以便保存PDF。如果PDF不能被查看,我只需要保存生成的文件。在新窗口中打開Fpdf JSON響應以保存PDF。 - PHP

下面的代碼我到目前爲止:

jQuery代碼

<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> 

控制器功能

<?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(); 
    } 

JSON響應

%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 
"" 

回答

0

將生成的文件保存在哪裏?在用戶電腦上?在服務器上?

看起來像$data中的createPFD方法 PDF。

對於前者,檢查http://codeigniter.com/user_guide/helpers/download_helper.html

force_download('coolPDF.pdf', $data); 

對於後者,檢查http://codeigniter.com/user_guide/helpers/file_helper.html

write_file('/path/to/these/PDFs/fileName.pdf', $data); 
+0

將文件下載到用戶的計算機 – 2012-03-11 15:00:00

+0

OK,所以第一個('force_download( 'coolPDF.pdf',$數據);')將做的工作。 – stormdrain 2012-03-11 15:10:45

1

也許有點晚了,還有就是我的答案,我得到了系統的工作。

由於我不明白它的原因不能使用ajax調用。

這個帖子幫着做了魔法: JavaScript post request like a form submit

HTML(這裏調用PHP中的PDF生成與POST參數):

post('GenerateStockLabelsWPDF.php', { CSVTable: JSON.stringify(encodeURIComponent(CSVtable))}); 

然後PHP(樣這是一個固定的文本

<?php 

include "php/fpdf/fpdf.php"; 

$pdf = new FPDF(); 
$pdf->Open(); 
$pdf->SetMargins(0, 0); 
$pdf->SetAutoPageBreak(false); 
$pdf->AddPage(); 

$content = 'thisis a test'; 

$pdf->SetXY(20, 20); 
$pdf->SetFont('Helvetica', 'B', 10); 
$pdf->MultiCell(150, 5, $content, 0, 'L'); 

$pdf->Output('Labels.pdf', 'D'); 

exit; 

?> 
0

我通過POST發送參數沒有Ajax和新WINDO返回PDF:您可以通過獲得通過_POST變量)適應它WS:

var form = document.createElement("form"); 
form.setAttribute("method", "post"); 
form.setAttribute("action", "admin/pdf/createPDF.aspx"); 

var hiddenField = document.createElement("input"); 
hiddenField.setAttribute("name", "record_type"); 
hiddenField.setAttribute("value", "1"); 
hiddenField.setAttribute("name", "title"); 
hiddenField.setAttribute("value", "Some Title Here"); 
hiddenField.setAttribute("name", "content"); 
hiddenField.setAttribute("value", "Some content here"); 

form.appendChild(hiddenField); 
form.setAttribute("target", "_blank"); 
document.body.appendChild(form); // Not entirely sure if this is necessary   
form.submit(); 
return;