简体   繁体   English

如何将jsPDF转换的pdf文件发送到后端服务器?

[英]How to send a jsPDF converted pdf file to backend server?

I need to send a png file to the backend server. 我需要将png文件发送到后端服务器。 I converted it to pdf using jsPDF: 我使用jsPDF将其转换为pdf:

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: 现在,我需要使用老式的JQuery项目将其发送到服务器:

$.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? 因为发送doc只是行不通? Unfortunately I cannot just send and check it as the backend is not ready yet. 不幸的是,由于后端尚未准备好,我无法发送并检查它。

You can use doc.output('datauristring') and send it to your server. 您可以使用doc.output('datauristring')并将其发送到您的服务器。 Below is my code to send the whole html page to the server, but you get the idea. 下面是我将整个html页面发送到服务器的代码,但是您明白了。

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
            });
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM