如何将ASPX页面转换为pdf文件

时间:2014-05-18 09:18:31

标签: c# asp.net

请参阅以下用于生成给定格式的pdf文件的代码。只要我点击提交按钮,它就不会显示任何错误,但创建的pdf是空白的。请帮助。

请参阅以下用于生成给定格式的pdf文件的代码。只要我点击提交按钮,它就不会显示任何错误,但创建的pdf是空白的。请帮助。

    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();
    }

1 个答案:

答案 0 :(得分:0)

“Rotativa”是解决您问题的最佳解决方案。它允许您通过直接传递任何页面URL来转换pdf,它还可以灵活地传递HTML内容。

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