2012-07-10 41 views
0

我面臨一個問題,我成功地使用dompdf生成PDF文件,但它沒有顯示到瀏覽器窗口中,當我嘗試使用螢火蟲進行調試時,它顯示我以下pdf格式數據codeigniter dompdf ajax不保存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�����{��[email protected]#��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精...

+0

通過AJAX的PDF是困難的,但並非不可能。我想問的第一個問題是:你爲什麼試圖通過AJAX來做到這一點?你的工作流程是什麼?你是說你想要用戶點擊一個鏈接,然後讓瀏覽器彈出一個對話框來保存PDF?如果是這種情況,我不會打擾AJAX。 – BrianS 2012-07-12 01:03:42

回答

0

我不知道你有沒有考慮過,但你可以使用get_pdf到forcedownload? http://codeigniter.com/user_guide/helpers/download_helper.html

另一種選擇是使用dompdf創建一個臨時文件,然後爲該文件創建一個錨點。你的get_pdf中的 :echo json_encode('html'=> $ anchor);

那麼你阿賈克斯成功處理程序,可以有這樣的:

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