无法将css链接到c#项目中生成的html

问题描述 投票:0回答:1

我有一个功能,可以通过单击按钮生成pdf文件。 PDF是通过构建html字符串使用NReco生成的。我正在使用这个html内容的样式表。当我使用样式表的绝对路径时,它工作正常。但相对路径却没有。以下是代码:

protected void Pdf_Download(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["test_rashmiConnectionString"].ToString();
        SqlConnection con = new SqlConnection(constr);
        con.Open();

        var closeLink = (Control)sender;
        GridViewRow row = (GridViewRow)closeLink.NamingContainer;
        string jobID = row.Cells[0].Text.Split('-')[1].ToString();

        string query = "select [TakenBy],[CertifiedPayroll],[PrevailingWage],[BillingType],[QuotedPricePer],[MobilizationPrice],[Labor],[Equipment] FROM [JobDetails] where jobID=" + jobID;
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        string query2 = "select C.Name, C.city,C.State,C.County, C.JobLocation,C.DateOrdered,C.StartDate,C.NoOfDays from JobDetails J inner join CustomerDetails C on J.CustomerID=C.ID where [JobID]=" + jobID;
        SqlCommand cmd2 = new SqlCommand(query2, con);
        SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
        DataTable dt2 = new DataTable();
        da2.Fill(dt2);

        string query3 = "select B.StreetAddress,B.City,B.State,B.Zip from JobDetails J inner join BillingAddress B on J.BillingAddressID=B.ID where[JobID]=" + jobID;
        SqlCommand cmd3 = new SqlCommand(query3, con);
        SqlDataAdapter da3 = new SqlDataAdapter(cmd3);
        DataTable dt3 = new DataTable();
        da3.Fill(dt3);

        string query4 = "select CD.Name,CD.PhoneNumber,CD.CellNumber,CD.Fax,CD.Description,CD.[Delivery Instructions] from JobDetails J inner join ContactDetails CD on J.BillingAddressID=CD.ID where [JobID]=" + jobID;
        SqlCommand cmd4 = new SqlCommand(query4, con);
        SqlDataAdapter da4 = new SqlDataAdapter(cmd4);
        DataTable dt4 = new DataTable();
        da4.Fill(dt4);

        StringBuilder newline = new StringBuilder();
        newline.Append("<br />");

        string html = toHTML_Table(dt);
        string html2 = toHTML_Table(dt2);
        string html3 = toHTML_Table(dt3);
        string html4 = toHTML_Table(dt4);

        string printDate = DateTime.Now.ToString();

        StringBuilder builder = new StringBuilder();
        builder.Append("<html>");
        builder.Append("<head runat='server'>");
        builder.Append("<title>");
        builder.Append(Guid.NewGuid());
        builder.Append("</title>");
        builder.Append("<link rel='stylesheet' type='text/css' href='~/PDF/style.css'>");
        builder.Append("</head>");
        builder.Append("<body>");
        builder.Append("<header><h3 align='center'>JobNumber - " + jobID + "</h3></header><p align='center'>PrintDate: " + printDate + "</p><div id='details'>");
        builder.Append("<h4>Job Details: " + "</h4>");
        builder.Append(html);
        builder.Append(newline);
        builder.Append("<h4>Customer Information: " + "</h4>");
        builder.Append(html2);
        builder.Append(newline);
        builder.Append("<h4>Billind Address: " + "</h4>");
        builder.Append(html3);
        builder.Append(newline);
        builder.Append("<h4>Contact Details: " + "</h4>");
        builder.Append(html4);
        //builder.Append("<hr width=25%>");
        builder.Append("<div id='signature1'><p id='cust_sign1'>Customer Signature: </p><hr id='sign1' width=25%></div></br><div id='signature2'><p id='cust_sign2'>Employee Signature: </p><hr id='sign2' width=25%></div>");
        builder.Append("</div></body></html>");

        string data = builder.ToString();
        //System.IO.File.WriteAllText(@"C:\test.html", data);
        System.IO.File.WriteAllText(Server.MapPath("~/PDF/test.html"), data);

        string filename = @"NReco.pdf";
        string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + filename;
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + filename);

        var htmlContent = String.Format(data);
        var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
        Byte[] pdfBytes = htmlToPdf.GeneratePdf(htmlContent);  
        Response.BinaryWrite(pdfBytes.ToArray());
        Response.End();
    }
c# html css relative-path
1个回答
0
投票

改变这一行:

builder.Append("<link rel='stylesheet' type='text/css' href='~/PDF/style.css'>");

至:

builder.Append("<link rel='stylesheet' type='text/css' href='" + Server.MapPath("~/PDF/style.css") + "'>");

在纯HTML代码的上下文中,~/PDF/style.css不是有效的相对路径。

© www.soinside.com 2019 - 2024. All rights reserved.