DataGrid How to display all of the records when printing?

Hi,
I am new to your components so please be nice. I am working on a Blazor Server project. One of the requirements is to display records on a popup and then print them as a PDF document. If the records are too many to fit on one page, the PDF saves only as a single page. I mean only the couple of records that fit on the first-page display on the print preview. How can I make the PDF display all of the records? Is it due to CSS?

Modal grid:

@page "/dialogcard/{Order}"
     @using IMS.CoreBusiness
     @using IMS.UseCases.Interfaces.OrderDetail
     @using System.Globalization
     @inject IViewOrderDetailsByOrderIdUseCase ViewOrderDetailsByOrderIdUseCase
     @inject DialogService DialogService
     @inject IJSRuntime JsRuntime
        
     <style>
         .rz-dialog-wrapper {
             left:0;
         }
        
          .rz-dialog-content {
              
             margin-top: -55px;
         }
            
     </style>
        
     @if (orderDetails != null)
     {
         <div id="printarea1">
             <div class="row my-4">
                 <div class="col-md-12">
        
                     <RadzenCard>
                         <h3 class="h5">
                             Order Date: @Order.OrderDateTime 
                                
                         </h3>
                         <RadzenTabs>
                             <Tabs>
        
                                 @{
                                     var detailVendorId = 0;
                                 }
                                 @foreach (var detail in orderDetails)
                                 {
                                     @if (detailVendorId != detail.VendorId)
                                     {
                                         <RadzenTabsItem Text="@detail.Vendor.Name">
        
                                             <div class="row">
                                                 <div class="col-lg-6 d-flex">
                                                     <RadzenCard Style="width: 100%; overflow: hidden;">
                                                         <h3 class="h5">Contact</h3>
                                                         <div class="d-flex flex-row">
                                                             <div>
                                                                 <div>Company</div>
                                                                 <b>Vorlance</b>
                                                                 <div class="mt-3">Responsible</div>
                                                                 <b>@detail.Order.DoneBy</b>
                                                                 <div class="mt-3">Vendor</div>
                                                                 <b>@detail?.Vendor.Name</b>
                                                             </div>
                                                         </div>
                                                     </RadzenCard>
                                                 </div>
                                                 <div class="col-lg-6 d-flex">
                                                     <RadzenCard Style="width: 100%; overflow: hidden;">
                                                         <h3 class="h5">Delivery Information</h3>
                                                         <div class="row">
                                                             <div class="col">
                                                                 <div>Company Name</div>
                                                                 @switch (detail.Warehouse)
                                                                 {
                                                                     case "Shenzhen Warehouse":
                                                                         <b>@string.Format(new CultureInfo("en-US"), "test")</b>
                                                                         <div>Address</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "test")</b>
                                                                         <div>Contact Name</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "test")</b>
                                                                         <div>Email</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "test")</b>
                                                                         <div>Phone</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "+1233455")</b>
                                                                         break;
                                                                     case "USA Warehouse":
                                                                         <b>@string.Format(new CultureInfo("en-US"), "Company Name: TRADING LLC")</b>
                                                                         <div>Address</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "1243445,Coconut Creek, FL, ZIP Code: 33073")</b>
                                                                         <div>Contact Name</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "Test")</b>
                                                                         <div>Email</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "gulcin@test.com")</b>
                                                                         <div>Phone</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "+123242422")</b>
                                                                         break;
                                                                     case "Private Shipping":
                                                                         <b>@string.Format(new CultureInfo("en-US"), "ELEKTRONIK SANAYI VE TICARET LTD. STI.")</b>
                                                                         <div>Address</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "test")</b>
                                                                         <div>Contact Name</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "Test")</b>
                                                                         <div>Email</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "s@test.com")</b>
                                                                         <div>Phone</div>
                                                                         <b>@string.Format(new CultureInfo("en-US"), "111122")</b>
                                                                         break;
                                                                 }
                                                                @* <div>Address</div>
                                                                 <b>@(detail?.Warehouse)</b>
                                                                 *@
                                                             </div>
                                                         </div>
                                                     </RadzenCard>
                                                 </div>
                                             </div>
        
        
                                             <RadzenDataGrid AllowFiltering="false" AllowPaging="false" AllowSorting="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                                                 Data="@orderDetails.Where(x => x.VendorId == detail.VendorId)" TItem="OrderDetail" Class="mt-3" Style="height:190px">
                                                 <Columns>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="OrderId" Title="Order ID" Width="100px"/>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="Id" Title="Product ID" Width="100px"/>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="ProductCode" Title="Product Code" Width="150px"/>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="Currency" Title="Currency" Width="95px"/>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" Width="95px">
                                                         <FooterTemplate>
                                                             <b>@string.Format(new CultureInfo("tr-TR"), "{0:G}", orderDetails?.Where(x => x.VendorId == detail.VendorId).Sum(o => o.Quantity))</b>
                                                         </FooterTemplate>
                                                     </RadzenDataGridColumn>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="BuyUnitPrice" Title="Unit Price" Width="100px">
                                                     <Template Context="detail">
                                                         @switch (detail.Currency)
                                                         {
                                                             case "USD":
                                                                 @string.Format(new CultureInfo("en-US"), "{0}{1:0.#####}",new CultureInfo("en-US").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
                                                                 break;
                                                             case "EURO":
                                                                 @string.Format(new CultureInfo("en-FR"), "{0}{1:0.#####}",new CultureInfo("en-FR").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
                                                                 break;
                                                             default:
                                                                 @string.Format(new CultureInfo("tr-TR"), "{0}{1:0.#####}",new CultureInfo("tr-TR").NumberFormat.CurrencySymbol, detail.BuyUnitPrice)
                                                                 break;
                                                         }
                                                     </Template>
                                                     </RadzenDataGridColumn>
                                                     <RadzenDataGridColumn TItem="OrderDetail" Property="TotalBuyPrice" Title="Total Price" Width="100px">
                                                         <Template Context="detail">
                                                             @switch (detail.Currency)
                                                             {
                                                                 case "USD":
                                                                     @string.Format(new CultureInfo("en-US"), "{0}{1:0.#####}",new CultureInfo("en-US").NumberFormat.CurrencySymbol, detail.TotalBuyPrice)
                                                                     break;
                                                                 case "EURO":
                                                                     @string.Format(new CultureInfo("en-FR"), "{0}{1:0.#####}",new CultureInfo("en-FR").NumberFormat.CurrencySymbol, detail.TotalBuyPrice)
                                                                     break;
                                                                 default:
                                                                     @string.Format(new CultureInfo("tr-TR"), "{0}{1:0.#####}",new CultureInfo("tr-TR").NumberFormat.CurrencySymbol, detail.TotalBuyPrice)
                                                                     break;
                                                             }
                                                         </Template>
                                                         <FooterTemplate>
                                                             @switch (detail.Currency)
                                                             {
                                                                 case "USD":
                                                                     <b>@string.Format(new CultureInfo("en-US"), "{0:N2}", orderDetails?.Where(x => x.VendorId == detail.VendorId).Sum(o => o.TotalBuyPrice))</b>
                                                                     break;
                                                                 case "EURO":
                                                                     <b>@string.Format(new CultureInfo("en-FR"), "{0:N2}", orderDetails?.Where(x => x.VendorId == detail.VendorId).Sum(o => o.TotalBuyPrice))</b>
                                                                     break;
                                                                 default:
                                                                     <b>@string.Format(new CultureInfo("tr-TR"), "{0:N2}", orderDetails?.Where(x => x.VendorId == detail.VendorId).Sum(o => o.TotalBuyPrice))</b>
                                                                     break;
                                                             }
        
                                                         </FooterTemplate>
                                                     </RadzenDataGridColumn>
        
                                                 </Columns>
                                             </RadzenDataGrid>
        
                                         </RadzenTabsItem>
                                     }
                                     detailVendorId = detail.VendorId;
                                 }
        
                             </Tabs>
                         </RadzenTabs>
                     </RadzenCard>
                 </div>
        
             </div>
         </div>
     }
        
        
     <div class="row">
         <div class="col-md-12 text-right">
             <RadzenButton Click="@((args) => DialogService.Close(false))" ButtonStyle="ButtonStyle.Secondary" Text="Close" Style="width: 120px" Class="mr-1"/>
             <RadzenButton Click="PrintDocument" Text="Print" Style="width: 120px" />
             @*<button type="button" class="btn btn-primary btns" @onclick="PrintDocument">Print</button>*@
         </div>
     </div>
        
     @code {
         [Parameter] public ReportViewModel Order { get; set; }
         IEnumerable<OrderDetail> orderDetails;
            
         protected override async Task OnInitializedAsync()
         {
             orderDetails = await ViewOrderDetailsByOrderIdUseCase.ExecuteAsync(Order.OrderId);
                
         }
         private void PrintDocument()
         {
             JsRuntime.InvokeVoidAsync("print");
         }
     }

screenshot of the modal:

print preview:

CSS:

@media print {
     body * {
         visibility: hidden;
     }
    
     #printarea1, #printarea1 *{
         visibility: visible;
     }
     #printarea, #printarea * {
         visibility: visible;
     }
     .rz-tabview-nav li:not(.rz-tabview-selected) {
         display: none;
     }
     #printarea1 {
         position: fixed;
         left: 0;
         top: 170px;
     }
     #printarea {
         position: fixed;
         left: 15px;
         top: 0;
     }
     button[type=button], input[type=text] {
         display: none;
     }
     .rz-data-grid {
            
         height: unset!important;
     }
       
 }

Thank you.

Hi @Robot,

As far as I know print preview shows exactly what would print on a sheet of paper with the selected size (A4 for example). I don't think this can be changed somehow with CSS. Also I think it isn't related to the Radzen.Blazor components.

1 Like

thank you @korchev for your quick reply. What I don't understand is why it is only one page on the print preview. There are hundreds of records on the modal.

Hi @Robot,

Maybe this thread will help you in your case:

Hello @enchev,

Thank you for your support. Actually, I tried this CSS (one by one, altogether), but still have the same issue :frowning:

.rz-data-grid {
        height: auto !important;
        overflow: visible !important;
        page-break-after: always;
    }

I used a third party PDF library instead of directly printing, thank you for your support.

Which Library Did you used for Printing?

I am using itext7 for PDF.

Any Guide Redarding itext7, How to use it to print PDF?

Any Guide Redarding itext7, How to use it to print PDF?

Here is a method that you can modify for your needs.

private async Task Report(VendorDto vendorVM)
{
    string currency = String.Empty;
    double sum = 0;
    using var memoryStream = new MemoryStream();
    await using var rPdfWriter = new PdfWriter(memoryStream);
    rPdfWriter.SetCloseStream(false);

// Create a PDF writer and a PDF document
    PdfDocument pdf = new PdfDocument(rPdfWriter);

// Create a document and add a page to it
    Document document = new Document(pdf, PageSize.A4, false);
    
    
// Add the logo to the document
    Image logo = new Image(ImageDataFactory.Create($"{System.IO.Directory.GetCurrentDirectory()}{@"\wwwroot\css\images\logo.png"}"));
    logo.ScaleToFit(100, 100);
    document.Add(logo);

    Paragraph header = new Paragraph("PURCHASE ORDER")
        .SetTextAlignment(TextAlignment.CENTER)
        .SetFontSize(20).SetBold();
    document.Add(new Paragraph());
    document.Add(header);
    
    FontProgram fontProgram1 =
        FontProgramFactory.CreateFont(@"Resources/arial.ttf");
    PdfFont turkish = PdfFontFactory.CreateFont(fontProgram1, PdfEncodings.IDENTITY_H);

    Text address =
        new Text("test address").SetFontSize(8).SetFont(turkish);

    Text orderDate =
        new Text("Order Date: " + Order.OrderDateTime.ToShortDateString()).SetFontSize(10).SetBold();

    Paragraph p = new Paragraph().Add(address).AddTabStops(new TabStop(408f, TabAlignment.LEFT)).SetMarginBottom(0f).Add(new Tab()).Add(orderDate);
    document.Add(p);

    Text address2 =
        new Text("Test address zip code:1234565").SetFontSize(8).SetFont(turkish);

    Text orderNumber =
        new Text("Order No: " + Order.Id).SetFontSize(10).SetBold();

    Paragraph p1 = new Paragraph().Add(address2).AddTabStops(new TabStop(408f, TabAlignment.LEFT)).SetMarginBottom(0f).SetMarginTop(0f).Add(new Tab()).Add(orderNumber);
    document.Add(p1);

    
// Add a table to the document
    Table table1 = new Table(new float[] {550});
    table1.AddCell(new Cell().Add(new Paragraph("Supplier: " + orderDetails.Where(x => x.VendorId == vendorVM.Id).Select(y => y.VendorDto.Name).FirstOrDefault())));
    table1.SetFont(turkish);
    document.Add(table1);

    Table table = new Table(new float[] { 275, 275 });
    table.AddCell(new Cell().Add(new Paragraph("Bill To:")));
    table.AddCell(new Cell().Add(new Paragraph("Ship To:")));
        
    FontProgram fontProgram =
        FontProgramFactory.CreateFont(@"Resources/NotoSansSC-Regular.otf");
    PdfFont chinese = PdfFontFactory.CreateFont(fontProgram, PdfEncodings.IDENTITY_H);

    var data = new string[1, 2];
    // Add data to the table
    if (warehouseDictionaryPDF.ContainsKey(vendorVM.Warehouse))
    {
        var warehouse = warehouseDictionaryPDF[vendorVM.Warehouse];
        var firstElement = warehouse[0];
        var secondElement = warehouse[1];
        var thirdElement = warehouse[2];
        var fourthElement = warehouse[3];
        var fifthElement = warehouse[4];

        // Add newline after element 0
        firstElement += "\n";

        // Find Zip Code in the 1st element and put newline before Zip Code
        int zipIndex = secondElement.IndexOf("ZIP Code");
        if (zipIndex != -1)
        {
            secondElement = secondElement.Insert(zipIndex, "\n") + "\n";
        }

        // Concat third and fourth element and add newline after it
        var thirdAndFourthElement = thirdElement + " " + fourthElement + "\n";

        // Add the last element
        var finalElement = fifthElement;

        data[0, 0] = firstElement + secondElement + thirdAndFourthElement + finalElement;
        data[0, 1] = firstElement + secondElement + thirdAndFourthElement + finalElement;
    }
    
    for (int i = 0; i < data.GetLength(0); i++)
    {
        table.AddCell(new Cell().Add(new Paragraph(data[i, 0]).SetFontSize(8).SetFont(chinese)));
        table.AddCell(new Cell().Add(new Paragraph(data[i, 1]).SetFontSize(8).SetFont(chinese)));
    }
        
    document.Add(table);

    document.Add(new Paragraph());
    document.Add(new Paragraph());
    document.Add(new Paragraph());

    Table table2 = new Table(7).UseAllAvailableWidth();

    for (int i = 0; i < 1; i++)
    {
        //table2.AddHeaderCell(new Cell().Add(new Paragraph("ORDER ID")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Product ID")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Product Code")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Currency")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Quantity")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Unit Price")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("Total Price")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
        table2.AddHeaderCell(new Cell().Add(new Paragraph("PO Notes")).SetBackgroundColor(ColorConstants.LIGHT_GRAY).SetBold());
    }

    foreach (var details in orderDetails.Where(x => x.VendorId == vendorVM.Id))
    {
        
        table2.AddCell(new Cell().Add(new Paragraph(details.Id.ToString())));
        table2.AddCell(new Cell().Add(new Paragraph(details.ProductCode)));
        table2.AddCell(new Cell().Add(new Paragraph(details.Currency)));
        currency = details.Currency;
        table2.AddCell(new Cell().Add(new Paragraph(details.Quantity.ToString())));
        table2.AddCell(new Cell().Add(new Paragraph(details.BuyUnitPrice.ToString("0.#####"))));
        table2.AddCell(new Cell().Add(new Paragraph(details.TotalBuyPrice.ToString("0.#####"))));
        table2.AddCell(new Cell().Add(new Paragraph(details.PoNotes ?? string.Empty)));
        sum = sum + details.TotalBuyPrice;
    }
   
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph()));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph()));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph()));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph()));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph("Total: ")));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph(sum.ToString("N2")).SetBold().Add(new Text(currency))));
    table2.AddCell(new Cell().SetBorder(Border.NO_BORDER).Add(new Paragraph()));

    document.Add(table2);

    var p5 = new Paragraph();
    p5.Add(new Text("Total Line:" + orderDetails.Count(x => x.VendorId == vendorVM.Id)));
    document.Add(p5);

    LineSeparator line2 = new LineSeparator(new SolidLine());
    document.Add(line2);

    var p6 = new Paragraph();
    p6.Add(new Text("Created By:" + Order.DoneBy + "\n"));
    p6.Add(new Text("Please Confirm by Email!\n"));

    if (vendorVM.Warehouse == "China Warehouse")
    {
        p6.Add(new Text("IMPORTANT NOTE :\n"));
        // Creating a list  
        List list = new List();
        list.Add("Please add a sticker on the box when you send the goods.");
        list.Add("Please paste the packing list on the outer box of the goods."); 
        list.Add("Please fill out the form attached and share it with supplier before every shipment. Otherwise, your package will be REJECTED.");
        list.Add("Please send us tracking numbers.");
        p6.Add(list);
    }

    p6.SetFontSize(8);
    document.Add(p6);

// Close the document
    document.Close();

    memoryStream.Position = 0;
    var pdfname = $"PO-{Order.Id}-{orderDetails.Where(x => x.VendorId == vendorVM.Id).Select(y => y.VendorDto.Name).FirstOrDefault()}.pdf";

    using var streamRef = new DotNetStreamReference(stream: memoryStream);
    await JsRuntime.InvokeVoidAsync("downloadFileFromStream", pdfname, streamRef);
}
1 Like