doudouji2016 2015-01-27 17:06
浏览 51
已采纳

如何使用jQuery向最终用户提供实时的PHP生成的PDF而不将其保存在服务器上?

My current setup: I have a php-generated table which shows some data of some "to-be-generated" pdfs. Each row has a checkbox. The "checked" unique values of the checkboxes are written into an array and are send to the createpdf.php - File.

The plan is to open a new window and put the pdf-File in it.

// put all the checked rows into array (e.g. "101,105,107")
$("#tools_savepdf").click(function(){
       arr_id = []; 
       $('.checkbox').each(function(){
            if ( $(this).is(':checked') ){
                arr_id[arr_id.length] = $(this).val();
            } 
       });

// open realtime-generated PDF

       $.ajax({
            type: "POST", 
            url: "createpdfs.php",
            data: { arr_id:arr_id },
            async: "true",
            success: function(data){
                var win = window.open();
                win.document.write(data);
            } //success
        }); //ajax

}); // click

The problem is: The generated PDF is printed to the browser like "%PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x��W�r�H��+ ...".

I want to have it as file. P.S.: Using mimetype-Parameter in ajax did not help.

Is there a ajaxify-to-file option I don't know?

  • 写回答

3条回答 默认 最新

  • doumiebiao6827 2015-01-29 16:43
    关注

    That cost a lot of whiskey and time :D Thank you Luca for your answers. Writing to disk is still not an option, because "parking" the file on disk would use valuable milliseconds on the server-side.

    I divided the transaction into two parts, because posting the selected ID via ajax did not work in my (up there) posted example. First I Post the values into $_SESSION - variables. Then (success-State) I open the link to the php-PDFgenerator.

    What I did was to create a server-sided file which stores the IDs into a Session-Variable :

    writesession.php

    session_start();
    $_SESSION['arr_certificate_id']= $_POST['arr_id'];
    

    ajaxified.php

     // put all the checked rows into array (e.g. "101,105,107")
     $("#tools_savepdf").click(function(){
            arr_id = []; 
            $('.checkbox').each(function(){
                 if ( $(this).is(':checked') ){
                     arr_id[arr_id.length] = $(this).val();
                 } 
            });
    
     // open realtime-generated PDF
    
            $.ajax({
                 type: "POST", 
                 url: "writesession.php",
                 data: { arr_id:arr_id },
                 async: "true",
                 success: function(data){
                     window.open('createpdfs.php');
                 } //success
             }); //ajax
    
     }); // click
    

    Then I modifies the pdfcreator-File to read the session-variables.

    createpdfs.php

    $pdf_id_arr = $_SESSION['arr_certificate_id'];
    

    [...]

    Now that "createpdfs.php" is a hard link without any "POSTS" is works just fine.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?