RK.
RK.

Reputation: 981

TCPDF does not render external CSS

I am trying to generate PDFs using TCPDF library. So far, I am able to generate the PDF files using PHP code. Although it does not render external CSS and result becomes useless as without CSS the document is not readable.

Following is my code in the controller:

 public function generate_pdf() {

        $this->load->model('rosters', 'roster');

        $data = array();

        $data['start_date'] = isset($_POST['fortnightStartDate']) ?
            date("Y-m-d", strtotime(getFortnightStartDate($_POST['fortnightStartDate']))) :
            getFortnightStartDate();
        $cid = $_POST['cid'];
        $wno = $_POST['wno'];


        $data['no']=$wno;

        $startDate = strtotime($data['start_date']);

        $data['schedule'] = $this->roster->getShifts($cid, $data);

        $data['startDate'] = $startDate;


        $listing = $this->load->view('roster/roster_pdf', $data, true);


        /**
 * Creates an example PDF TEST document using TCPDF
 * @package com.tecnick.tcpdf
 * @abstract TCPDF - Example: XHTML + CSS
 * @author Nicola Asuni
 * @since 2010-05-25
 */

// Include the main TCPDF library (search for installation path).
require_once(APPPATH.'libraries/tcpdf/tcpdf.php');

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

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 061', 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(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, 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 (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

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

// set font
$pdf->SetFont('helvetica', '', 10);

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


$pdf->writeHTML($listing, true, false, true, false, '');

//Close and output PDF document
$pdf->Output('TEST.pdf', 'F');  

    }

I am passing the $listing variable which holds the data that needs to be included in the PDF. The data is printing fine but without CSS. If I try to include the external CSS it pops up the error.

Code to include external css

$listing. = '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';

I try to include this line of code below the $listing variable but it generates following error:

A PHP Error was encountered

Severity: Warning

Message: array_merge(): Argument #2 is not an array

Filename: tcpdf/tcpdf.php

Line Number: 16375

Is there any other way I can load the external CSS ?? Any help will be highly appreciated.

Upvotes: 2

Views: 6210

Answers (1)

RK.
RK.

Reputation: 981

After researching here and there, I come to know that TCPDF library has few limitations including CSS limitation and does not support many CSS properties. Then, I looked into dompdf library and found it useful. Surprisingly, it solved all my problem with almost similar code and also it is way easy to use than TCPDF. Following is my piece of code i wrote to solve my problem (The code is similar until the $listing variable then i simply change the library code.)

 $listing .= '<style>'.file_get_contents(base_url("styles/style.css")).'</style>';


        require_once(APPPATH.'libraries/dompdf/dompdf_config.inc.php');

        $dompdf = new DOMPDF();
        $dompdf->load_html($listing);
        $dompdf->set_paper('a4', 'landscape');
        $dompdf->render();

        file_put_contents('my_pdf_test.pdf', $dompdf->output());
        //$dompdf->stream("dompdf_out.pdf", array("Attachment" => true));
        exit(0);

Upvotes: 4

Related Questions