Alina Anjum
Alina Anjum

Reputation: 1230

pdf file not saving after creation

I am creating a pdf file in my project.Pdf generation is working fine.After creation,it gets open into the browser but when I click on save link ,it prompts to save the whole page not the pdf file.Can any one help me please ? Here is my code :

  protected void btn_ViewSlip_Click(object sender, EventArgs e)
        {
            dt_Payslip = mm_salary.GetPayslipData(DDL_Year.SelectedValue.ToString(), DDL_Month.SelectedValue.ToString(), Session["empcd"].ToString());
            if (dt_Payslip.Rows.Count > 0)
            {
                dt_CurrPay = emp_coprofile.GetCurrExp(Session["empcd"].ToString(), DDL_Month.SelectedValue.ToString(), DDL_Year.SelectedValue.ToString());
                GenerateDisclaimerPDF();//pdf is generating here
                string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() + " - Payslip.pdf";
                System.IO.FileInfo file = new System.IO.FileInfo(url);
                if (file.Exists)
                {
                    WebClient client = new WebClient();
                    Byte[] buffer = client.DownloadData(url);
                    Response.ContentType = "application/pdf";
                    Response.AddHeader("content-length", buffer.Length.ToString());
                    Response.BinaryWrite(buffer);
                }
            }
            else
            {
                msg = "Pay slip for "+DDL_Month.SelectedValue.ToString()+"-"+DDL_Year.SelectedValue+" does not exist";
            }
        }

OUTPUT : enter image description here

Upvotes: 0

Views: 516

Answers (2)

Stefan Steiger
Stefan Steiger

Reputation: 82186

That is because you're setting

Response.ContentType = "application/pdf";

That tell's Chrome the file is a PDF, and then it opens it with PDFjs, and when you click on save, it wants to save the PDFjs-Viewer web-Page instead of the PDF.

If you click on the download-icon, then you can download the PDF.
You need to set content-disposition "attachment", and you need to set the mime-type to application/octet-stream. Then it works in Chrome (and IE and Firefox).

If you want to display the PDF in a viewer in IE just like in Chrome, then you need to set content-disposition inline (IE ignores the mime-type and goes for the file extension instead).

Dim baPDF As Byte() = GetPdfFromImage(Me.Data)

context.Response.Clear()
'context.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName)
context.Response.AddHeader("Content-Disposition", Portal.ASP.NET.GetContentDisposition("Drucken.pdf", "inline"))
context.Response.AddHeader("Content-Length", baPDF.Length.ToString())
' context.Response.ContentType = "application/msword"
' context.Response.ContentType = "application/octet-stream"

' http://superuser.com/questions/219870/how-to-open-pdf-in-chromes-integrated-viewer-without-downloading-it#
' context.Response.ContentType = "text/html"
context.Response.ContentType = "application/pdf"

Also, be aware if you have UTF-8 characters in the filename, you need to properly set the filename header (different values for different browsers in different versions [IE]).

Public Shared Function GetContentDisposition(ByVal strFileName As String) As String
    Return GetContentDisposition(strFileName, "attachment")
End Function ' GetContentDisposition '


' http://www.iana.org/assignments/cont-disp/cont-disp.xhtml '
Public Shared Function GetContentDisposition(ByVal strFileName As String, ByVal strDisposition As String) As String
    ' http://stackoverflow.com/questions/93551/how-to-encode-the-filename-parameter-of-content-disposition-header-in-http '
    Dim contentDisposition As String
    strFileName = StripInvalidPathChars(strFileName)

    If String.IsNullOrEmpty(strDisposition) Then
        strDisposition = "inline"
    End If

    If System.Web.HttpContext.Current IsNot Nothing AndAlso System.Web.HttpContext.Current.Request.Browser IsNot Nothing Then
        If (System.Web.HttpContext.Current.Request.Browser.Browser = "IE" And (System.Web.HttpContext.Current.Request.Browser.Version = "7.0" Or System.Web.HttpContext.Current.Request.Browser.Version = "8.0")) Then
            contentDisposition = strDisposition + "; filename=" + Uri.EscapeDataString(strFileName).Replace("'", Uri.HexEscape("'"c))
        ElseIf (System.Web.HttpContext.Current.Request.Browser.Browser = "Safari") Then
            contentDisposition = strDisposition + "; filename=" + strFileName
        Else
            contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
        End If
    Else
        contentDisposition = strDisposition + "; filename*=UTF-8''" + Uri.EscapeDataString(strFileName)
    End If

    Return contentDisposition
End Function ' GetContentDisposition '

Also, if you're open to good advice, get rid of

Session["empcd"]

You're setting yourselfs up for NullReferenceExceptions, and it's entirely unnecessary...
Try not to use sessions, if you can avoid it.

Oh, and by the way:

You should add a using clause to your WebClient; otherwise you're not properly disposing of resources.

using(WebClient client = new WebClient())
{
       // Your code goes here
} // End Using

That will have the added benefit over client.Dispose() that you don't need to put it into a try-cach if-not-null-dispose to assure resources are freed afterwards.

Upvotes: 1

Alina Anjum
Alina Anjum

Reputation: 1230

I modified my code as :

 protected void btn_ViewSlip_Click(object sender, EventArgs e)
    {
        dt_Payslip = mm_salary.GetPayslipData(DDL_Year.SelectedValue.ToString(), DDL_Month.SelectedValue.ToString(), Session["empcd"].ToString());
        if (dt_Payslip.Rows.Count > 0)
        {
            dt_CurrPay = emp_coprofile.GetCurrExp(Session["empcd"].ToString(), DDL_Month.SelectedValue.ToString(), DDL_Year.SelectedValue.ToString());
            GenerateDisclaimerPDF();
            string url = FilesPath.Path_SaveFile + Session["empcd"].ToString() + " - Payslip.pdf";
            System.IO.FileInfo file = new System.IO.FileInfo(url);
            if (file.Exists)
            {
                //WebClient client = new WebClient();
                //Byte[] buffer = client.DownloadData(url);
                //Response.ContentType = "application/pdf";
                //Response.AddHeader("content-length", buffer.Length.ToString());
                //Response.BinaryWrite(buffer);
               // Response.Clear();
                WebClient client = new WebClient();
                Byte[] buffer = client.DownloadData(url);
                Response.AddHeader("content-disposition", "attachment; filename=" + Session["empcd"].ToString() + " - Payslip.pdf");
                Response.AddHeader("content-length", buffer.Length.ToString());  
                Response.ContentType = "application/pdf";
                Response.BinaryWrite(buffer);
            }
        }
        else
        {
            msg = "Pay slip for "+DDL_Month.SelectedValue.ToString()+"-"+DDL_Year.SelectedValue+" does not exist";
        }
    }

Upvotes: 0

Related Questions