Laravel - Generate a PDF from a form , including also the user's signature












1















I have a big chalenge for my level.



The title is self explanatory : Generate a PDF from a form , including also the user's signature, using Laravel.



My laravel and general php level is basic. I know how to make a basic blog on laravel (like all guys who have followed laracasts or similar) but this one I find it tough cause I see there are many concepts from different places going on at the same time and I have many gaps of knowledge.



It would be at least very helpful to know which packages are the best ones to do all this. I have been trying with DOMPDF and https://github.com/szimek/signature_pad. But I have read that with DOMPDF I can't use active forms ... I have also checked MPDF.



What I would mainly need is a view , where there is a form , where there is a signature pad for the user to sign after the form , and this, after submitting generates a pdf.



Any starting points on what to read, study, focus ?



Already how to mix the image of the signature, which the script is on js, with the data of the form seems like the everest to me.



What I can't neither imagine is how to deal with the different controllers ..










share|improve this question



























    1















    I have a big chalenge for my level.



    The title is self explanatory : Generate a PDF from a form , including also the user's signature, using Laravel.



    My laravel and general php level is basic. I know how to make a basic blog on laravel (like all guys who have followed laracasts or similar) but this one I find it tough cause I see there are many concepts from different places going on at the same time and I have many gaps of knowledge.



    It would be at least very helpful to know which packages are the best ones to do all this. I have been trying with DOMPDF and https://github.com/szimek/signature_pad. But I have read that with DOMPDF I can't use active forms ... I have also checked MPDF.



    What I would mainly need is a view , where there is a form , where there is a signature pad for the user to sign after the form , and this, after submitting generates a pdf.



    Any starting points on what to read, study, focus ?



    Already how to mix the image of the signature, which the script is on js, with the data of the form seems like the everest to me.



    What I can't neither imagine is how to deal with the different controllers ..










    share|improve this question

























      1












      1








      1








      I have a big chalenge for my level.



      The title is self explanatory : Generate a PDF from a form , including also the user's signature, using Laravel.



      My laravel and general php level is basic. I know how to make a basic blog on laravel (like all guys who have followed laracasts or similar) but this one I find it tough cause I see there are many concepts from different places going on at the same time and I have many gaps of knowledge.



      It would be at least very helpful to know which packages are the best ones to do all this. I have been trying with DOMPDF and https://github.com/szimek/signature_pad. But I have read that with DOMPDF I can't use active forms ... I have also checked MPDF.



      What I would mainly need is a view , where there is a form , where there is a signature pad for the user to sign after the form , and this, after submitting generates a pdf.



      Any starting points on what to read, study, focus ?



      Already how to mix the image of the signature, which the script is on js, with the data of the form seems like the everest to me.



      What I can't neither imagine is how to deal with the different controllers ..










      share|improve this question














      I have a big chalenge for my level.



      The title is self explanatory : Generate a PDF from a form , including also the user's signature, using Laravel.



      My laravel and general php level is basic. I know how to make a basic blog on laravel (like all guys who have followed laracasts or similar) but this one I find it tough cause I see there are many concepts from different places going on at the same time and I have many gaps of knowledge.



      It would be at least very helpful to know which packages are the best ones to do all this. I have been trying with DOMPDF and https://github.com/szimek/signature_pad. But I have read that with DOMPDF I can't use active forms ... I have also checked MPDF.



      What I would mainly need is a view , where there is a form , where there is a signature pad for the user to sign after the form , and this, after submitting generates a pdf.



      Any starting points on what to read, study, focus ?



      Already how to mix the image of the signature, which the script is on js, with the data of the form seems like the everest to me.



      What I can't neither imagine is how to deal with the different controllers ..







      laravel forms dompdf mpdf signaturepad






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 10:32









      tomatoes17tomatoes17

      285




      285
























          1 Answer
          1






          active

          oldest

          votes


















          2














          Hats off for accepting that you lack the knowledge to do this on your own. It is no shame to ask and I guess it will make your life a lot easier after all as well.



          My advice is to split the whole task into suitable sub-tasks that you are able to handle. In my opinion that are:





          1. Create an absolutely normal HTML form that you can submit.

            You'll need one controller with two functions for this. One function will display the form and the other one will handle the submitted data once the form gets submitted.



            class MyFormController
            {
            public function getForm()
            {
            return view('my.form');
            }

            public function postForm(Request $request)
            {
            // do something with the form data in $request
            }
            }


            I guess you know how to put together a proper HTML form.




          2. Generate the PDF with the submitted form data.

            The way most pdf generator packages of Laravel work is by using a blade template view. For you that is actually quite beneficial, because you already know how to put together an HTML view!



            So if we translate this into code based on the barryvdh/laravel-dompdf package, we get something like this:



            public function postForm(Request $request)
            {
            $pdf = PDF::loadView('my.pdf', [
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            // ... and more data if you like
            ]);

            return $pdf->download('signed_form.pdf');
            }


            Of course we will also need a blade template. According to the name it will have the path and file name resources/views/my/pdf.blade.php:



            <table>
            <tr>
            <th>Name:</th>
            <td>{{ $name }}</td>
            </tr>
            <tr>
            <th>Address:</th>
            <td>{{ $address }}</td>
            </tr>
            </table>



          3. Now, as we do have the form and PDF generation itself, we can go on to the signature.

            To be fair, I've never done anything similar myself so far. But after a very quick search I came across the JavaScript package szimek/signature_pad, which looks incredibly promising. I think the setup is quite self explanatory, but here is what I understood so far:




            • Add the script tag (<script src="...">) given in the documentation to your form view.

            • Add a hidden input field to your form (<input type="hidden" name="sig" id="sig">).

            • Add a <canvas> HTML element to your form view. Give it an identifier: <canvas id="signature">.


            • Add another script tag but with custom JavaScript code below the one you already added. Something like this:



              <script type="text/javascript">
              var canvas = document.getElementById('signature');
              var signaturePad = new SignaturePad(canvas);

              function onFormSubmit()
              {
              document.getElementById('sig').value = signaturePad.toDataURL();
              }
              </script>


            • You'll now also have to add onsubmit="onFormSubmit()" to your <form ...> tag. What this script does is to first initialize the signature pad but then also to set the value of the hidden input field to the contents of the signature pad.


            • Last thing you'll want to do is to add the submitted sig value to the view that is used to generate the pdf. And in the view itself you'll have to use the submitted sig data in a new image element: <img src="{{ $sig }}">




          And if you are lucky, all of what I just wrote straight out of my mind (without any testing anything of it) works right out of the box. If not, you should at least have an idea what the steps are to achieve your goal.



          By the way, of course you can make all the views look fancy. But be aware that PDFs cannot display as much information as HTML can. So you will definitely lose information and styling at some points when you convert a view to pdf.






          share|improve this answer
























          • Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

            – tomatoes17
            Nov 23 '18 at 13:21













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445009%2flaravel-generate-a-pdf-from-a-form-including-also-the-users-signature%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Hats off for accepting that you lack the knowledge to do this on your own. It is no shame to ask and I guess it will make your life a lot easier after all as well.



          My advice is to split the whole task into suitable sub-tasks that you are able to handle. In my opinion that are:





          1. Create an absolutely normal HTML form that you can submit.

            You'll need one controller with two functions for this. One function will display the form and the other one will handle the submitted data once the form gets submitted.



            class MyFormController
            {
            public function getForm()
            {
            return view('my.form');
            }

            public function postForm(Request $request)
            {
            // do something with the form data in $request
            }
            }


            I guess you know how to put together a proper HTML form.




          2. Generate the PDF with the submitted form data.

            The way most pdf generator packages of Laravel work is by using a blade template view. For you that is actually quite beneficial, because you already know how to put together an HTML view!



            So if we translate this into code based on the barryvdh/laravel-dompdf package, we get something like this:



            public function postForm(Request $request)
            {
            $pdf = PDF::loadView('my.pdf', [
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            // ... and more data if you like
            ]);

            return $pdf->download('signed_form.pdf');
            }


            Of course we will also need a blade template. According to the name it will have the path and file name resources/views/my/pdf.blade.php:



            <table>
            <tr>
            <th>Name:</th>
            <td>{{ $name }}</td>
            </tr>
            <tr>
            <th>Address:</th>
            <td>{{ $address }}</td>
            </tr>
            </table>



          3. Now, as we do have the form and PDF generation itself, we can go on to the signature.

            To be fair, I've never done anything similar myself so far. But after a very quick search I came across the JavaScript package szimek/signature_pad, which looks incredibly promising. I think the setup is quite self explanatory, but here is what I understood so far:




            • Add the script tag (<script src="...">) given in the documentation to your form view.

            • Add a hidden input field to your form (<input type="hidden" name="sig" id="sig">).

            • Add a <canvas> HTML element to your form view. Give it an identifier: <canvas id="signature">.


            • Add another script tag but with custom JavaScript code below the one you already added. Something like this:



              <script type="text/javascript">
              var canvas = document.getElementById('signature');
              var signaturePad = new SignaturePad(canvas);

              function onFormSubmit()
              {
              document.getElementById('sig').value = signaturePad.toDataURL();
              }
              </script>


            • You'll now also have to add onsubmit="onFormSubmit()" to your <form ...> tag. What this script does is to first initialize the signature pad but then also to set the value of the hidden input field to the contents of the signature pad.


            • Last thing you'll want to do is to add the submitted sig value to the view that is used to generate the pdf. And in the view itself you'll have to use the submitted sig data in a new image element: <img src="{{ $sig }}">




          And if you are lucky, all of what I just wrote straight out of my mind (without any testing anything of it) works right out of the box. If not, you should at least have an idea what the steps are to achieve your goal.



          By the way, of course you can make all the views look fancy. But be aware that PDFs cannot display as much information as HTML can. So you will definitely lose information and styling at some points when you convert a view to pdf.






          share|improve this answer
























          • Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

            – tomatoes17
            Nov 23 '18 at 13:21


















          2














          Hats off for accepting that you lack the knowledge to do this on your own. It is no shame to ask and I guess it will make your life a lot easier after all as well.



          My advice is to split the whole task into suitable sub-tasks that you are able to handle. In my opinion that are:





          1. Create an absolutely normal HTML form that you can submit.

            You'll need one controller with two functions for this. One function will display the form and the other one will handle the submitted data once the form gets submitted.



            class MyFormController
            {
            public function getForm()
            {
            return view('my.form');
            }

            public function postForm(Request $request)
            {
            // do something with the form data in $request
            }
            }


            I guess you know how to put together a proper HTML form.




          2. Generate the PDF with the submitted form data.

            The way most pdf generator packages of Laravel work is by using a blade template view. For you that is actually quite beneficial, because you already know how to put together an HTML view!



            So if we translate this into code based on the barryvdh/laravel-dompdf package, we get something like this:



            public function postForm(Request $request)
            {
            $pdf = PDF::loadView('my.pdf', [
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            // ... and more data if you like
            ]);

            return $pdf->download('signed_form.pdf');
            }


            Of course we will also need a blade template. According to the name it will have the path and file name resources/views/my/pdf.blade.php:



            <table>
            <tr>
            <th>Name:</th>
            <td>{{ $name }}</td>
            </tr>
            <tr>
            <th>Address:</th>
            <td>{{ $address }}</td>
            </tr>
            </table>



          3. Now, as we do have the form and PDF generation itself, we can go on to the signature.

            To be fair, I've never done anything similar myself so far. But after a very quick search I came across the JavaScript package szimek/signature_pad, which looks incredibly promising. I think the setup is quite self explanatory, but here is what I understood so far:




            • Add the script tag (<script src="...">) given in the documentation to your form view.

            • Add a hidden input field to your form (<input type="hidden" name="sig" id="sig">).

            • Add a <canvas> HTML element to your form view. Give it an identifier: <canvas id="signature">.


            • Add another script tag but with custom JavaScript code below the one you already added. Something like this:



              <script type="text/javascript">
              var canvas = document.getElementById('signature');
              var signaturePad = new SignaturePad(canvas);

              function onFormSubmit()
              {
              document.getElementById('sig').value = signaturePad.toDataURL();
              }
              </script>


            • You'll now also have to add onsubmit="onFormSubmit()" to your <form ...> tag. What this script does is to first initialize the signature pad but then also to set the value of the hidden input field to the contents of the signature pad.


            • Last thing you'll want to do is to add the submitted sig value to the view that is used to generate the pdf. And in the view itself you'll have to use the submitted sig data in a new image element: <img src="{{ $sig }}">




          And if you are lucky, all of what I just wrote straight out of my mind (without any testing anything of it) works right out of the box. If not, you should at least have an idea what the steps are to achieve your goal.



          By the way, of course you can make all the views look fancy. But be aware that PDFs cannot display as much information as HTML can. So you will definitely lose information and styling at some points when you convert a view to pdf.






          share|improve this answer
























          • Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

            – tomatoes17
            Nov 23 '18 at 13:21
















          2












          2








          2







          Hats off for accepting that you lack the knowledge to do this on your own. It is no shame to ask and I guess it will make your life a lot easier after all as well.



          My advice is to split the whole task into suitable sub-tasks that you are able to handle. In my opinion that are:





          1. Create an absolutely normal HTML form that you can submit.

            You'll need one controller with two functions for this. One function will display the form and the other one will handle the submitted data once the form gets submitted.



            class MyFormController
            {
            public function getForm()
            {
            return view('my.form');
            }

            public function postForm(Request $request)
            {
            // do something with the form data in $request
            }
            }


            I guess you know how to put together a proper HTML form.




          2. Generate the PDF with the submitted form data.

            The way most pdf generator packages of Laravel work is by using a blade template view. For you that is actually quite beneficial, because you already know how to put together an HTML view!



            So if we translate this into code based on the barryvdh/laravel-dompdf package, we get something like this:



            public function postForm(Request $request)
            {
            $pdf = PDF::loadView('my.pdf', [
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            // ... and more data if you like
            ]);

            return $pdf->download('signed_form.pdf');
            }


            Of course we will also need a blade template. According to the name it will have the path and file name resources/views/my/pdf.blade.php:



            <table>
            <tr>
            <th>Name:</th>
            <td>{{ $name }}</td>
            </tr>
            <tr>
            <th>Address:</th>
            <td>{{ $address }}</td>
            </tr>
            </table>



          3. Now, as we do have the form and PDF generation itself, we can go on to the signature.

            To be fair, I've never done anything similar myself so far. But after a very quick search I came across the JavaScript package szimek/signature_pad, which looks incredibly promising. I think the setup is quite self explanatory, but here is what I understood so far:




            • Add the script tag (<script src="...">) given in the documentation to your form view.

            • Add a hidden input field to your form (<input type="hidden" name="sig" id="sig">).

            • Add a <canvas> HTML element to your form view. Give it an identifier: <canvas id="signature">.


            • Add another script tag but with custom JavaScript code below the one you already added. Something like this:



              <script type="text/javascript">
              var canvas = document.getElementById('signature');
              var signaturePad = new SignaturePad(canvas);

              function onFormSubmit()
              {
              document.getElementById('sig').value = signaturePad.toDataURL();
              }
              </script>


            • You'll now also have to add onsubmit="onFormSubmit()" to your <form ...> tag. What this script does is to first initialize the signature pad but then also to set the value of the hidden input field to the contents of the signature pad.


            • Last thing you'll want to do is to add the submitted sig value to the view that is used to generate the pdf. And in the view itself you'll have to use the submitted sig data in a new image element: <img src="{{ $sig }}">




          And if you are lucky, all of what I just wrote straight out of my mind (without any testing anything of it) works right out of the box. If not, you should at least have an idea what the steps are to achieve your goal.



          By the way, of course you can make all the views look fancy. But be aware that PDFs cannot display as much information as HTML can. So you will definitely lose information and styling at some points when you convert a view to pdf.






          share|improve this answer













          Hats off for accepting that you lack the knowledge to do this on your own. It is no shame to ask and I guess it will make your life a lot easier after all as well.



          My advice is to split the whole task into suitable sub-tasks that you are able to handle. In my opinion that are:





          1. Create an absolutely normal HTML form that you can submit.

            You'll need one controller with two functions for this. One function will display the form and the other one will handle the submitted data once the form gets submitted.



            class MyFormController
            {
            public function getForm()
            {
            return view('my.form');
            }

            public function postForm(Request $request)
            {
            // do something with the form data in $request
            }
            }


            I guess you know how to put together a proper HTML form.




          2. Generate the PDF with the submitted form data.

            The way most pdf generator packages of Laravel work is by using a blade template view. For you that is actually quite beneficial, because you already know how to put together an HTML view!



            So if we translate this into code based on the barryvdh/laravel-dompdf package, we get something like this:



            public function postForm(Request $request)
            {
            $pdf = PDF::loadView('my.pdf', [
            'name' => $request->input('name'),
            'address' => $request->input('address'),
            // ... and more data if you like
            ]);

            return $pdf->download('signed_form.pdf');
            }


            Of course we will also need a blade template. According to the name it will have the path and file name resources/views/my/pdf.blade.php:



            <table>
            <tr>
            <th>Name:</th>
            <td>{{ $name }}</td>
            </tr>
            <tr>
            <th>Address:</th>
            <td>{{ $address }}</td>
            </tr>
            </table>



          3. Now, as we do have the form and PDF generation itself, we can go on to the signature.

            To be fair, I've never done anything similar myself so far. But after a very quick search I came across the JavaScript package szimek/signature_pad, which looks incredibly promising. I think the setup is quite self explanatory, but here is what I understood so far:




            • Add the script tag (<script src="...">) given in the documentation to your form view.

            • Add a hidden input field to your form (<input type="hidden" name="sig" id="sig">).

            • Add a <canvas> HTML element to your form view. Give it an identifier: <canvas id="signature">.


            • Add another script tag but with custom JavaScript code below the one you already added. Something like this:



              <script type="text/javascript">
              var canvas = document.getElementById('signature');
              var signaturePad = new SignaturePad(canvas);

              function onFormSubmit()
              {
              document.getElementById('sig').value = signaturePad.toDataURL();
              }
              </script>


            • You'll now also have to add onsubmit="onFormSubmit()" to your <form ...> tag. What this script does is to first initialize the signature pad but then also to set the value of the hidden input field to the contents of the signature pad.


            • Last thing you'll want to do is to add the submitted sig value to the view that is used to generate the pdf. And in the view itself you'll have to use the submitted sig data in a new image element: <img src="{{ $sig }}">




          And if you are lucky, all of what I just wrote straight out of my mind (without any testing anything of it) works right out of the box. If not, you should at least have an idea what the steps are to achieve your goal.



          By the way, of course you can make all the views look fancy. But be aware that PDFs cannot display as much information as HTML can. So you will definitely lose information and styling at some points when you convert a view to pdf.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 11:05









          NamoshekNamoshek

          2,9812819




          2,9812819













          • Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

            – tomatoes17
            Nov 23 '18 at 13:21





















          • Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

            – tomatoes17
            Nov 23 '18 at 13:21



















          Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

          – tomatoes17
          Nov 23 '18 at 13:21







          Hats off for your time and help :) I'm getting into it right away. You helped me already a lot on how to structure the solution. Cheers !

          – tomatoes17
          Nov 23 '18 at 13:21




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445009%2flaravel-generate-a-pdf-from-a-form-including-also-the-users-signature%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'