How to send a jsPDF converted pdf file to backend server?

笑着哭i 提交于 2019-12-04 06:29:03

问题


I need to send a png file to the backend server. I converted it to pdf using jsPDF:

var doc = new jsPDF('l', 'mm', [210, 210]);
doc.addImage(myPngData, 'PNG', 0, 0, 210, 210);

Now I need to send it to the server using my old-school JQuery project:

$.post(url,
  {
    key1: val1,
    key2: val2,
    pdf: //pdf file goes here, will doc work?,
  },

But what should I really send? Cause sending doc just won't work? Unfortunately I cannot just send and check it as the backend is not ready yet.


回答1:


You can use doc.output('datauristring') and send it to your server. Below is my code to send the whole html page to the server, but you get the idea.

function sendToServer() {
    let pdf = new jsPDF('p', 'pt', 'a4');
    pdf.html(document.body, {
        callback: function (pdf) {
            let obj = {};
            obj.pdfContent = pdf.output('datauristring');
            var jsonData = JSON.stringify(obj);
            $.ajax({
                url: '/api/jspdf/html2pdf',
                type: 'POST',
                contentType: 'application/json',
                data: jsonData
            });
        }
    });
}


来源:https://stackoverflow.com/questions/53556465/how-to-send-a-jspdf-converted-pdf-file-to-backend-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!