0

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:

Front end stored PDF image

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.

Sam
  • 1
  • 1
  • Why is your HTML code starting with ``? You should really be using UTF-8 _everywhere_. – CBroe Mar 09 '23 at 11:15
  • @CBroe, i tried with with no luck still – Sam Mar 09 '23 at 11:54
  • [UTF-8 all the way through](https://stackoverflow.com/q/279170/1427878) has some good hints in which places you have to pay attention in terms of character encoding. – CBroe Mar 09 '23 at 12:13
  • `$in = fopen($filename , "a+");` - the `+` hardly makes sense here, you don't want to write multiple PDF into one file. And this should probably also use the `b` flag. (Or use file_put_contents, to get the whole thing done with one call.) – CBroe Mar 09 '23 at 12:14
  • @CBroe, made changes to code with file_put_contents($filename, $data); The issue still exists. Could you please help why generated pdf on server doesnt take these special characters – Sam Mar 09 '23 at 12:57
  • I can't look into your system from the outside, _you_ will have to debug this. Start by going through the link I gave you, and verify that you are indeed handling encoding correctly in all relevant places. – CBroe Mar 09 '23 at 13:48

0 Answers0