1

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.

Jen143
  • 815
  • 4
  • 17
  • 42
  • Are you able to render the PDF on the web page? – Souvik Ghosh Apr 17 '18 at 04:07
  • No. Nothing happens. It is because i call the controller from ajax request? Bec I could normally download the file when not using ajax request – Jen143 Apr 17 '18 at 04:11
  • You should first render the PDF on the web page. It can be loaded within an `iframe` and then you can trigger the print on that without downloading it. However, there are security issues with that due to which the modern browsers doesn't allow it. Check these links- https://stackoverflow.com/questions/7074070/how-to-send-a-pdf-file-directly-to-the-printer-using-javascript – Souvik Ghosh Apr 17 '18 at 04:16
  • I dont think if that is what i want. – Jen143 Apr 17 '18 at 04:17

0 Answers0