TCPDF - AJAX - Upload the file without saving it to the web server using an AJAX call

I have an AJAX call on the page create_pdf.php:

$('body').on('click', '.PrintButtonWithClass', function (event) {
    var1 = $('#id1').val();
    var2 = $('#id2').val();
    dataString='var1='+var1+'&var2='+var2+'&pdf_name=PdfName&pdf_creator=myname';

    $.ajax({ 
        type: 'post',
        url: '/path/to/createpdf/file/create_pdf.php',
        data: dataString,
        success: function (data) {
            alert('success');
        }
    });
});

Q create_pdf.phpI tried to use this line to upload a file:

$pdf->Output(str_replace(' ','_',utf8_decode($_POST['pdf_name'])).'.pdf', 'D');

I also tried the options FDand Iwithout success, the file does not load.

How can I force a file created without saving it to a web server and not redirect the user to any other page? I want it to stay on the same page, and that the browser opens (for downloading or viewing a dialog box) for the PDF. Is there any way to do this?

EDIT: create_pdf.php expects POST variables. and uses them to create HMTL for pdf.

+4
3

(, ):

<form method="post" id="myform" action="your_url">
    <input name="param1">
</form>

javascript

// create popup window
var wind = window.open('about:blank', '__foo', 'width=700,height=500,status=yes,resizable=yes,scrollbars=yes');

// submit form to popup window
$("#myform").attr("target", "__foo");

php:

header("Content-Type", "application/pdf");

: pdf-, . , , , ,

0

.

AJAX AJAX.

AJAX - .

:

AJAX, .

            $.ajax({ 
                type: 'post',
                url: '/path/to/create_pdf.php',
                data: dataString,
                success: function (data) {

                window.open(
                  data,
                  '_blank' // <- This is what makes it open in a new window.
                );


                window.setTimeout(function () {
                    dataString2 = 'Downloaded=true';
                            $.ajax({
                                type: 'post',
                                url: '/path/to/create_pdf.php',
                                data: dataString2,
                                success: function (data) { alert(data); }, // handler if second request succeeds 
                            });
                }, 5000);



                    },

                  });
0

: csv php

(1) pdf ; (2) CSV .

(http://potoococha.net/) . :

function getCSVText(evt)  {

  if (currentChecklistCountry)  {

    var form = $('<form method="post" action="../php/sendCSV.php?country=' + currentChecklistCountry + '"></form>');

    $('body').append(form);

    form.submit();

    form.remove();
  }
  else  checklistCountryButton.classList.add("needsAttention");
}

function openChecklistPage()  {

   if (!currentChecklistCountry)  {
       checklistCountryButton.innerHTML = "Select Country";
       checklistCountryButton.classList.add("needsAttention");
       return;
    }

   if (gNumDays == undefined) gNumDays = 12;

   vars = "?country="     + currentChecklistCountry;
   vars += "&num_days="   + gNumDays;
   vars += "&line_nos="   + lineNumbers.checked;
   vars += "&left_check=" + leftCheck.checked;
   vars += "&endemics="   + showEndemics.checked;
   vars += "&sci_names="  + !sciNames.checked;
   vars += "&italics="    + !italics.checked;

   window.open( '../php/makePDF.php' + vars, '_blank' );
}

Thus, the getCSVText () methods load the file using a temporary form and then immediately delete it, and openChecklistPage () successfully opens another browser window with the PDF file. A PDF file is never saved on the server. The CSV file has already been saved and has just been extracted. Perhaps you can change the code for your purposes.

0
source

Source: https://habr.com/ru/post/1629353/


All Articles