1

I am using barryvdh's library DOMPdf to generate pdf documents from html. My goal is to make it possible for users to preview their invoices as pdf before they submit the form. I am using Ajax to pass the data from the form to the controller. The controller receives the data from the form, but it returns an encoded string which I cannot decode and display. I would like to display the invoice on the same page in a pop-up, but it could also be made into a new window. Any suggestions would be appreciated:

  1. Javascript code:

    $(document).on("click", ".viewAsPdf", function(){
        var formID = $(this).closest("form").attr("id");
        var formHTML = document.getElementById(formID).innerHTML;
        var nettoTotal = "";
        var vatTotal = "";
        var bruttoTotal ="";
        var formData = ""; 
        var url="";
    
    switch(formID){
        case "demandOfferForm":
            nettoTotal = $(formHTML).find("#demandOfferTotalNetto").text();
            vatTotal = $(formHTML).find("#demandOfferTotalVat").text();
            bruttoTotal = $(formHTML).find("#demandOfferTotalBrutto").text();
    
            formData ="nettoTotal="+nettoTotal+"&vatTotal="+vatTotal+"&bruttoTotal="+bruttoTotal;
            console.log(formData);
            url ="../buy/newOffer/pdfOffer";
            var pdf = doAjaxCall(url, formData);
    
        break;
       }
       });
    
  2. Ajax call

      function doAjaxCall(url, formData){
             $.ajax({
                type        : 'GET', 
                url: url,
                data        : formData, 
            })
            .done(function(data) {   
                 console.log(data);
                $('#demandOfferForm').fadeOut();
                $('#buyNewOffer').html(data);
    
            })
            // If the data hasn't beed received correct.
        .fail(function(data) {
            console.log(data);
        });
    }
    
  3. Controller code

    public function previewPDF(){
    $nettoTotal = Input::get("nettoTotal");
    $vatTotal = Input::get("vatTotal");
    $grossTotal = Input::get("bruttoTotal");
    $pdf = App::make('dompdf');
        $pdfContent ='';
        $pdfContent .='<DOCTYPE HTML><html>';
        $pdfContent .='<head></head><body>';
        // $pdfContent .='<head><link rel="stylesheet"   href="http://localhost/freesoft_front/public/css/pdf.css" type="text/css"></head><body>';
        $pdfContent .='<div class="pdfContent" style="position:absolute;">';
        $pdfContent .='<div class="companyInfo" style="position:absolute;">';
            $pdfContent .='<h4 style="padding:0; margin:0; text-align: right; line-height: 10px;">Faktura</h4>';
            $pdfContent .='<div class="header">';
    
                $pdfContent .='<div class="headerLeft" style="display: inline-block; width:50%; text-align: left;">';
                $pdfContent .='<div class="logoContainer" style="height: 50px; border: 1px solid blue;"><p>Company logo goes here</p></div>';
                $pdfContent .='<p>Hr. Hansen</p>';
                $pdfContent .='<p>Strømvej 2</p>';
                $pdfContent .='<br/> <p>5700 Svandeborg</p>';
                $pdfContent .='</div>';
    
                $pdfContent .='<div class="headerRight" style="display: inline-block; width:50%; text-align: right;"><p>DEMO Firma</p>';
                $pdfContent .='<p> 9560 Hadslund</p>';
                $pdfContent .='<p> Tlf: 44 55 90 17</p>';
                $pdfContent .='<p> <strong> FAKTURA NR. : 18540 </strong></p>';
                $pdfContent .='<br/> <p class="smallInput" style="font-size: 15px; line-height: 5px;">Dato: 15-09-2014</p>';
                $pdfContent .='<p class="smallInput"  style="font-size: 15px; line-height: 5px;">Forfaldsdato: 23-09-2014</p>';
                $pdfContent .='</div>';
    
                $pdfContent .='</div>';
                    // ------------end of header 
                $pdfContent .= '<div class="paymentDetails">';
                $pdfContent .='<div class="detailsLeft" style="display: inline-block; width:50%; text-align: left;">';
                $pdfContent .='<p class="smallInput"  style="font-size: 15px; line-height: 5px;"> Kundenr.: 358</p><br/>';
                $pdfContent .='<p class="smallInput"  style="font-size: 15px; line-height: 5px;"> Betailngsbet.: 8 dage</p>';
                $pdfContent .='<p class="smallInput"  style="font-size: 15px; line-height: 5px;"> Rentebet.: 1,3% rente pr. påb. måned</p>';
                $pdfContent .='</div>';
    
                $pdfContent .='<div class="detailsRight" style="bottom:0px; display: inline-block; width:50%; text-align: right;">';
                $pdfContent .='<p class="smallInput" style="font-size: 15px; line-height: 5px;"> Bank</p>';
                $pdfContent .='<p class="smallInput" style="font-size: 15px; line-height: 5px;">Kontonr.: 9999 0001234567</p>';
                $pdfContent .='</div>';
                $pdfContent .= '</div>';
                // ------------end of paymentDetails 
                $pdfContent .='<div class="invoiceLines">';
                    $pdfContent .='<div class="linesHeading" style="border-top:1px solid black; border-bottom: 1px solid black;">';
                        $pdfContent .='<p style="line-height:5px;">Tekst  <span style="margin:0px 0px 0px 350px;">Antal</span> <span style="margin:0px 0px 0px 75px;"> A pris</span> <span style="margin:0px 0px 0px 75px;">Nettobeløb</span></p>';
                    $pdfContent .='</div>';
    
                        // foreach ($lines as $index => $line) {
                        //  $pdfContent .='<p class="line"><span style="display: inline-block; width:55%;">'.$line ->  ItemName_Line .'</span>  <span style="display: inline-block; width:15%;">'. $line -> Quantity_Line .'</span> <span style="display: inline-block; width:17%;">'.$line -> Price_Line.'</span> <span style="display:inline-block; width:10%; text-align:right;">'.$line -> Netto_Total_Line.'</span></p>';
                        //  }
    
                $pdfContent .='</div>';
            // --------------end of invoice lines
                $pdfContent .='<div style="border-top:1px solid black; border-bottom: 1px solid black; position:absolute; bottom:0;">';
                    $pdfContent .='<p style="line-height:8px;"> <span style="display:inline-block;width:41%;"> Nettobeløb </span> <span style="display:inline-block; width:43.5%;"> Momsbeløb </span> <span style="text-align:right; display:inline-block; width:14%;"> Totalbeløb</span></p>';
                    $pdfContent .='<p style="line-height:2px;"> <span style="display:inline-block;width:41.5%;"> '.$nettoTotal.' </span> <span style="display:inline-block; width:43%;"> '.$vatTotal.' </span> <strong> <span style="text-align:right; display:inline-block; width:13.5%;">'.$grossTotal.'</span></strong></p>';
                $pdfContent .='</div>';
        $pdfContent .='</div>';
    $pdfContent .='</div>';
    $pdfContent .='</html></body>';
    
    
    $pdf->loadHTML($pdfContent);
    
    return $pdf->stream();
    }
    
    1. Output snippet:

    stream x����N�@��y����J�t�v�%E�R)�+�')ű�m�����'������9�����gc0�V��I�{F�c�T�d�e��d���?.һ��J?�@r��l��w��w2Z ��9\��C6� ��` G'#�Yҡ��>�U�e�k �/��������y�)aS�~����%' Rf&qV!��E��9Z�"�US=o��X�\��-���1-��*�͐U^Q�ek'B�v��',n�m:���s�M���a�cV�E6F���V(��x�|}��੥vDK��0��&6����r�K�c�N+c��!c����?Gѥ�6%����#���J-�P�R��(�u�gu�S2�ҵLH��ED.�h����vR���"�Ih�ۼ�ԫ�� K7����O��E�4�,?�Ơ5����?

  • The encoded string is the actual PDF document. While it's [technically possible to do this via ajax](http://stackoverflow.com/a/23797348/264628), the easier method is to use window.open to preview the document in a separate pop-up window. If you really want to do this via some kind of modal dialog you could set the content of the modal to be an iframe and set the location of the iframe to the appropriate URL. – BrianS Nov 28 '14 at 20:43
  • You can ignore the PDF portion of the implementation. Where you should focus is on how to display a binary to the client. There are a lot of good answers for that question. See [ajax + pdf](http://stackoverflow.com/search?q=ajax+pdf) and [ajax + binary](http://stackoverflow.com/search?q=ajax+binary) – BrianS Nov 28 '14 at 20:47

0 Answers0