问题
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