如何将jsPDF转换的pdf文件发送到后端服务器? [英] How to send a jsPDF converted pdf file to backend server?

查看:807
本文介绍了如何将jsPDF转换的pdf文件发送到后端服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将png文件发送到后端服务器.我使用jsPDF将其转换为pdf:

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

现在,我需要使用老式的JQuery项目将其发送到服务器:

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?,
  },

但是我应该真正发送什么?导致发送doc只是行不通?不幸的是,由于后端尚未准备好,我无法发送并检查它.

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.

推荐答案

您可以使用

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

这篇关于如何将jsPDF转换的pdf文件发送到后端服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆