codeigniter dompdf ajax不保存pdf

时间:2012-07-10 10:36:23

标签: jquery codeigniter-2 dompdf

我正面临一个问题,我已经成功使用dompdf生成PDF文件,但它没有显示到浏览器窗口中,当我尝试使用firebug进行调试时,它向我显示以下pdf格式数据

%PDF-1.3 1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >> endobj 2 0 obj << /Type /Outlines /Count 0 >> endobj 3 0 obj << /Type /Pages /Kids [6 0 R ] /Count 1 /Resources << /ProcSet 4 0 R /Font << /F1 8 0 R >> /XObject << /I1 9 0 R >> >> /MediaBox [0.000 0.000 612.000 792.000] >> endobj 4 0 obj [/PDF /Text /ImageC ] endobj 5 0 obj << /Creator (DOMPDF) /CreationDate (D:20120710102754+00'00') /ModDate (D:20120710102754+00'00') >> endobj 6 0 obj << /Type /Page /Parent 3 0 R /Contents 7 0 R >> endobj 7 0 obj << /Length 400 >> stream 0.000 0.000 0.000 rg BT 34.016 719.095 Td /F1 24.0 Tf [(Darksiders II)] TJ ET BT 34.016 683.969 Td /F1 14.0 Tf [(Panda)] TJ ET BT 34.016 653.268 Td /F1 12.0 Tf [(2012-07-04)] TJ ET q 48.000 0 0 48.000 34.016 586.457 cm /I1 Do Q 0.500 0.500 0.500 rg BT 34.016 627.257 Td /F1 8.0 Tf [(Image not readable or empty)] TJ ET BT 34.016 617.257 Td /F1 8.0 Tf [(media/stats/images/a:2:{i:0;s:36:)] TJ ET endstream endobj 8 0 obj << /Type /Font /Subtype /Type1 /Name /F1 /BaseFont /Times-Bold /Encoding /WinAnsiEncoding >> endobj 9 0 obj << /Type /XObject /Subtype /Image /Width 64 /Height 64 /Filter /FlateDecode /DecodeParms << /Predictor 15 /Colors 3 /Columns 64 /BitsPerComponent 8>> /ColorSpace /DeviceRGB /BitsPerComponent 8 /Length 929>> stream h�՚�n�@���T�R�#���.�"Z!�����P���\!W[M�.����]�,��*@ș��������3�����6��n�O�M]�#����|֯/����D4�I��LJ�pEQ�$UUM\UU�e�l6�O~C�8��|��DD��>�cD�>5��������k�~�gߴZ� ���bQŤ4QY���g����$��"*�b�X�Z�ડ,���#'I�N�O�� b����nD?Q�睑����Q���j+��z�6Xfp�����{��3w@#��k�!�<�<�TU�~�����r��R�y�{���`/��1˲p9��k�8��~�slp�W?��4ǽ��3VI܂�LY� �ӮY���"��|���@p�>9=,k[��;�� u]ߝsl�M��%p����fE��^`\�%=��["�e^b�-�|CHcS[뺶��,���õ��O�n�K�Tv�����}a�5/��N �M��x�7 "��y\���� ���������L(Dt:��,��wQ�$��T����C��(���cs� �3����9�vƒ�R���}�6��XL�R�t:%Ib�!v�2������M�@峷���1H�V_(��C�NS�����\sV�ՀS���a����i��~i`��b�c��0D����N<w���w��r)x�������#���c>8�I��k}�Ӷ���}����Y�Ŵ�ug|_��}������>��M�L��b9/�z�k��Ͼ�ͼ�-����c��i����;B�{�#�֌�O�����`�����*�BvΑ����k}���%�9kǪ���yi>������i�N�z���������f�a�O�`�ݎg�/��� endstream endobj xref 0 10 0000000000 65535 f 0000000008 00000 n 0000000073 00000 n 0000000119 00000 n 0000000299 00000 n 0000000336 00000 n 0000000450 00000 n 0000000513 00000 n 0000000964 00000 n 0000001072 00000 n trailer << /Size 10 /Root 1 0 R /Info 5 0 R >> startxref 2241 %%EOF

但它没有将其保存为pdf文件,我使用以下代码生成pdf文件。

$('#get_pdf').live('click', function(e) {
    e.preventDefault();
    c_id = $('#ca_id').val();
    c_st = $('#ca_start').val();
    c_ed = $('#ca_end').val();              

    $.ajax({
        url: 'get_pdf',
        type: 'POST',
        data: {ca_id: c_id, ca_start: c_st, ca_end: c_ed},
        success: function(data) {
            console.log(data);
        }
    });
});

没有ajax就行了......

1 个答案:

答案 0 :(得分:0)

我不知道你是否考虑过它,但你可以使用get_pdf进行强制下载吗? http://codeigniter.com/user_guide/helpers/download_helper.html

另一种选择是使用dompdf创建临时文件,然后创建该文件的锚点。 在你的get_pdf中:echo json_encode('html'=&gt; $ anchor);

那么你的.ajax成功处理程序就可以有:

success: function (data){
  try{
    var json = $.parseJSON(data);
    if(typeof(json.html)==='string'){
        $(<selector>).html(json.html);
    }
    catch(err){
    .. handle errors ...;
  }
}

我使用的另一种方法是fancybox.net插件,它使用iframe来显示pdf。 这段代码可能会有所帮助:

function setup_fancybox()
{
    $('a.fancybox').unbind('click');
    /* Apply fancybox to multiple items */
    $("a.fancybox").fancybox({
        'transitionIn'  :   'none',
        'transitionOut' :   'none',
        'speedIn'       :   200, 
        'speedOut'      :   200, 
        'overlayShow'   :   false,
        'width'         :   650,
        'height'        :   800,
        'onComplete'    :   function() { parent.$.fancybox.resize();},
        'type'          :   'iframe'
    });


    $('a.fancybox').click(function(){
        return false;
    });
}
相关问题