如何使用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?

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.

I don't understand why you want to force the Pdf to be opened in the browser window ? Why you don't force the download or provide a link to download it once it has been created ?

You have to write the file on the hard drive anyway, but you can remove it after it has been downloaded.

/*Force the script to delete the file even if the browser is closed by the user*/
ignore_user_abort(true);

/*Force the download opening a save as dialog*/
$path = "path to file/file.pdf";
$filename = "file.pdf";
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($path));
header('Content-Encoding: none');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename=' . $filename);
readfile($path);


/*Remove the file from the hard drive*/
unlink($path);