Set PDF page size in Rotativa plugin in ASP.Net Core

jmontano
 
on Jul 20, 2022 10:54 PM
438 Views

I am developing a system in asp.net core and rotativa pdf, in this I need to print sales tickets.

How can I establish in a pdf rotativa that the size of the page is of the ticket type?

I use printing from a pdf rotativa press on ticket printers, those used in supermarkets to print the sales ticket.

In printing I find, among other sizes, such as letter or legal but there is no way to tell that the size is a ticket type that has a width but not a height.

Do you know how to indicate the type of ticket in rotativa pdf?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jul 29, 2022 12:16 AM

Hi jmontano,

You need to set options to the rendering engine. You have to set PageWidth and PageHeight and Margins in millimeters.

Check the example.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

Model

public class Customer
{
    public string CustomerId { get; set; }
    public string ContactName { get; set; }
    public string Country { get; set; }
}

Namespaces

using Rotativa.AspNetCore;

Controller

public class HomeController : Controller
{
    private DBCtx Context { get; }
    public HomeController(DBCtx _context)
    {
        this.Context = _context;
    }

    public IActionResult Index()
    {
        return View(this.Context.Customers.ToList());
    }

    public IActionResult Export()
    {
        return new ViewAsPdf("Index", this.Context.Customers.ToList())
        {
            FileName = "Customers.pdf",
            PageMargins = { Left = 0, Right = 0 }, // In millimeters.
            PageWidth = 80, // In millimeters.
            PageHeight = 200
        };
    }
}

View

@model List<EF_Core_MVC.Models.Customer>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <form method="post" asp-action="Export" asp-controller="Home">
        <input type="submit" value="Submit" />
    </form>
    <hr />
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Id</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (var customer in Model)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.ContactName</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
</body>
</html>

Screenshot