I have implemented gapi/ 0Auth2 elsewhere in my code and everything works, except the attached PDF is broken. The PDF can not be previewed and is empty when downloaded.
´´´
JavaScript
x
61
61
1
function stuffer() {
2
var sender = "sender@gmail.com";
3
var receiver = "reciver@hotmail.com";
4
5
let dem = pdfCon("ny brab verder");
6
console.log(dem);
7
8
var mail = [
9
'Content-Type: multipart/mixed; boundary="foo_bar_baz"rn',
10
"MIME-Version: 1.0rn",
11
"From: " + sender + "rn",
12
"To: " + receiver + "rn",
13
"Subject: Subject Textrnrn",
14
15
"--foo_bar_bazrn",
16
'Content-Type: text/plain; charset="UTF-8"rn',
17
"MIME-Version: 1.0rn",
18
"Content-Transfer-Encoding: 7bitrnrn",
19
20
"The actual message text goes herernrn",
21
22
"--foo_bar_bazrn",
23
"Content-Type: application/pdfrn",
24
"MIME-Version: 1.0rn",
25
"Content-Transfer-Encoding: base64rn",
26
'Content-Disposition: attachment; filename="generated.pdf"rnrn',
27
28
dem,
29
"rnrn",
30
31
"--foo_bar_baz--",
32
].join("");
33
return sendMessage(mail);
34
35
}
36
37
function sendMessage(message) {
38
var base64EncodedEmail = btoa(message)
39
.replace(/+/g, "-")
40
.replace(///g, "_");
41
gapi.client.gmail.users.messages
42
.send({
43
userId: "me",
44
resource: {
45
raw: base64EncodedEmail,
46
},
47
})
48
.then(function (response) {
49
console.log(response);
50
});
51
}
52
53
function pdfCon(content) {
54
var pdf = new jsPDF();
55
pdf.text(content, 10, 10);
56
pdf.setProperties({
57
title: "new Report",
58
});
59
return pdf.output("datauristring");
60
}
61
´´´
I have tried premade PDF, rather than jsPDF.
I have followed the documentation on google and looked at different post, but there seem to be no documentation for this specifik problem.
Advertisement
Answer
From your script, I thought that the returned value from pdfCon("ny brab verder")
is the data URL. So, how about the following modification?
From:
JavaScript
1
2
1
let dem = pdfCon("ny brab verder");
2
To:
JavaScript
1
2
1
let dem = pdfCon("ny brab verder").split(",")[1];
2
- In this modification, the header is removed from the data URL. By this, only the base64 data is retrieved.