Exporting Html to PDF Using MVC .NET CORE

1 Answer 73 Views
Form General Discussions PDFViewer
Charlston
Top achievements
Rank 1
Charlston asked on 05 Dec 2023, 06:15 PM

I have a HTML form that has a body with data already populated in it. I want to export that data on a button click to then download that html to pdf. The form already has a method of post. I am stuck on how to get the Telerik Document process to hook up with it once the button is click.

 

It looks like this

index.cshtml

<body>

<form id ="letterForm" method="post">

.... "Data and text here"

</form>

<button type="submit"> Download</button>

</body>

 

 

Currently I dont have a controller method to sync up with yet. Looking for example on how to get it to work

1 Answer, 1 is accepted

Sort by
0
Mihaela
Telerik team
answered on 08 Dec 2023, 10:10 AM

Hello Charlston,

You can trigger an AJAX request when the "Download" button is clicked and send the HTML content of the form to the server. For example:

//View
<form id="letterForm" method="post">
    <!-- form controls -->
</form>

<button type="submit" id="generatePdfBtn">Download</button>

<script>
    $(document).ready(function () {
        $('#generatePdfButton').on("click", function () {
            var formHtml = $('#letterForm').html(); // Get the HTML content of the form
            $.ajax({
                type: 'POST',
                url: '/Home/DownloadPdf',
                data: { formContent: formHtml },
                success: function () {
                   ...
                }
            });
        });
    });
</script>

//Controller
[HttpPost]
public IActionResult DownloadPdf(string formContent)
{
  //use DPL to generate the PDF file based on the received content
  ...
}

Regarding the PDF file generation, I would recommend the following Document Processing related articles:

I hope that helps.

Regards,
Mihaela
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Charlston
Top achievements
Rank 1
commented on 08 Dec 2023, 12:10 PM

Thank you that works 
Tags
Form General Discussions PDFViewer
Asked by
Charlston
Top achievements
Rank 1
Answers by
Mihaela
Telerik team
Share this question
or