20

I'm using wkhtmltopdf to export html pages to pdf, but it seems it has a problem with Czech characters... I load whole html into variable, then I change encoding and run wkhtmltopdf like this:

$html = ob_get_clean();
$html = iconv("UTF-8","Windows-1250", $html);
file_put_contents('../export.php', $html);

$commandString = WKHTML_LIB.'http://www.estiroad.com/export.php sestava.pdf';
exec($commandString);

The .html file has the right encoding, but even when I set --encoding windows-1250 parameter to command string, its just not working... Thanks for any ideas...

EDIT: I solved the issue! The catch was in constant WKHTML_LIB, which I defined on the beginning of the page:

define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-amd64");

I just wrote the path directly to the exec(); command and now it works even with the flags. Sorry for bothering you with such a triviality... Now the $commandString line looks like this:

$commandString = '"../wkhtmltopdf/wkhtmltopdf-amd64" --print-media-type --page-size A4 -R     50 --encoding windows-1250 --header-html header.html --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm http://www.estiroad.com/export.php sestava.pdf';
Bart
  • 19,692
  • 7
  • 68
  • 77
Michal S
  • 1,103
  • 6
  • 19
  • 31
  • Should there be a space before http? `WKHTML_LIB.' http:/` – Cups Aug 08 '12 at 08:53
  • It shouldn´t, but it takes no effect whether it is there or is not.. Command is executed without problems, pdf generated, but it is incorrectly encoded.... For example, instead of "ř" there is "ø" ... – Michal S Aug 08 '12 at 08:58
  • If you solved the issue, make it an actual answer. Don't add [solved] to the title. – Bart Aug 31 '12 at 13:17

4 Answers4

72

For future reference:

I had the same problem with german umlauts.

As soon as I added

<meta charset="UTF-8" />

to the html page the problem was solved.

That of course presupposes your page is served as utf-8.

Marcel Burkhard
  • 3,453
  • 1
  • 29
  • 35
5

I solved the issue! The catch was in constant WKHTML_LIB, which I defined on the beginning of the page:

define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-amd64");

I just wrote the path directly to the exec(); command and now it works even with the flags. Sorry for bothering you with such a triviality... Now the $commandString line looks like this:

$commandString = '"../wkhtmltopdf/wkhtmltopdf-amd64" --print-media-type --page-size A4 -R     50 --encoding windows-1250 --header-html header.html --margin-top 10mm --margin-bottom 10mm --margin-left 10mm --margin-right 10mm http://www.estiroad.com/export.php sestava.pdf';
Michal S
  • 1,103
  • 6
  • 19
  • 31
  • Possibly in original solution it was missing the quotes inside the quotes, so that they appear when inserted into the command string. Like '"some path with spaces and stuff"'. – userfuser Apr 29 '14 at 21:11
0

I had a similar problem with wkhtmltopdf before where the Chinese characters were not rendered properly. I have solved the problem by installing the appropriate Chinese fonts.

By any chance do you have to install any Czech related fonts for the characters to render properly? As I am not familiar with Czech characters / fonts, I am not sure whether this applies. Hope this helps.

Leon Chung
  • 329
  • 2
  • 6
  • I think that this won´t help... Almost every font I have installed supports czech characters and additionally, this wouldn´t solve the problem if distant user would want to display the pdf... :/ – Michal S Aug 08 '12 at 09:29
  • BTW: special czech characters are: ť, ď, ň, ó, ě, š, č, ř, ž, ý, á, í, é, ú, ů. – Michal S Aug 08 '12 at 09:31
  • Is there any reason you had to convert to encoding from UTF-8 to Windows-1250? Also, perhaps try using iconv with //IGNORE appended to the out_charset parameter. Maybe also give mb_convert_encoding a try. – Leon Chung Aug 08 '12 at 09:45
  • When I leave it in UTF-8, it also shows wrong characters. Win1250 is the only encoding, that showed me html page in correct characters. – Michal S Aug 08 '12 at 09:47
  • I believe //TRANSLIT is more appropriate for my above comment. But since it shows correctly in your browser, I guess it is not an encoding problem. – Leon Chung Aug 08 '12 at 09:57
  • Unfortunately, IGNORE and TRANSLIT seem to take no effect :( – Michal S Aug 08 '12 at 11:23
0

Just now I made a test with those characters you provided and they work fine for me. Win7, wkhtmltopdf 0.11.0 rc2. Works on cover, toc, bookmarks, content and headers. Also tested with korean and chinese characters and even they work.

PDF Generated using (file locations removed) --print-media-type --page-size A5 --header-html header.html --footer-html footer.html --margin-bottom 10mm --margin-top 10mm --margin-left 10mm --margin-right 10mm cover cover.html toc --xsl-style-sheet tocfile.xsl temp.html temp.pdf

temp.html is Extremely invalid XHTML, first line says <?xml version="1.0" encoding="iso-8859-1"?>. The temp.html file was written to disk with C# using UTF-8 and it works. I really suggest using UTF-8 wherever possible.

Screenshot of generated PDF

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • I tried your command, but it just won´t work :(( Its like the added parameters were ignored...even if I change for example margin-top value, it takes no effect. – Michal S Aug 08 '12 at 11:10
  • Hmm, I just tested again and changed my margin from 10mm to 20mm and it worked. Could you post the contents of `$commandString` and your source HTML somewhere so that I could test using exactly your materials? One thing to try would be to use wkhtmltopdf directly and not from php. If you can give the source and exact command I'll try to replicate the problem here. – Joel Peltonen Aug 08 '12 at 11:28
  • Now I tried to run it directly and flags are working, even encoding works...Any idea how could I force it to run from PHP ? I cant be running it directly because I need every user to be able to generate pdf by one click and not to download wkhtml... – Michal S Aug 08 '12 at 11:47
  • I haven't done PHP in a while so I don't have a dev server for PHP at the moment (I mainly do C#) but I can check later when I get the time but at least the encoding issue works :) There are similar issues on SO where people try to get wkhtmltopdf you might want to check out. It seems getting PHP+wkhtmltopdf to work is challenging so if I have time I might try to find out if I can do it. – Joel Peltonen Aug 08 '12 at 12:51
  • OK, I´ll be looking forward to that :) Really, since I use this program, there´s not a single step, where I wouldn´t encounter some problem :( – Michal S Aug 08 '12 at 12:57