0

I need to created pdf in my project so I am using pdfmake js plugin it is working fine but I want to put that file to server, I tried to do this using file_put_content but this is not working,my question is how to put pdf file to server once it is generated by plugin.

<button class="btn btn-success">Generate pdf</button>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" ></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.22/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>


    <script>
     $("body").on("click", ".save_changes", function () {

        var ts = String(new Date().getTime()), i = 0, out = '';
        for (i = 0; i < ts.length; i += 2) {
            out += Number(ts.substr(i, 2)).toString(36);
        }
        var newdate = ('d' + out);
        html2canvas($('#page-wrap')[0], {
            onrendered: function (canvas) {
                var data = canvas.toDataURL();
                var docDefinition = {
                    content: [{
                            image: data,
                            width: 500
                        }]
                };
         
                   pdfMake.createPdf(docDefinition).download(newdate + ".pdf");
                   
                   
                var file_created = pdfMake.createPdf(docDefinition);
                <?php
$file_location = $_SERVER['DOCUMENT_ROOT'] . "folder_name/pdfReports/";
file_put_contents($file_location, $pdf);
?>
            }
        });
    });
    </script>
user_1234
  • 741
  • 1
  • 9
  • 22
  • What exactly does the PHP variable `$pdf` contain? – M. Eriksson Jun 28 '20 at 11:38
  • @MagnusEriksson by mistake I kept, we can't assign js variable value to php – user_1234 Jun 28 '20 at 11:40
  • 1
    Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) - PHP is _server side_ and Javascript is _client side_. They can't "share" variables. They are executed in completely different environment. You can send data (like the file contents of the PDF) from Javascript to PHP, using Ajax or similar. – M. Eriksson Jun 28 '20 at 11:41
  • Instead of downloading i want to save to server,any idea? – user_1234 Jun 28 '20 at 11:41
  • yes, client side language is different from server side,so no chance to save. – user_1234 Jun 28 '20 at 11:42
  • You can send the file to your server using Ajax. Search for "upload using ajax" and you should get a bunch of different examples. – M. Eriksson Jun 28 '20 at 11:44

0 Answers0