This is my very first post. Thanks to all your answers on other posts, from where , i had picked up code and tried to reach here.
Am using JSPDF to send an attachment through AJAX call to PHP server- A mail is sent from here with the generated PDF as attachment. I have been through many posts where they say other languages like Danish ( in this example ) needs to have a custom font.
But my question here is -> When i save the file using doc.save , it is completely fine. But when i send the data through AJAX call to PHP , the attachment has scrambled characters for the specific danish letters.
The HTML body displays the letters as well, but not the PDF attachment.
Has it something to do with the encoding ? that only attached PDF doesnt show the letters( front end saved PDF does show letters).
My HTML Code :
<!--?xml version="1.0" encoding="iso-8859-1"?-->
<!DOCTYPE html>
<html>
<head>
</head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.5.3/jspdf.debug.js"></script>
<script>
function sendmail() {
var doc = new jsPDF('p', 'pt', 'a4');
var con1 = document.getElementById("dispID1");
doc.fromHTML(con1, 10, 10);
doc.save("file.pdf"); // This is to test from frontend if the PDF is saving
var pdf = doc.output();
var data = new FormData();
data.append("data", pdf);
var mailid = document.getElementById('mailadd').value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var mailresp = this.responseText;
// alert(mailresp);
}
}
xmlhttp.open("POST", "/sendmailattach.php?q=" + mailid, true);
xmlhttp.send(data);
}
</script>
<body>
<div id="dispID1">
These are the danish characters å æ ø that needs be sent as PDF attachment
</div>
<div id="dispID2">
<input type="text" style="width:auto;" id="mailadd" size="32" name="mailadd" maxlength="32" value="Indtast email-adresse">
<br>
<input type="submit" style=" width: auto;background-color: #eb5f1f;padding: 5px 5px;border: none;border-radius: 1px;cursor: pointer;text-transform: none;" value="Send" onclick="sendmail()">
</div>
</body>
</html>
My PHP Code from server
<?php
require __DIR__ . '/wp-load.php';
if(!empty($_POST['data'])){
$filepath = __DIR__ . DIRECTORY_SEPARATOR;
$filename = __DIR__ . DIRECTORY_SEPARATOR."test.pdf";
$data = $_POST['data'];
$fname = "test.pdf";
$in = fopen($filename , "a+");
$attachment = array($filename);
fwrite($in, $data.PHP_EOL);
fclose($in);
} else {
$hint= "No Data Sent";
}
// get the q parameter from URL
$q = $_REQUEST["q"];
$to = $q;
$subject = 'This email has attachment';
$body = "<html>
<body>
<p> These are the danish characters “å æ ø ” that needs be sent as PDF attachment-but appearing in email</p>
</body>
</html>";
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers, $attachment );
unlink($filename); // Deletion of the created file.
echo $hint;
?>
:
The PDF File when saved at frontend has clear danish letters:
The mail body: These are the danish characters "å æ ø " that needs be sent as PDF attachment-but appearing in email
The PDF file receieved as an attachment : enter image description here
Request if you could please help with this.