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


Open Fpdf JSON response, in new window, to save PDF. - PHP

我正试图使用AJAX调用将Json数组发送到控制器函数,该函数使用已在Codeigniter中实现的FPDF库处理数据。我可以看到PDF数据被返回,但我该如何在新窗口中打开这些PDF数据,以便保存PDF。如果PDF无法查看也没关系,我只需要保存生成的文件。

这是我迄今为止的代码:

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

也许有点晚了,我的答案是我让系统工作了。

由于我不理解的原因,它不适用于ajax调用。

这篇文章起到了神奇的作用:JavaScript帖子请求类似于表单提交

HTML(这里用POST参数调用PHP中的PDF生成器):

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

然后是PHP(对于示例,它是一个固定的文本,您可以通过_post获取变量来调整它):

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

将生成的文件保存在哪里?在用户计算机上?在服务器上?

看起来createPFD方法中的$dataPDF。

对于前者,请检查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);