Home Generate PDF then Print C#
Reply: 0

Generate PDF then Print C#

user2782
1#
user2782 Published in April 22, 2018, 10:03 pm

After calling API to get data, I need to generate the PDF file to PRINT using the default available. Currently, Im able to get the data but then PDF not generated or downloaded.

AJAX codes to call controller to process request.

$(document).on('click', '#RePrint', function (event) {
        var checkedVals = $('.ExportOrder:checkbox:checked').map(function () {
            var orderId = this.value;
            var status = document.getElementById(orderId).innerText;
            if (status != "Received")
            {
                return this.value;
            }
        }).get();
        if (checkedVals.length > 0) {
            $.ajax({
                url: '/Orders/RePrintLabel',
                type: 'POST',
                data: { ExportOrder: checkedVals },
                dataType: "json",
                async: true
            });
        }
    });

Controller:

public ActionResult RePrintLabel(string[] ExportOrder)
        {
            var orders = ExtractOrders(ExportOrder, "Reprint");
            if (orders.Count() > 0)
            {
                foreach (var item in orders)
                {
                    var label = _orderMgr.RePrintLabel(item);

                    //Generate PDF For Label
                    if (label != null)
                    {
                        if (label.success)
                        {
                            byte[] byteContent = label.labels[0];
                            MemoryStream pdf = new MemoryStream(byteContent);
                            Response.Clear();
                            Response.ContentType = "application/pdf";

                            string pdfName = label.order_number;
                            Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
                            Response.ContentType = "application/pdf";
                            Response.Buffer = true;
                            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
                            Response.BinaryWrite(byteContent);
                            Response.End();
                            Response.Close();
                        }
                    }
                }
            }

            return RedirectToAction("Export");
        }

The data is successfully returned and able to execute until this code Response.Close(); pdf was not downloaded or displayed to print.

Is there a way that I could automatically print the PDF file right after it is generated or the data is returned successfully using a default printer?

Thank you in advance for your help. Really appreciated.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30748 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO