简体   繁体   中英

How to Convert ASPX page to pdf file

please see the below code i had used to generate a pdf file in the given format. As soon as i hit the submit button it does not sho any error but the pdf created is blank. Kindly help.

please see the below code i had used to generate a pdf file in the given format. As soon as i hit the submit button it does not sho any error but the pdf created is blank. Kindly help.

    protected void Button1_Click(object sender, EventArgs e)
    {
        string str;
        DataTable dt = new DataTable();
        string code;
        string codeval;
        int ctr;
        string int_id="";

        str = ("select top 1 int_id from interview_call  order by int_id desc");

        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();

        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        ad.Fill(dt);

        try
        {
            code = dt.Rows[0]["int_id"].ToString();
            codeval = code.Substring(1, 3);
            ctr = Convert.ToInt32(codeval);
        }
        catch (Exception ew)
        {
            code = "";
            codeval = "";
            ctr = 0;
        }

        if ((ctr == 0))
        {
            int_id = "I001";
        }
        else if ((ctr >= 1) && (ctr < 9))
        {
            ctr = ctr + 1;
            int_id = "I00" + ctr;
        }
        else if ((ctr >= 9) && (ctr < 99))
        {
            ctr = ctr + 1;
            int_id = "I0" + ctr;
        }
        else if (ctr >= 99)
        {
            ctr = ctr + 1;
            int_id = "I" + ctr;
        }

        con.Close();

        string date = Label6.Text;
        string time = Label7.Text;
        string venue = TextBox1.Text;

        con.Open();

        using (SqlCommand cmd3 = new SqlCommand())
        {
            cmd3.Connection = con;
            cmd3.CommandType = CommandType.Text;
            cmd3.CommandText = "insert into interview_call values (@var0,@var1,@var2,@var3,@var4,@var5,@var6)";

            cmd3.Parameters.AddWithValue("@var0", int_id);
            cmd3.Parameters.AddWithValue("@var1", jid);
            cmd3.Parameters.AddWithValue("@var2", uid);
            cmd3.Parameters.AddWithValue("@var3", date);
            cmd3.Parameters.AddWithValue("@var4", time);
            cmd3.Parameters.AddWithValue("@var5", venue);
            cmd3.Parameters.AddWithValue("@var6", int_id+"_"+uid+".pdf");

            int rowsAffected = cmd3.ExecuteNonQuery();

            //---------------------------------------------------------------------------
            string sPathToWritePdfTo = Server.MapPath("~/Call Letters/") + int_id + "_" + uid + ".pdf";
            System.Text.StringBuilder sbHtml = new System.Text.StringBuilder();

            sbHtml.Append("<html>");
            sbHtml.Append("<body>");
            sbHtml.Append("<table style='width: 70%;'>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td colspan='3'>");
            sbHtml.Append("<b>AEDC Ltd. Job Interview</b></td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Full Name</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label1.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("<td rowspan='6' valign='top'>");
            sbHtml.Append(Image1.ImageUrl = url);
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Email ID</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label2.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Contact Number</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label3.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Job Applied</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label4.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px' valign='top'>");
            sbHtml.Append("Current Address</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label5.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Date</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label6.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Time</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(Label7.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("Venue</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append(TextBox1.Text);
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("</td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("<b>Regards,</b></td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("<tr>");
            sbHtml.Append("<td style='width: 239px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td style='width: 405px'>");
            sbHtml.Append("</td>");
            sbHtml.Append("<td>");
            sbHtml.Append("<b>Assam Electronics Development Corporation Ltd.(AMTRON)<br />");
            sbHtml.Append("Industrial Estate, Bamunimaidan<br />");
            sbHtml.Append("Guwahati-21, Assam<br />");
            sbHtml.Append("+91 0361-2724204/222 (Off)<br />");
            sbHtml.Append("+91 0361-2724181/131 (Fax)</b></td>");
            sbHtml.Append("</tr>");
            sbHtml.Append("</table>");
            sbHtml.Append("</body>");
            sbHtml.Append("</html>");

            using (System.IO.Stream stream = new System.IO.FileStream(sPathToWritePdfTo, System.IO.FileMode.OpenOrCreate))
            {
                Pdfizer.HtmlToPdfConverter htmlToPdf = new Pdfizer.HtmlToPdfConverter();
                htmlToPdf.Open(stream);
                htmlToPdf.Run(sbHtml.ToString());
                htmlToPdf.Close();
            }
            //---------------------------------------------------------------------------

            if (rowsAffected == 1)
            {
                using (SqlCommand cmd4 = new SqlCommand())
                {
                    cmd4.Connection = con;
                    cmd4.CommandType = CommandType.Text;
                    cmd4.CommandText = "update applied_list set status=@var0 where app_id=@var1";

                    cmd4.Parameters.AddWithValue("@var0", "INVITED");
                    cmd4.Parameters.AddWithValue("@var1", aid);

                    int rowsAffected2 = cmd4.ExecuteNonQuery();
                }
                Response.Redirect("hr_list5.aspx");
            }
        }
        con.Close();
    }

"Rotativa" is the best fit solution to your problem. it allow you to convert pdf by passing any URL of page directly, it also give flexibility to pass HTML content also.

http://www.nuget.org/packages/Rotativa/1.6.1
https://github.com/webgio/Rotativa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM