0

Right now the generated pdf is saving on my computer and i need to preview it so that i can either choose to download or to print it. I have a feeling i need to save the generated pdf in a memory stream and then use that as a path to open it in a new window, but i don't know how to do that.

If there is a different way of doing it I'm also open to that.

This is the code i have that generates a pdf and downloads it on a button click event.

       protected void btn_process_invoice_Click(object sender, EventArgs e)
    {

        int id = Int32.TryParse(txt_update_invoice_id.Text, out id) ? id : 0;

        int update_invoice_id = int.TryParse(txt_update_invoice_id.Text, out update_invoice_id) ? update_invoice_id : 0;

        int linked_comp_id = int.TryParse(DD_update_comp_name.SelectedValue, out linked_comp_id) ? linked_comp_id : 0;
        int client_comp_id = int.TryParse(DD_update_client_name.SelectedValue, out client_comp_id) ? client_comp_id : 0;
        DateTime inv_date = DateTime.TryParse(txt_update_invoice_date.Text, out inv_date) ? inv_date : DateTime.MinValue;
        DateTime due_date = DateTime.TryParse(txt_update_due_dates.Text, out due_date) ? due_date : DateTime.MinValue;

        int inserted_id = 0;

        db_invoice.save(id, linked_comp_id, client_comp_id, txt_update_invoice_num.Text, inv_date, due_date, txt_ref.Text, txt_update_pastel_ref.Text, 2, out inserted_id);

        Session["id"] = id.ToString();
        Session["invoice_num"] = txt_update_invoice_num.Text;

        //ScriptManager.RegisterStartupScript(Page, Page.GetType(), "newWindow", "$window.open('../view_invoice.aspx');", true);
        Response.Redirect("~/view_invoice.aspx");

        //Response.Redirect("~/invoice.aspx");

    }
  • You need a `content-disposition` header. See https://stackoverflow.com/questions/1012437/uses-of-content-disposition-in-an-http-response-header but you'll only be able to view a PDF if the browser has a PDF view add-in. – Robin Bennett Apr 03 '19 at 08:02
  • And even then it's up to the user to allow opening of a PDF in the browser. – VDWWD Apr 03 '19 at 08:23
  • Thank you. I have just used this. And It does open in a new tab but it first downloads and saves on my computer. Do you think I should remove the `db_invoice.save` to prevent the download? – Madeline Kallis Apr 03 '19 at 08:38

0 Answers0