Question

I am trying to create 2 separate pdf files with different content. I have the following code which creates the 2 files.

$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());
        }

The problem i have is the 1st pdf that gets created is overwritten by the content of the 2nd pdf which makes no sense.

Was it helpful?

Solution

I do not know the class you are using but this code seems suspicios:

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

For me it seems that you convert 2 files into 1 output files, so logically you overwrite it!?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top