ttmind

Main Navigation

ttmind
  • jim-jams
  • Tech
  • Positive
Login

Login

Facebook Google

OR

Remember me Forgot password?

Don't have account? Signup here.

Sort by Categorys

.Net

PHP

Java

JavaScript

Database

Server

Client Side

Tools

Artificial Intelligence

Cloud

Hybrid Development

Event

Smart City

Education

Security

Scrum

Digital Marketing

APP Development

Business

Internet

Simulation

Art

Network

Microservices

Architecture

Technology

Leadership

    Top Articles

  • How Does Social Media Bring People Together?
    TTMind Author
  • How to read appSettings JSON from Class Library in ASP.NET Core
    Anil Shrestha
  • Printing Support In Asp.Net Core
    TTMind Author
  • HOW TO EXTRACT TEXT FROM IMAGE USING JAVASCRIPT (OCR with Tesseract.js)?
    Prakash Pokhrel
  • Images Upload REST API using ASP.NET Core
    Prakash Pokhrel
  • Related Topic

  • How to read appSettings JSON from Class Library in ASP.NET Core
  • Printing Support In Asp.Net Core
  • Images Upload REST API using ASP.NET Core
  • How to use IActionFilter, IAsyncActionFilter in ASP.NET Core MVC?
  • ASP.NET CORE - Blazor CRUD operation using ADO.NET
  • Tech
  • About Us
  • Contact Us
  • TechHelp
  • PositiveHelp
  • Jim-Jams Help
  • Terms & Conditions

© Copyright ttmind.com

Main Content

How to return View as PDF in .Net core ?

.Net .Net Core about 7 years ago || 2/4/2019 || 4.4 K View

  • Hide

Portable Document Format(PDF) as the name refers is a file format which presents documents in a manner independent of software , hardware and OS.

PDF format is very important for printing the document and information.

We can display the content in our webpage in a pdf format which makes it easier to print rather than displaying it in an usual normal web page.

Suppose We have a condition where we need to print the invoice or bill to the user from our system then there is a simple way for doing it in .net core. It will be easier to print the invoice or bill if it is first converted into PDF format.

So, below we have the steps for returning the view in a PDF format in .net core:

  • First create a new .net core web application
  • Then go to tools, click on nugget packet manager and install package- Rotativa which is compatible to the version of your project

  • Once the package Rotativa is installed, go to startup of the project and  add the following code inside Configure in Startup
 RotativaConfiguration.Setup(env);
  • We need an additional file to convert html to pdf. You can dowload the files from the linnk given below and add the file to wwwroot in your project inside Rotativa folder:

File Location: Github Location

  • Now on your Home Controller add a new Action and return the View in a PDF format like below:
public IActionResult Index()
        {
            return new  ViewAsPdf();
        }
  • Now add the view named Index in our case , which will be displayed as the PDF :
@{ ViewData["Title"] = "Home Page"; }

<html>

<head>
    <meta charset="utf-8">
    <title>Demo</title>
    <link rel="stylesheet" href="style.css">
    <link rel="license" href="https://www.opensource.org/licenses/mit-license/">
    <script src="script.js"></script>
    <link href="~/css/demo.css" rel="stylesheet" />
</head>

<body>
    </div>
    <h1>Invoice</h1>
    <table class="inventory" border="1">
        <thead>
            <tr>
                <th>
                    <span contenteditable>Item</span>
                </th>
                <th>
                    <span contenteditable>Description</span>
                </th>
                <th>
                    <span contenteditable>Rate</span>
                </th>
                <th>
                    <span contenteditable>Quantity</span>
                </th>
                <th>
                    <span contenteditable>Price</span>
                </th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <a class="cut">-</a>
                    <span contenteditable>Table</span>
                </td>
                <td>
                    <span contenteditable>Experience Review</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span contenteditable>150.00</span>
                </td>
                <td>
                    <span contenteditable>4</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span>600.00</span>
                </td>
            </tr>
            <tr>
                <td>
                    <a class="cut">-</a>
                    <span contenteditable>Chair</span>
                </td>
                <td>
                    <span contenteditable>Round</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span contenteditable>100.00</span>
                </td>
                <td>
                    <span contenteditable>4</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span>400.00</span>
                </td>
            </tr>
            <tr>
                <td>
                    <a class="cut">-</a>
                    <span contenteditable>Bed</span>
                </td>
                <td>
                    <span contenteditable>Wide Long</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span contenteditable>15000.00</span>
                </td>
                <td>
                    <span contenteditable>4</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span>60000.00</span>
                </td>
            </tr>
            <tr>
                <td>
                    <a class="cut">-</a>
                    <span contenteditable>Daraz</span>
                </td>
                <td>
                    <span contenteditable>Woods</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span contenteditable>10000.00</span>
                </td>
                <td>
                    <span contenteditable>4</span>
                </td>
                <td>
                    <span data-prefix>र </span>
                    <span>40000.00</span>
                </td>
            </tr>
        </tbody>
    </table>
    <aside>
        <h1>
		<span contenteditable>Additional Notes</span>
		</h1>
        <div contenteditable>
            <p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
        </div>
    </aside>
</body>

</html>
  • Now run the project and you will get the output and the view in PDF as given below:

So now the page is displayed in PDF format and can be easily printed or downloaded.

Also Read: Code Faster in Visual Studio with Code Snippet

  • 0
  • 0
  • 0
    • Facebook
    • Twitter
    • Google +
    • LinkedIn

About author

Binod  Bhandari

Binod Bhandari

Life is not just about thrieving but all about living.

Reset Your Password
Enter your email address that you used to register. We'll send you an email with your username and a link to reset your password.

Quick Survey