8

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 : https://i.stack.imgur.com/HOyMO.png

how can I fix this?

  • "output is broken" ..are you talking about a broken RTL output? – sofl Nov 13 '13 at 12:34
  • 4
    domdpf does not currently support RTL. You can hack the library to get pseudo-support ([see here](https://github.com/dompdf/dompdf/issues/426)). You might also try either [TCPDF](http://www.tcpdf.org) or [mPDF](http://www.mpdf1.com), both of which appear to have at least some level of RTL support. – BrianS Nov 13 '13 at 17:11
  • is this what you are looking for? http://i.imgur.com/UBdkNDx.png if so, i can give you your solution i think... – frymaster Jul 29 '15 at 23:39
  • I think persian is written right to left. Dompdf does not allow that yet – Ritesh Ksheersagar Aug 01 '15 at 12:36
  • I think this will help you - http://stackoverflow.com/questions/21201257/arabic-fonts-display-in-reverse-order-in-dompdf – Ritesh Ksheersagar Aug 01 '15 at 12:39

1 Answers1

0

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

https://i.stack.imgur.com/HggNU.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.

frymaster
  • 664
  • 7
  • 12