0

I'm using Laravel 5 to generate a form for a warehouse. In Main form you can select which items to get and it should generate a PDF with the items (all info), people to get the items, date and invoice number.

All the information is on a DIV called 'invoice'. How can I send this object to a new view to generates the PDF. I read about 'invoking dompdf via web' to make it more interactive but the documentation is not clear enough or doesn't fit with I'm needing. This is my current code: From: enter image description here Recibo.js

 $('#createPDF').click(function()
        {
            $htmlData = "data";
            $_token = $('[name="_token"]').val();
            $.get('recibos/pdf',{ html:$htmlData, _token: $_token })
               .done(function(data)
               {
                   console.log('Done PDF!'); 
               });
        });

ReciboController.php

public function reciboPDF(Request $request)
    {
        $data = $request->get('html');
        $pdf = \PDF::loadView('create.template.formReciboPDF',compact('data'))->setPaper('letter')->setOrientation('landscape');
        return $pdf->stream();
        //return view('create.template.formReciboPDF');
    }

formReciboPDF.blade.php

<tr>
<td>{{$data}}</td>
<td>Papeleria</td>
<td>Unidad</td>
<td>Grande</td>
<td>La Palma</td>
<td>2342423424234</td>
<td>Bueno</td>
<td>9</td>
</tr>

1 Answers1

1

Instead of trying to capture the HTML can you send the selection parameters to the server and rebuid the page? Then you would just need to feed the generated HTML into dompdf and render. If visual appearance is important you could use hidden checkboxes as the selection mechanism.

If you want to use a shortcut you can capture the HTML using jQuery.contents().

In your second part it looks like you're trying to send your request via AJAX and get back the PDF. While this is technically possible it's more trouble than it's worth. An easier alternative would be to submit a hidden form to a blank window. It would require some minor changes to your HTML/JS but little else, e.g.

$('#createPDF').click(
   function() {
     $('#html').val($('#invoice').content());
     $(this).closest('form').submit();
   });
<form method="POST" action="recibos/pdf" target="_blank">
  <input type="hidden" name="html" id="html" value="" />
  <input type="hidden" name="_token" value="{value from server}" />
  <button type="button" id="createPDF">Create PDF</button>
</form>
Community
  • 1
  • 1
BrianS
  • 13,284
  • 15
  • 62
  • 125