Question

I wrote a small PHP script which does a "branding" on a present PDF file. This means on every page I put a string like "belongs to " at a special position. Therefor I use Zend_Pdf out of the Zend Framework. Because the script is used in German language area, in one line there I use the special character "ö" ("Gehört zu ").

On my local machine (Windows, XAMPP) the script worked fine, but when moving it to my hoster's webspace (some Linux), the string breaks at "ö". That means in my PDF on appears "Geh".

The code is this:

        if (substr($file, strlen($file) - 4)  === '.pdf') {
            $name = $user->GetName;
            $fontSize = 12;
            $xTextPos = 100;
            $yTextPos = 10;

            set_include_path(dirname(__FILE__)); // set include_path for external library Zend Framework
            require_once('Zend' .DS . 'Pdf.php');
            $pdf = Zend_Pdf::load($file);
            $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);

            $branding = 'Gehört zu ' . $name; // German for: 'Belongs to ', problem with 'ö'

            foreach ($pdf->pages as &$page) {
                $page->setFont($font, $fontSize);
                $page->drawText($branding, $xTextPos, $yTextPos);
            }
        }

I guess the problem is related to some kind of default charset or language setting of the PHP environment. So I searched here and tried out:

            $branding = utf8_encode('Gehört zu ') . $name;

...and I made some experiments with functions like html_entity_decode but nothing helped and I decided stopping groping in the dark and open an own question.

Looking forward to any hints. Thank you in advance for your help!

EDIT: Meanwhile I found the same (?) problem, solved on a German forum. But if I do it like they say...

             $branding = mb_convert_encoding('Gehört zu ', 'ISO-8859-1') . $name;

... the resulting branding in the PDF is "Gehrt zu ". The "ö" is skipped now.

For this I found another hint on the Zend issue tracker.

I sum up, that I can drop all UTF8 things and concentrate on Latin-1 AKA ISO 8859-1.

I still don't understand why the code worked on my Windows + XAMPP and now crashes on my hoster's Linux.

Was it helpful?

Solution

Your guess is right, the problem is related to encoding. Where exactly the encoding is messed up is hard to say from afar. I'm assuming you work not only with Zend_Pdf, but also have the MVC in place (meaning a complete Zend_Application).

You should check if your application serves pages as UTF-8, by setting:

resources.view.encoding = "UTF-8"

and also placing the appropriate meta-tags in your layout/view.

Depending on what Editor you use, your files may be encoded in a different encoding. You can use Notepad++ on Windows to check your file-encoding and for converting it to UTF-8 (don't just set the encoding to UTF-8, this might mess up your file!) if necessary. I recommend using Eclipse with text file encoding set to "UTF-8" (Preferences > General > Workspace) to make sure your code files are encoded in UTF-8.

Now comes the crucial part:

Zend_Pdf_Page::drawText(string $text, float $x, float $y, string $charEncoding)

See that last argument... set it. If you're lucky, you can skip the previous stuff and just set the encoding there.

edit: I missed something. Database connections. You should check the encoding there too. I frequently work with MS SQL Server, which uses Latin-1 internally; not setting driver_otpions.CharacterSet can mess up stuff pretty bad too. This might be relevant, if you have soemthing like Gehört zu: Günther, where the Name Günther is fetched from db.

OTHER TIPS

Encoding is also depending of the file encoding. If you encode your file in UTF8 for example and use ut8_encode("ö"), then you'll encode in UTF_8 something already in UTF_8.

So you may want to check what your file encoding is, and what your PDF lib is requiring. Then apply the right formula/transformation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top