2017-12-17 9 views
0

버튼 클릭으로 pdf 파일을 생성하는 기능이 있습니다. PDF는 HTML 문자열을 작성하여 NReco를 사용하여 생성됩니다. 이 html 콘텐츠에 대한 스타일 시트를 사용하고 있습니다. 스타일 시트에 절대 경로를 사용하면 잘 동작합니다. 그러나 상대 경로는 그렇지 않습니다.css를 C# 프로젝트 내에 생성 된 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(); 
    } 

답변

0

변경이 라인 :

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

에 순수한 HTML 코드의 맥락에서

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

, ~/PDF/style.css가 유효한 상대 경로가 아닌 다음은 코드입니다.

+0

. 정말 고맙습니다! –