Results 1 to 2 of 2

Thread: How to handle function return values from server

  1. #1
    Sencha User
    Join Date
    Oct 2011
    Posts
    5
    Vote Rating
    0
      0  

    Default Unanswered: How to handle function return values from server

    Hi,

    I have ExtJS application that is querying database and showing the results in grid which is working properly.

    Users can select any record from the grid by checkbox then send to server to print out. The server side code generate PDF file ( by TCPDF ) and return result the browser, it also works properly.

    My problem is, client side doesn't show PDF file in browser window, instead of view just showing binary form of the PDF page.

    So, could you please help me, how can I handle returning PDF file to show client screen?

    console.png



    CLIENT SIDE

    Code:
                            xtype: 'button',
                            width: 90,
                            text: 'GNDER',
                            cls: 'x-btn-gonder',
                            handler: function(){
                                var ppt     = Ext.getCmp('labelType').getValue();
                                var sb      = Ext.getCmp('basic-statusbar');
                                var count   = Ext.getCmp('labelGrids').getSelectionModel().getCount();
                                var rows    = Ext.getCmp('labelGrids').getSelectionModel().getSelection();
    
                                if(ppt == null) {
                                    dialog.show();
                                } else {
                                if(count > 0) {
                                    var paper = {};
                                    paper.PAPER = ppt;
                                    
                                    var prints = new Array();
                                    for (var i = 0; i < count; i++)
                                    {
                                        prints[i] = {'LABEL_ID': rows[i].data.LABEL_ID, 'LABEL_TYPE':rows[i].data.LABEL_TYPE}
                                    }
                                    paper.LABELS = prints;
                                    
                                    Ext.Ajax.request({
                                        url: 'lib/labels/print_label.php',
                                        timeout: 60000,
                                        headers: {
                                            'Content-type': 'application/json',
                                            'Accept': 'application/pdf'
                                        },
                                        
                                        success: function() 
                                        { 
                                            Ext.Msg.alert('??lem Ba?ar?l?', 'Etiketler yaz?c?ya gnderildi.');
    
                                            // set statusbar text after print
                                            sb.setStatus({
                                                text: 'Etiketler yaz?c?ya gnderildi..!',
                                                iconCls: 'x-status-saved',
                                                clear: true
                                            });
                                            
                                            // remove checked items
                                            Ext.select('.x-grid-row-selected').each(function (element) {
                                                Ext.getCmp('labelGrids').getSelectionModel().deselectAll();
                                            });
                                        },
                                        failure: function() { Ext.Msg.alert('Yazd?rma Hatas?', 'Etiketler yazd?r?lamad?..!')},
                                        jsonData: Ext.JSON.encode(paper)
                                    });
                                    
                                    // clear combobox selected value after send to printer
                                    Ext.getCmp('labelType').reset();
                                    
                                    // console.log(Ext.JSON.encode(prints));
                                } else if(count == 0) {
                                    sb.setStatus({
                                        iconCls: 'x-status-error',
                                        text: 'Ltfen yazd?rmak istedi?iniz etiketleri seiniz!'
                                    });
                                }                            
                                winPaper.hide();
                                }
                            }
    SERVER SIDE

    PHP Code:
    <?php
        
    require_once('../tcpdf/config/lang/eng.php');
        require_once(
    '../tcpdf/tcpdf.php');

        
    $db = new mysqli("10.10.10.10","user","pass","label");
        
    $db->query("SET NAMES UTF8");
        
    $db->query("SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO'");
        
    $db->query("SET lc_time_names = 'tr_TR'");
        
        
    $datas json_decode(file_get_contents("php://input"));

        
    // get paper type from json array
        
    $paper  $datas->PAPER;
        
        foreach(
    $datas->LABELS as $data)
        {
            
    $lblids[] = $data->LABEL_ID;
        }
        
        
    $ids implode(","$lblids);
        
        
    // get labels from db
        
    $sql "SELECT LABEL_ID, SUBSYS_ART_NO, ARTICLE_DESC, END_DATE, PRODUCT_PRICE, SHELF_PRICE, LABEL_TEXT, LABEL_TYPE, LABEL_SIGN, PROMO FROM labels WHERE LABEL_ID IN (".$ids.")";
        
    $result $db->query($sql);
        
        while(
    $row $result->fetch_assoc())
        {
            
    $labels[] = $row;
        }

        switch(
    $paper)
        {
            case 
    "SF":
                
    print_shelf($labels);
            break;
        }
        
        function 
    print_shelf($labels)
        {
            
    # defining PDF variables
            
    $width      100;
            
    $height     55;
            
    $pageSize   = array($width$height);
            
            
    # create new BMPL PDF price label
            
    $pdf = new TCPDF('L'PDF_UNIT$pageSizetrue'UTF-8'false);
            
            
    # set PDF document information
            
    $pdf->SetCreator(PDF_CREATOR);
            
    $pdf->SetAuthor('O?uz elikdemir, Metro Systems Turkey');
            
    $pdf->SetTitle('BMPL Price Label');
            
            
    # disable pdf document header and footer
            
    $pdf->setPrintHeader(false);
            
    $pdf->setPrintFooter(false);
            
            
    # set PDF default font
            
    $pdf->setDefaultMonospacedFont('PDF_FONT_MONOSPACED');
            
            
    # set auto page breaks
            
    $pdf->SetAutoPageBreak(true2);
            
            
    $crs   = array('width' => 0.5);
            
    $style = array('width' => 0.25'cap' => 'butt''join' => 'miter''dash' => 0'color' => array(000));
            
            for(
    $i 0$i count($labels); $i++)
            {
                
    # add PDF page
                
    $pdf->addPage();
                
                
    # split the price values
                
    $prdPrice explode("."$labels[$i]['PRODUCT_PRICE']);
                
    $shfPrice explode("."$labels[$i]['SHELF_PRICE']);
                
                
    $prcLeft  $prdPrice[0];
                
    $prcRight $prdPrice[1];
                
    $shfLeft  $shfPrice[0];
                
    $shfRight $shfPrice[1];
                
                
    # Label variables
                
    $priceLeft      '<span style="color:#b4b4b4;">'.$prcLeft.'</span>';
                
    $priceLeftCent  '<span style="color:#b4b4b4;">'.$prcRight.'</span';
                
    $priceRight     '<span style="color:#b4b4b4;">'.$shfLeft.'</span>';
                
    $priceRightCent '<span style="color:#000;">'.$shfRight.'</span>';
                
    $taxLabelLeft   '<span style="color:#b4b4b4;">KDV\'L?</span>';
                
    $taxLabelRight  'KDV\'L?';
                
    $articleDesc    '\''.$labels[$i]['ARTICLE_DESC'].'\'';
                
    $articleNumber  $labels[$i]['SUBSYS_ART_NO'];
                
    $labelText      '\''.$labels[$i]['LABEL_TEXT'].'\'';
                
    $promoDate      $labels[$i]['END_DATE'];
                
    $promoLabel     $labels[$i]['PROMO'];
                
                
    ////////////// PREPARE THE LABELS \\\\\\\\\\\\\\
                
                # LABEL BOTTOM
                
    $pdf->SetFont('dinpro'''6);
                
    $pdf->writeHTMLCell(200,  550$articleNumber''10true'L');
                
    $pdf->writeHTMLCell(2002850$promoDate''10true'L');
                
                if(
    is_null($promoLabel)) {
                    
    $pdf->writeHTMLCell(2004250''''10true'R');
                } else {
                    
    $pdf->writeHTMLCell(2004250'FIRSAT'''10true'R');
                }
                
                
    # LABEL LEFT
                
    $pdf->SetFont('dinprob''B'10);
                
    $pdf->MultiCell(9020$articleDesc0'L'false155.50);
                
    $pdf->SetFont('dinprob'''8);
                
    $pdf->writeHTMLCell(4510517'SATI? F?YATI'''10true'F');
                
    $pdf->SetFont('dinprob''B'44);
                
    $pdf->writeHTMLCell(3027524$priceLeft''10true'R'true);
                
    $pdf->SetFont('dinprob''B'20);
                
    $pdf->writeHTMLCell(151026.528.5$priceLeftCent''10true'R'true);
                
    $pdf->SetFont('dinpro'''6);
                
    $pdf->writeHTMLCell(151029.237.3$taxLabelLeft''10'L'true);
                
                if(!
    is_null($labels[$i]['LABEL_SIGN']))
                {
                    
    $pdf->Line(7413822$crs);
                }
                
                
    # LABEL RIGHT
                
    $pdf->SetFont('dinprob','',8);
                
    $pdf->writeHTMLCell(45105017$labelText''10true'C');
                
    $pdf->SetFont('dinprob''B'58);
                
    $pdf->writeHTMLCell(45304221$priceRight''10true'R'true);
                
    $pdf->SetFont('dinprob''B'24);
                
    $pdf->writeHTMLCell(151079.528$priceRightCent''10true'R'true);
                
    $pdf->SetFont('dinpro'''6);
                
    $pdf->writeHTMLCell(15108138.5$taxLabelRight''10'L'true);
                
                
    $js .= 'print(true);';
                
    $pdf->IncludeJS($js);
                
                
    ob_end_clean();
                
    header('Content-Type: application/pdf');
                return 
    $pdf->Output('bpml_label.pdf''I');
                
            }
        }
    ?>

  2. #2
    Sencha User friend's Avatar
    Join Date
    Apr 2011
    Posts
    895
    Answers
    106
    Vote Rating
    27
      0  

    Default

    You need to perform a standard submit to issue the request for your PDF. Use a FormPanel with standardSubmit: true and url: 'lib/labels/print_label.php'.

    To submit, do this:

    Code:
    form.submit({
      method: 'POST',
      target: 'reportWindow'
    });

    Server side, you may also need to set the Content-Disposition header as well:

    Code:
     Content-Disposition: attachment; filename=someFile.pdf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •