when export data to pdf using itextcharp libarary it give me empty file downloaded and can’t open

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 it give me file not in correct format or may be damaged with size 1 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({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "../BusinessLayer/WebMethods.asmx/ExportGridViewToPdf",
        data: JSON.stringify({ gridData: tableData, columnNames: columns }),
        responseType: 'arraybuffer',
        processData: false,
        

        success: function (response) {
           

            var fileName = "GridView.pdf";
            var blob = new Blob([response.d], { type: 'application/pdf' });
            var downloadLink = document.createElement("a");
            downloadLink.href = window.URL.createObjectURL(blob);
            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();
                HttpContext.Current.Response.ContentType = "application/pdf";
                return ms.ToArray();
            }
            catch (Exception ex)
            {
                // Handle the exception
                throw new Exception("Error exporting GridView to PDF: " + ex.Message);
            }
        }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật