ahmed salah

ahmed salah

  • 1.1k
  • 596
  • 37.1k

export gridview have data to pdf failed and file not open

Jul 26 2024 5:49 PM

I work on asp.net I face issue when export grid view have more than 2000 rows it download empty file pdf without data inside .

and when click on file not open and give me file not in correct format or may be damaged with size 22 byte

so How to solve this issue and generate pdf file have more data and open it.

I don't need to use base 64 because it small limit for data until 8000 characters

and data exported will have ore than this limit .

what I try as below :

<div class="form-group col-12">
                                            <button id="btnSave" type="button" class="btn btn-outline-primary col-12" onclick="ExportGridViewToPdf();">
                                                <span style="font-size: 20px; line-height: 1.5;">'????? ?????????</span><i class="fas fa-print fa-fw fa-2x pull-center"></i></button>
                                        </div>
 <div class="mt-2 row">
                    <div class="form-group col-lg-12">
                        <div class="table-responsive">
                            <asp:GridView ID="gvResults" ClientIDMode="Static" runat="server"
                                ShowFooter="false" RowStyle-CssClass="record" CssClass="table table-striped table-hover table-bordered direction gridview-scroll  customCard card-outline-primary"
                                PageSize="5" Width="100%" border="0" CellSpacing="2" CellPadding="0"
                                AutoGenerateColumns="false">
                                <RowStyle HorizontalAlign="Center" CssClass="record " />

                                <HeaderStyle CssClass="table-info text-center" />

                            </asp:GridView>
                        </div>
                    </div>
                </div>
 
function ExportGridViewToPdf() {
debugger;
    var tableData = [];
    var columns = [];

    // Get the data from the GridView
    $('#gvResults tr').each(function (index) {
        if (index === 0) {
            // Get the column names
            $(this).find('th').each(function () {
                columns.push($(this).text());
            });
        } else {
            var rowData = [];
            $(this).find('td').each(function () {
                rowData.push($(this).text());
            });
            tableData.push(rowData);
        }
    });
 
    // Make an AJAX call to the server-side method
    $.ajax({

     
        
        

        success: function (response) {
           

             var fileName = "GridView.pdf";
            var blob = new Blob([response.d], { type: 'application/pdf' });
            var downloadLink = document.createElement("a");
           
            downloadLink.setAttribute("download", fileName);
            document.body.appendChild(downloadLink);
            downloadLink.click();
            document.body.removeChild(downloadLink);
      
       

        },
        error: function (xhr, status, error) {
            // Handle the error response
            console.error("Error exporting PDF: " + error);
        }
    });
}
   [WebMethod]
        public byte[] ExportGridViewToPdf(string[][] gridData, string[] columnNames)
        {
            try
            {
                // Create a new PDF document
                var document = new Document(PageSize.A4, 25, 25, 30, 30);
                var ms = new MemoryStream();
                var writer = PdfWriter.GetInstance(document, ms);
                document.Open();

                // Add the table to the PDF document
                var table = new PdfPTable(columnNames.Length);
                table.WidthPercentage = 100;

                // Add the column headers
                foreach (var columnName in columnNames)
                {
                    var cell = new PdfPCell(new Phrase(columnName));
                    cell.BackgroundColor = new BaseColor(System.Drawing.Color.LightGray);
                    table.AddCell(cell);
                }

                // Add the data rows
                foreach (var row in gridData)
                {
                    foreach (var value in row)
                    {
                        table.AddCell(value);
                    }
                }

                document.Add(table);
                document.Close();
                writer.Close();
      
                return ms.ToArray();
            }
            catch (Exception ex)
            {
                // Handle the exception
                throw new Exception("Error exporting GridView to PDF: " + ex.Message);
            }
        }

the below is image for what I try with debug


Answers (2)