Save and (pdf generate) in same process

Hi, has anyone ever had this need? I would like to perform two actions in the action update (update + makePdf), it’s possible?

thanks

it is possible you pass in a param to your action and based on that you can perform for instance




public function actionPerform($type)

{

   if ($type=="print')

      // print 

   else

      // else do something else

}

Not in if structure, but in a single process… I would like to save and print the model in pdf format (with the library tcpdf, I have a controller that returns the pdf)

tnx ;)

This should be something like:

public function actionUpdate($id)

{

$model = $this->loadModel($id);

// set new attributes

$model->attributes = $_POST[‘Model’];

if($model->save())

{

   // do your print here

}

$this->render(‘update’, [‘model’=>$model]);

}

thanks @dragan but my action contain only ajax request, here the code:




//.....

if($model->save())

{

        //Ajax Request

        if(Yii::app()->request->isAjaxRequest)

        {

         echo CJSON::encode(array(

         'status'=>'success',

         'div'=> Yii::t('app','save_success'),

         ));

         exit;

        }

}



… how can solved this ? Thanks

nobody is gonna help you unless you paste your code here dump you action and controller that generates your pdf

Hi @alirz23 the controller/action for generate pdf it’s in other place, I would insert action call on afterSave (ajax) event, then here the printer action:




public function actionPrint_ContrattiInScadenza($from=false, $to=false){

            // extend from beauty class TCPDF of Nicola Asuni

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

            spl_autoload_register(array('YiiBase','autoload'));


            // set document information  


            $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "Hello world", date('d/m/Y'));

            $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));

            $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

            $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

            $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);

            $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

            $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

            $pdf->SetFont('helvetica', '', <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />;

            $pdf->SetTextColor(80,80,80);

            $pdf->AddPage();


            //Write the html

            $html = "<div style='margin-bottom:15px;'>Hi folks</div>";

            

            $pdf->lastPage();


            //Close and output PDF document

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

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


        }



thanks

You cant save and print. You would have to save and open the document… then the user would have to tell the browser to print.

You could prompt the user to print when the pdf opened… you could doing something like


function printPage() {print();}

however, the user still has to physically click print.

Image if that would be possible…you would have tons of sites just printing things when you went to them.

Save is ajax style, ajax calls ended with exit(); … how can process in same time printPDF() action ? :huh:

You can try this:

  1. add the printUrl to your ajax response, with the urlparams you need.

  2. redirect to the printUrl in the ajax-success




//.....

if($model->save())

{

        //Ajax Request

        if(Yii::app()->request->isAjaxRequest)

        {

         echo CJSON::encode(array(

         'status'=>'success',

         'div'=> Yii::t('app','save_success'),

         'printUrl'=> Yii::app()->createUrl('routeToPrintUrl',array(..your params for the printUrl ...)),

         ));

         Yii::app()->end(); //use instead of exit, because otherwise nothing will be logged

        }

}








  $.ajax({

           ...

            success: function(data) {

              ... display the response message ...

             

              if(data.printUrl)

                 location.href=data.printUrl;


             }

        });




Besides:

If you work with TCPDF you can take a look at my extension pdffactory.

It’s a helper to work with TCPDF, with predefined docs and integrates FPDI to print on existing pdf documents (letter headed paper,…) too.

very interesting your extension, thanks for the reply @Joblo, I will try to days and let you know ;)

Actually the solution is more complex than usual, because I have two buttons, "Save" and "Save and Print" both of which must process the form and only after the model-> save () one of the two must also print the newly model created or modified.

So the example of printURL can only be used within an if condition … :unsure: it would seem almost impossible to do so !

Is it possible to use jsPrintSetup extension in Firefox?

You should be able to return a variable to distinguish "Save" and "Save and Print" buttons from ajax return. If the variable return the "Save and Print", you can call the jsPrintSetup to start printing.

However, I am not quite sure whether you can load the pdf to web browser and printing automatically. You might be able to do it.

I would just like to be able to generate the pdf … but I think it is really impossible :unsure: because the event should take place after a submit form

Sorry but I don’t understand. What is stopping you from redirecting the browser (via javascript) to an action that would generate the pdf for you, after saving?

Because I have 2 buttons for submit/validate form, the first for generate pdf too … in newRecord case, how pass new inserted id to action ? This is case of modal objects, the content of form return from ajax call.

tnx

Pass the id back from your ajax code?




if($model->save())

{

        //Ajax Request

        if(Yii::app()->request->isAjaxRequest)

        {

         echo CJSON::encode(array(

         'status'=>'success',

		 'id' => $model->id, // Or whatever is your pk field

         'div'=> Yii::t('app','save_success'),

         ));

         exit;

        }

}



ok and where I distinguish the case of the simple "model saves" about "save and generates pdf" ? Where I wrote the if statement ?

You have 2 buttons correct? Both do an ajax call to save your model, but only one of them has to take you to your generated PDF. Does each button have its own script to do the ajax call? If so, then just place the redirect inside the block of code for your 2nd button that should trigger the PDF part.