I am working with a PHP class called mPDF, which is a PHP class that generates PDF files from UTF-8 encoded HTML. It works great, but everything going into it MUST be UTF-8, which is the thing tripping me up I think!
So here is some quick test code that works:
1 <?php
2 define('_MPDF_PATH','mpdf/
');
3 include("mpdf/mpdf.php");
4
4 //this is 6x9 book format
6 $mpdf=new mPDF('en-US','Royal','11',
'dejavuser
ifcondense
d',32,25,2
7,25,16,13
);
7
8 include "poemfile1.php";
9 $mpdf->WriteHTML($html);
10 $mpdf->Output('mpdfA.pdf',
'I');
11 exit;
12 ?>
poemfile1.php looks like this:
<?php
$html='
<h1>Title</h1>
<pre>
lots of text</pre>';
<?php>
Using textpad I had to save poemfile1.php AS UTF-8 format. Then when I run the script, my pdf comes out just fine.
Now, what I really want is to write to a file called 'selections.txt' which will contain one or more poems selected by the user and which come from mySQL database. At this point I haven't brought the db into the picture yet. Right now, I just want to create the selections.txt file, write to it one or more poems, and then create my pdf. If I can get *that* to work, I'll move on to selecting from the DB!
So after googling and reading tons of stuff about PHP and UTF-8, I am now totally boggled. Here is the test code I am trying in the attempt to CREATE a file and save it as UTF-8 format:
1 <?php
2 //iconv_set_encoding("outp
ut_encodin
g", "UTF-8");
3 //iconv_set_encoding("inpu
t_encoding
", "UTF-8");
4 //var_dump(iconv_get_encod
ing('all')
);
5 //========================
==========
==========
==========
========
6 define('_MPDF_PATH','mpdf/
');
7 include("mpdf/mpdf.php");
8 $selections_file="selectio
ns.txt";
9 $input_file="cartog3.htm";
10 //$file_array= array("cartog3.htm","prowl
3.htm");
11
12 //this is 6x9 book format
13 $mpdf=new mPDF('en-US','Royal','11',
'dejavuser
ifcondense
d',32,25,2
7,25,16,13
);
14
15 $input=file_get_contents($
input_file
);
16 //echo $input;
17 $fh=fopen($selections_file
,"w") or die("can't open file");
18
19 //foreach ($file_array as $poetry_file) {
20 //fwrite($fh,utf8_encode($
input));
21 fwrite($fh,iconv("ISO-8859
-1", "UTF-8", $input));
23 fclose($fh);
24 //file_put_contents($selec
tions_file
, $input, FILE_TEXT|FILE_APPEND);
25 //}
26
27 $fh=fopen($selections_file
,"r") or die("can't open file");
28 $html=fread($fh, filesize($selections_file)
);
29 fclose($fh);
30 echo $html;
31
32 $mpdf->WriteHTML($html,2);
33 $mpdf->Output('mpdfA.pdf',
'I');
34 exit;
35 ?>
You can see from various commented lines different things I've been trying. I have saved the file cartog3.htm as a UTF-8 file and it now contains ONLY html and text (unlike the version that works that includes a PHP file with PHP information in it). However, when I echo $html (on line 30), my text is preceded by the characters:

And the PDF of course is just a bunch of junk characters.
Can anybody help? I get the feeling that I am somehow making it harder than it needs to be?
Thanks!