douge3492 2012-03-10 01:49
浏览 80
已采纳

在新窗口中打开Fpdf JSON响应以保存PDF。 - PHP

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
""
  • 写回答

3条回答 默认 最新

  • dongtang6681 2012-03-10 18:47
    关注

    Save the generated file where? On the users computer? On the server?

    Looks like $data in the createPFD method is the PDF.

    For the former, check http://codeigniter.com/user_guide/helpers/download_helper.html

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

    For the latter, check http://codeigniter.com/user_guide/helpers/file_helper.html

    write_file('/path/to/these/PDFs/fileName.pdf', $data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败