Menu

Justified HTML text gets a mystery margin

Help
2014-11-21
2014-12-09
  • Jonne Mikkanen

    Jonne Mikkanen - 2014-11-21

    I've been having some issues getting HTML content aligned properly with text-align set to "justify". For some reason, TCPDF seems to add a margin to the right side of the text block.

    See example in the link below - page margins are colored grey and the text area blue for clarity. See how there's a gap between the text area and the right margin of the page? I'd like to get rid of that and make the text reach the page's right margin.
    Justified text-image

    I've tried making the text area a span instead of p, forcing "margin:0 !important;" in it's style etc, nothing has worked yet. The same happens if I set margins manually for the page instead of using setBooklet.

    Any hints would be greatly appreciated. Thanks in advance!

    Code for the example page, if it helps:

    class MYPDF extends TCPDF {
        public function Header() {
            // Coloring the margins for clarity:
            $this->setColor("fill", 0, 0, 0, 50);
            $this->Rect(0, 0, 148, 20, 'F');
            $this->Rect(0, 185, 148, 25, 'F');
            $this->Rect(128, 0, 20, 210, 'F');
            $this->Rect(0, 0, 17, 210, 'F');
        }
        public function Footer() {}
    }
    $pdf = new MYPDF('P', 'mm', 'A5', 1, 'UTF-8');
    $pdf->SetBooklet(true, 20, 17);
    $pdf->setColor("text", 0, 0, 0, 100);
    $pdf->setColor("fill", 100, 0, 0, 0);
    
    $pdf->AddPage('P', 'A5');
    $pdf->SetTopMargin(21);
    $pdf->SetAutoPageBreak(1, 25);
    
    // Title:
    $pdf->SetFont('crimsontext', 'B', 12, '', false);
    $pdf->writeHTML("Testing justified text", 1, 0, 0, 0,"L");
    $pdf->ln(5);
    
    // Page text:
    $text = 'Jukolan talo, eteläisessä Hämeessä, seisoo erään mäen pohjoisella rinteellä, liki Toukolan kylää. Sen läheisin ympäristö on kivinen tanner,
            mutta alempana alkaa pellot, joissa, ennenkuin talo oli häviöön mennyt, aaltoili teräinen vilja. Peltojen alla on niittu, apilaäyräinen, halkileikkaama monipolvisen ojan; ja
            runsaasti antoi se heiniä, ennenkuin joutui laitumeksi kylän karjalle. Muutoin on talolla avaria metsiä, soita ja erämaita, jotka, tämän tilustan ensimmäisen perustajan oivallisen
            toiminnan kautta, olivat langenneet sille osaksi jo ison jaon käydessä entisinä aikoina. Silloinpa Jukolan isäntä, pitäen enemmän huolta jälkeentulevainsa edusta kuin omasta
            parhaastansa, otti vastaan osaksensa kulon polttaman metsän ja sai sillä keinolla seitsemän vertaa enemmän kuin toiset naapurinsa. Mutta kaikki kulovalkean jäljet olivat jo
            kadonneet hänen piiristänsä ja tuuhea metsä kasvanut sijaan. Ja tämä on niiden seitsemän veljen koto, joiden elämänvaiheita tässä nyt käyn kertoilemaan.';
    
    // Justify:
    $text = '<p style="text-align:justify;">'.$text.'</p>';
    
    $pdf->SetFont('crimsontext', '', 12, '', false);
    $pdf->writeHTML($text, '', 1);  
    $pdf->Output();
    
     

    Last edit: Jonne Mikkanen 2014-11-21
  • Jonne Mikkanen

    Jonne Mikkanen - 2014-12-09

    Thank you so much! Of course it had to be something simple that I managed to miss. Kind of odd that it automatically adds that padding, but hey, now I know. Problem solved!

     

    Last edit: Jonne Mikkanen 2014-12-09

Log in to post a comment.