TCPDF - How can I show a div with absolute position in a pdf created with tcpdf?

Stimart picture Stimart · Jan 23, 2015 · Viewed 8.5k times · Source

I have following tcpdf code:

    <?php
    require_once('config/lang/eng.php');
    require_once('tcpdf.php');

    // Extend the TCPDF class to create custom Header and Footer
    class MYPDF extends TCPDF {

      //Page header
      public function Header() {
      }

      // Page footer
      public function Footer() {
      }
    }

    // create new PDF document
    $pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

    // set document information
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Stimart');
    $pdf->SetTitle('test');
    $pdf->SetSubject('test');
    $pdf->SetKeywords('test');

    // set default header data
    $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);

    // set header and footer fonts
    $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

    // set default monospaced font
    $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

    //set margins
    $pdf->SetMargins(40, 40, PDF_MARGIN_RIGHT);
    $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

    //set auto page breaks
    $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

    //set image scale factor
    $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

    //set some language-dependent strings
    $pdf->setLanguageArray($l);

    // ---------------------------------------------------------

    // set font
    $pdf->SetFont('times', '', 12);

    // add a page
    $pdf->AddPage();

    // set some text to print
    $html = '<div style="position: absolute; top:200px; left:300px;">Hi, I am an ABSOLUTE div.</div>';

    $pdf->writeHTML($html, true, false, false, false, '');
    // ---------------------------------------------------------
    $pdf->lastPage();
    //Close and output PDF document
    $pdf->Output('test.pdf', 'I');

    //============================================================+
    // END OF FILE                                                
    //============================================================+

after the line "// set some text to print" I set up the html code to show but it doesn't works as expected. In this case I need to pass a div with absolute position but when the code runs it creates the pdf page showing the div in relative position.

How can I solve this annoying problem??

Answer

Raju Rajotia Jangid picture Raju Rajotia Jangid · Mar 16, 2016

TCPDF has a very limited CSS support. It doesn't support all attributes.

Currently, only the following CSS attributes are supported:

  • font-family
  • font-size
  • font-weight
  • font-style
  • color
  • background-color
  • text-decoration
  • width
  • height
  • text-align

So try removing the other attributes, and tell if it works.