How to save PDF file from jsPDF on a server in Javascript?

后端 未结 1 803
暗喜
暗喜 2021-01-04 21:13



        
相关标签:
1条回答
  • 2021-01-04 21:48

    Instead of doc.save function you have to use doc.output function with type 'blob' as a parameter.

    In Javascript part:

    var doc = new jsPDF();
    $('#generatereport').click(function()
    {
        doc.fromHTML(
            $('#lppresults'), 15, 15,
            {width: 170},
            function()
            {
                var blob = doc.output('blob');
    
                var formData = new FormData();
                formData.append('pdf', blob);
    
                $.ajax('/upload.php',
                {
                    method: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(data){console.log(data)},
                    error: function(data){console.log(data)}
                });
            }
        );
    });
    

    Here is the code for upload.php:

    <?php
    move_uploaded_file(
        $_FILES['pdf']['tmp_name'], 
        $_SERVER['DOCUMENT_ROOT'] . "/uploads/test.pdf"
    );
    ?>
    
    0 讨论(0)
提交回复
热议问题