Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DomPDF output is broken when I use persian text

Tags:

php

dompdf

I'm using DomPDF and PHP to create PDF Files. When the text is English everything is ok, but when I want to convert Persian text, output is broken

this is the example file that contains Persian and English text:

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
    body {
        font-family: 'dejavu sans';
direction;rtl;
    }
    p {
        font-size: 2em;
        background: #eee;
        padding: 1em;
    }

    h2 {
        color: #999;
    }
</style>
<style type="text/css"></style></head>
<body marginwidth="0" marginheight="0">
<div style="text-align:right">
<h2>Give You Glory</h2>
<br/>
Hadi
</div>
<br/>
هادی
</body></html>

this is output PDF file : http://i.stack.imgur.com/HOyMO.png

how can I fix this?

like image 245
user2987413 Avatar asked Nov 13 '13 11:11

user2987413


1 Answers

okay, i think i have a solution to your problem. i can make a pdf that looks like what i think you're looking for. here's a screenshot of it

http://i.imgur.com/UBdkNDx.png

to do this, you have to use a different way of making pdfs than dompdf: wkhtmltox-php.

wkhtmltox-php is a custom php command compiled from source that uses the libwkhtmltox to make pdfs. installing it takes a bit of effort, but it will render your persian text as above and will be much faster than dompdf.

these instructions assume linux or similar as your os:

first: install wkhtmltopdf. there are pre-compiled binaries for most operating systems here:

http://wkhtmltopdf.org/downloads.html

second: get and compile and install php-wkhtmltox.

cd /tmp/
wget https://github.com/mreiferson/php-wkhtmltox/archive/master.zip
unzip master.zip
cd php-wkhtmltox-master/
phpize
./configure
sudo make install

note: if you do not have phpize installed on your machine, you will need to install your php dev packages. note: if you get errors on configure or make install you will need to install c compilation tools like 'make' and 'gcc'

by reading the output of make install you will know what directory the module is located in. usually it's:

 /usr/lib64/php/modules/

third: set php to know about this module in your php.ini file, add the following line under the section heading "Dynamic Extensions"

extension=phpwkhtmltox.so

fourth: run ldconfig

$ ldconfig

fifth: restart apache (or whatever httpd you are using)

finally: use it like so: for my example here, i'm just using a chess opening page from wikipedia since i don't have an url to your sample html.

<?php

    /**
     * the config_array  has lots of options but the two most important are:
     * "out" this is the full path to where you want your pdf made
     * "imageQuality" basically the same as jpg image quality. lower quality is slower, higher quality is a bigger file
     */
    $config_array = array(  "out" => "/tmp/pdfdocument.pdf",
                            "imageQuality" => 95);

    /**
     * the array of urls that are the input html for making your pdf. note that these are not files, but urls
     * also note that this is an array of arrays keyed by "page"
     */
    $htmls_array = array(array("page"=>"http://en.wikipedia.org/wiki/Queen's_Gambit_Declined"));

    /**
     * run the conver like so and your pdf file should be on disk
     */
    wkhtmltox_convert('pdf', $config_array, $htmls_array);

?>

if you look at the screenshot i posted above, it looks like phpwkhtmltox does the job right.

like image 166
frymaster Avatar answered Sep 29 '22 01:09

frymaster