在yii中创建单独的pdf文件

时间:2014-03-18 11:27:07

标签: php yii ghostscript wkhtmltopdf

我正在尝试创建具有不同内容的2个单独的pdf文件。我有以下代码创建2个文件。

$invoice = Invoices::model()->findByPk($_GET['id']);
        $invoicedetail = InvoicesDetail::model()->findAll(array("condition"=>"invoiceno=".$invoice->INVOICENO . " && biditems_id is null"));
        $client = $invoice->pROJ->oRDERNO->addressesinvoices;
        $invoices = Invoices::model()->findAll(array("condition"=>"PROJID=".$invoice->PROJID . " && INVOICENO != ".$invoice->INVOICENO));

        /*$this->renderPartial("partialInvoice",
                array(
                        "invoice"=>$invoice,
                        "client"=>$client,
                        //"lookupprocesstotalArr"=>$lookupprocesstotalArr,
                        "adjustments"=>$invoices,
                        "invoicedetail"=>$invoicedetail[0]
                ),true);
        */
        $html = $this->renderPartial("partialInvoice",
                array(
                        "invoice"=>$invoice,
                        "client"=>$client,
                        //"lookupprocesstotalArr"=>$lookupprocesstotalArr,
                        "adjustments"=>$invoices,
                        "invoicedetail"=>$invoicedetail[0]),true);

        $filename_invoice = 'invoice_'.$invoice->INVOICENO.'.pdf';

        Yii::import('application.extensions.pdfable.WkHtmlToPdf');

        $pdf = new WkHtmlToPdf(array(
                'no-outline',         // Make Chrome not complain
                'margin-top'    => 10,
                'margin-right'  => 10,
                'margin-bottom' => 10,
                'margin-left'   => 10,
        ));

        $pdf->setOptions(array(
                'orientation' => 'portrait'
        ));

        // Add a HTML file, a HTML string or a page from a URL
        $pdf->addPage($html);

        // Save the PDF
        $pdf->saveAs("/tmp/$filename_invoice");

        // ... or send to client as file download
        /*if(!$pdf->send($filename)){
            throw new Exception('Could not create PDF: '.$pdf->getError());
        }*/





        $globalController = new GlobalController;
        //$project = Projects::model()->findAll(array("condition"=>"PROJID=".$_GET['PROJID']));
        //$projid = $_GET['PROJID'];
        $pdfDetail = new WkHtmlToPdf();
        $orientation = "Landscape";
        $pdfDetail->setOptions(array('orientation'=>$orientation,
                'no-outline',         // Make Chrome not complain
                'margin-right'  => 10,
                'margin-bottom' => 5,
                'margin-left'   => 10,
        ));

        //$this->layout = 'pdf';

        //get list of process stages for this project
        $lastrun = $globalController->lastRun($invoice->PROJID);
        $processstages = ExtHtml::listData($lastrun, 'ID',array('cumulative','ITEM'));

        $lookupprocesstotalArr = $globalController->lookupprocesstotal($invoice->PROJID,$processstages);

        // get all lines
        $connection = Yii::app()->db;
        $sql = "SELECT jobs.JOBNO, jobs.NAME, FSP, LSP, SHOTS, KM
                FROM hdb.jobs
                join detailsseismic on jobs.JOBNO = detailsseismic.JOBNO
                where jobs.PROJID = :pid";
        $command=$connection->createCommand($sql);
        $command->bindValue(":pid",$invoice->PROJID,PDO::PARAM_INT);
        $command->execute();
        $lines = $command->queryAll();

        $dates = array();
        foreach ($lines as $arr) {
            $sql = "SELECT jobsprocesscomplete.datedone, ITEM_fk
                    FROM hdb.jobsprocesscomplete
                    left join lookupprocess on jobsprocesscomplete.lookupprocess_id = lookupprocess.ID
                    left join biditems on lookupprocess.biditems_id = biditems.ITEMID
                    where jobno = :jobno";
            $command=$connection->createCommand($sql);
            $command->bindValue(":jobno",$arr['JOBNO'],PDO::PARAM_INT);
            $command->execute();
            $dates[$arr['JOBNO']] = $command->queryAll();
        }

        /*echo $this->renderPartial('pdf_'.$_GET['type'],array(
         'lookupprocesstotalArr'=>$lookupprocesstotalArr,
                "currency"=>$project[0]->currency0->currency,
                "projectpercent"=>$project[0]->PERCENT,
                "name"=>$project[0]->PROJECT . " - " . $project[0]->oRDERNO->cONTACTsugar->first_name . " " . $project[0]->oRDERNO->cONTACTsugar->last_name,
                "lines"=>$lines,
                "dates"=>$dates,
                "project"=>$project[0]));
        */
        $detail = $this->renderPartial('/lookupprocess/pdf_detail',array(
                'lookupprocesstotalArr'=>$lookupprocesstotalArr,
                "currency"=>$invoice->pROJ->currency0->currency,
                "projectpercent"=>$invoice->pROJ->PERCENT,
                "name"=>$invoice->pROJ->PROJECT . " - " . $invoice->pROJ->oRDERNO->cONTACTsugar->first_name . " " . $invoice->pROJ->oRDERNO->cONTACTsugar->last_name,
                "lines"=>$lines,
                "dates"=>$dates,
                "project"=>$invoice->pROJ
        ),true);


        $filename_detail = $invoice->PROJID . "_" . date("ymd-his") . ".pdf";

        // Add a HTML file, a HTML string or a page from a URL
        $pdfDetail->addPage($detail);

        // Save the PDF
        $pdfDetail->saveAs("/tmp/$filename_detail");


        shell_exec("gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail");
        echo "gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail";

        exit();
            /*
        // ... or send to client as file download
        if(!$pdf->send($filename_invoice)){
            throw new Exception('Could not create PDF: '.$pdf->getError());
        }

我遇到的问题是第一个创建的pdf被第二个pdf的内容覆盖,这是没有意义的。

1 个答案:

答案 0 :(得分:1)

我不知道你正在使用的课程,但这段代码似乎很可疑:

-sOutputFile=/tmp/$filename_invoice /tmp/$filename_invoice /tmp/$filename_detail

对我来说,似乎你将2个文件转换为1个输出文件,所以逻辑上你要覆盖它!?