PDF or Word creation documents with Yii?

本小妞迷上赌 提交于 2019-12-05 21:00:02
ivantxo

UPDATE 1: At the moment I got PDFs working. This is how I did it: First I downloaded TCPdf from its site and open it in Yii as a 3rd-party library. Then:

Controller: protected/controllers/mycontroller.php
public function actionGeneratePdf() {
    Yii::import('application.vendors.*');
    require_once('tcpdf/tcpdf.php');
    require_once('tcpdf/config/lang/eng.php');
    $pdf = new TCPDF();
    $pdf->SetCreator(PDF_CREATOR);
    $pdf->SetAuthor('Nicola Asuni');
    $pdf->SetTitle('TCPDF Example 001');
    $pdf->SetSubject('TCPDF Tutorial');
    $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
    $pdf->SetHeaderData('', 0, PDF_HEADER_TITLE, '');
    $pdf->setHeaderFont(Array('helvetica', '', 8));
    $pdf->setFooterFont(Array('helvetica', '', 6));
    $pdf->SetMargins(15, 18, 15);
    $pdf->SetHeaderMargin(5);
    $pdf->SetFooterMargin(10);
    $pdf->SetAutoPageBreak(TRUE, 0);
    $pdf->SetFont('dejavusans', '', 7);
    $pdf->AddPage();
    $pdf->writeHTML("<span>Hello World!</span>", true, false, true, false, '');
    $pdf->LastPage();
    $pdf->Output("example_002.pdf", "I");
}

View: Wherever you want to place a trigger to your controller:
echo CHtml::link('Generate PDF', array('mycontroller/generatePdf'));

Anyway I want to be able to generate a word document as the requirement says the report is going to be edited by the user after generation.

UPDATE 2: For the Word document's report generation this is what I am doing.

akb

Extensions are available in Yii to generate PDF documents. tcpdf (http://www.yiiframework.com/extension/tcpdf/) for example..

Check this article on a general roundup of options available for PDF and Excel http://www.yiiframework.com/wiki/74/

However, if you need to create word documents, then you can try the following

Write a extension in Yii to generate word document (please see this link which shows how to do it in PHP/Linux - Create Word Document using PHP in Linux)

To create word document you can use phpword library And to use, extract the library to folder protected\extensions\PHPWord In this folder after extract you will have folders: Examples, PHPWord and one file: PHPWord.php . In your controller/code you need to call like in this example:

    spl_autoload_unregister(array('YiiBase','autoload'));
    Yii::import('ext.phpword.phpword', true);
    $PHPWord = new PHPWord();
    spl_autoload_register(array('YiiBase','autoload'));
    $document = $PHPWord->loadTemplate($path);
    $document->setValue('Value1', 'Sun');
    ....
    $document->save('path\file.docx');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!