Generate PDF file in Yii 2. Strict alignment

I need to create a PDF file with very exacting alignment. I am looking at using fpdf.

I saw mPDf, but I really don’t want to do HTML2PDF. I need to be able to do some calculations

and line drawing directly from my controller/view.

I installed fpdf from itbs/fpdf, but I am getting errors. I tried changing the namespace to itbs\fpdf, but that has not helped.

I am getting a


Class 'itbz\fpdf' not found

error.

Here is my test code




<?php


namespace app\controllers;


use Yii;

use kartik\widgets\Alert;

use itbz\fpdf;

use yii\web\Controller;

use yii\web\Response;


class TestController extends \yii\web\Controller

{


    public function actionIndex()

    {


    }




    public function actionPdftest()

    {

        $pdf = new FPDF();

        $pdf->AddPage();

        $pdf->SetFont('Arial','B',16);

        $pdf->Cell(40,10,'Hello World!');

        $pdf->Output();


    }

}

?>




I expect this is mainly a matter of getting the connection to fpdf library sorted out…

Thanks

-John

I suggest you TCPDF

TCPDF looks good. Do you have a recommended distribution? I find several of them, but it looks like many of them might be unsupported now…

From here:

http://sourceforge.net/projects/tcpdf/files/

I am trying to use TCPDF to output PDF files to the browser.

But it is giving me the error


TCPDF ERROR: Some data has already been output, can't send PDF file

How can be sure YII2 only outputs the file I need? Something is sending out the text/html headers and spoiling the PDF transfer.

Thanks

-John

That error means that there is an output (text or php notices, errors,…) inside sending pdf.

Try to comment line that outputs pdf, usually the last, with:




// $pdf->Output(...)



So you should see the errors in the page.

Otherwise put an "exit" immediately after $pdf->Output

No Joy…

Here is my controller… just the first sample from TCPDF.





    public function actionPdftest()

    {


        // 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 001');

        $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.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));

        $pdf->setFooterData(array(0,64,0), array(0,64,128));


// 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 default font subsetting mode

        $pdf->setFontSubsetting(true);


// Set font

// dejavusans is a UTF-8 Unicode font, if you only need to

// print standard ASCII chars, you can use core fonts like

// helvetica or times to reduce file size.

        $pdf->SetFont('dejavusans', '', 14, '', true);


// Add a page

// This method has several options, check the source code documentation for more information.

        $pdf->AddPage();


// set text shadow effect

        $pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));


// Set some content to print

        $html = <<<EOD

<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>


EOD;


// Print text using writeHTMLCell()

        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);


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


// Close and output PDF document

// This method has several options, check the source code documentation for more information.

//        header("Content-type:application/pdf"); //for pdf file

        $pdf->Output('/example_001.pdf', 'I');

        exit;


    }






I am getting a blank screen with





TCPDF ERROR: Some data has already been output, can't send PDF file




put




ini_set('display_errors', true);



at top of code.

No change. I am already running in debug mode.

-John

Have you checked application log (or webserver log) ?

You sure have some errors.

I know that FPDF has similar errors if the layout is not completely blank. In other words, if your pdf content is being output inside the html of your regular main layout it won’t work. Try a dedicated layout that is only echo $content where $content = your pdf.

OK, it now seems to be working…

I added ob_end_clean(); before creating the TCPDF creation call. That wiped any and everything in the PHP output buffer.


        ob_end_clean();

        $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);



I did not have to put anything after the output call




// Close and output PDF document

// This method has several options, check the source code documentation for more information.

//        header("Content-type:application/pdf"); //for pdf file

        $pdf->Output('/example_001.pdf', 'I');

//        Yii::app()->end();


    }

}

?>



just the call to Output then close the action and (in this case), close the controller

I still don’t know what was being output, but I might be able to find out with another call to a ob_* call. If I get time I will fiddle with it and report back.

Thank you for your help everyone!

-John

Thanks for your help everyone.

Hi,

There is extension created by Karthik for PDF creation

Its very good with lots of options.I have currently implemented in my project.

Thanks,

Vishwas