I want to use mpdf to create my PDF-files because I use norwegian letters such as ÆØÅ. The information on the PDF-file would mostly consist of text written by the user in a HTML form. But, I have some problems.

When using this code:

$mpdf->WriteHTML('Text with ÆØÅ');

The PDF will show the special characters.

But when using this:

<?php
include('mpdf/mpdf.php');

$name = 'Name - <b>' . $_POST['name'] . '</b>';

$mpdf = new mPDF();

$mpdf->WriteHTML($name);

$mpdf->Output();
exit;

?>

The special characters will not show.

The HTML form looks like this:

<form action="hidden.php" method="POST">
    <p>Name:</p>
    <input type="text" name="name">

   <input type="submit" value="Send"><input type="reset" value="Clear">
</form>

Why won't the special characters show with this method? And which method should I use?

有帮助吗?

解决方案

Since echoing the POST-data back onto the website does not show the characters as well, this clearly isn't an issue with mpdf. When using content including non-Ascii characters, special care about the websites character encoding has to be taken.

From the mpdf-documentation it can be seen that it supports UTF-8 encoding, so you might want to use that for your data. POST-data is received in the same encoding that is used by the website. So if the website is in latin-1, you will need to call utf8_encode() to convert the POST-data to unicode. If the website already uses UTF-8 you should be just fine.

If you don't set a specific encoding in the website header (which you should always to avoid this kind of trouble), encoding might depend on several factors such as the operating system and configuration on the server or the encoding of the original php sourcefile which, as it turns out, is influenced by your own OS configuration and choice of editor.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top