0

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?

John Pixel
  • 57
  • 1
  • 8
  • ... or maybe I don't need jquery and ajax and can POST the array and open the PDF using another way? – John Pixel Jan 27 '15 at 17:42
  • A "save to" would be ok too. – John Pixel Jan 28 '15 at 15:05
  • Slightly related : [How to render a pdf from bytestream in ajax response](http://stackoverflow.com/questions/16996743/how-to-render-a-pdf-from-bytestream-in-ajax-response). Someone has recommended http://mozilla.github.com/pdf.js/ – slaur4 Jan 29 '15 at 17:05
  • I tried pdf.js to display the stream - but it didn't work. The PDF-Browser openend (jihaaaw), but was empty. – John Pixel Feb 02 '15 at 18:01

3 Answers3

0

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 ?

Luca
  • 108
  • 1
  • 11
  • Thank you for your answer. A direct download would be good alternative. The PDF is has no relevance for longtime-storage on the server. All the data in it are just-for-one-use. I am using fpdf to generate the PDF based on the selected rows. Generating a PDF(file) and an additional link would be a workaround, but not a solution. – John Pixel Jan 27 '15 at 17:30
  • If you want to keep the server clean, you can remove the older pdf files easily using filetime and datediff togheter. – Luca Jan 28 '15 at 13:23
  • Do you have any idea how i can force the download directly? That would be a good alternative without writing the file to the server. – John Pixel Jan 28 '15 at 15:11
0

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);
Luca
  • 108
  • 1
  • 11
0

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.

John Pixel
  • 57
  • 1
  • 8