سؤال

I am using MVC3 and evopdf (http://www.evopdf.com/) to create a pdf when user clicks on a print button.

I have the initial view that contains a form and print button and a 2nd view specifically designed for printing.

Clicking on the print button calls javascript to submit the form, which calls the print action.

What I would like to happen is that once the print button has been clicked, a "processing" message is displayed. Once the pdf has been generated I would like the message to be removed.

This is the javascript (I have not included all of my javascript as there are parts that are not relevant)

$(document).ready(function () {
    $(".btn").click(function () {
        var delay = 10; 
        $("#AjaxDelay").val(delay);

        $.blockUI();
        $('#printForm').submit();
    });

    $('#printForm').submit(function () {
        $.blockUI();    
        $.ajax({
            url: this.action,
            type: this.method,          
            success: function (data) {
                $.unblockUI();
                //I need to open the pdf in appropriate app, adobe pdf viewer or similar
            },
            failure:function(data) {
                alert("error");
                $.unblockUI();
            }
        });
        return false;
    });

});

The form

@using (Html.BeginForm("PrintView", "Index", new { format = "pdf/", id = Model.ID,     AjaxDelay = Model.AjaxDelay }, FormMethod.Get, new { id="printForm"  }))
{
    @Html.HiddenFor(m=>m.AjaxDelay)         
    <button type="button" class="btn print" >Print</button>        
}

IndexController

public ActionResult PrintView(int id)
{           
    var model = GetData(id);
    return View(model);
}

This is my HttpHandler

public void ProcessRequest(HttpContext context)
{
    if (!context.Request.IsAuthenticated) return;    
    ProducePDF(context, "pdf title", 20);
}

private void ProducePDF(HttpContext context, string title,int delay)
{
    var pdfConverter = GetPdfConverter();

    // set the license key
    pdfConverter.LicenseKey = "licence key here";
    pdfConverter.JavaScriptEnabled = true;

    var pdfBytes = pdfConverter.GetPdfBytesFromUrl(context.Request.Url.ToString().Replace(".pdf/", ""));

    // send the PDF document as a response to the browser for download
    var response = HttpContext.Current.Response;
    response.Clear();
    response.AddHeader("Content-Type", "application/pdf");
    response.AddHeader("Content-Disposition", String.Format("attachment; filename=" + title + ".pdf; size={0}", pdfBytes.Length));
    response.BinaryWrite(pdfBytes);
    response.End();
}   

Thanks.

هل كانت مفيدة؟

المحلول

I found a solution that uses the jquery cookie plugin and appending a cookie to the response as described in this stackoverflow article.

Hide image using Javascript after controller action is complete MVC3

I did have to make a small change in that I had to specify the path when removing the cookie.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top