Dompdf with special characters (UTF-8)
I’m not really into php, but sometime you just have no other option. And you know: If all you have is a hammer, everything looks like a nail.
Recently I have been struggling for few hours how to generate pdf from html, that looks like original page and show proper characters. I my case I had problem to print czech chars like ěščřžýáíé.
First I found recommendation to use Sans Deja Vu font family, which looks like s*it.
Than I finaly found not outdated howto on official github page. In short:
-
- Download and install Dompdf
- Download dompdf-utils (don’t know why its not part of Dompdf anymore ?!) and copy laod_font.php in you dompdf folder
- And finally!
php load_font.php Arial /path/to/arial.ttf
Now you should have your font installed and you can use it in you php script like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$html = '<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
</head> | |
<body> | |
<p style="font-family: Arial, sans-serif;">ěščřžýáíé</p> | |
</body> | |
</html>'; | |
require_once dirname(__FILE__) . '/Libraries/dompdf/autoload.inc.php'; //$rendererLibrary; | |
$dompdf = new \Dompdf\Dompdf(); | |
$dompdf->load_html($html, 'UTF-8'); | |
$dompdf->set_paper('A4'); | |
$dompdf->render(); | |
file_put_contents('test.pdf', $dompdf->output()); |
Uff.. Why do I feel like this?
Hmm, php is not my cup of tea.