问题
i have created pdf file genration using html template. but after generating pdf background color on h4 tag and and color also not set please suggest me
i have try to inline css on set background color and table proper border color in html but after generating pdf this css not apply it
Controller:-
public void abcd()
{
try
{
string abc = Request.QueryString["abc"];
string strFileName = abc;
string strFileExtension = ".pdf";
string strContentType = FileManager.FileContentType_application_msexcel;
string strExportData = string.Empty;
Document pdfDoc = new Document(PageSize.A4, 43f, 50f, 5f, 50f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
var output = new MemoryStream();
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
StringReader sr;
sr = new StringReader(Convert.ToString(ExportData.abcUserDetails(abc)));
PdfWriter.GetInstance(pdfDoc, output);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
strFileName = strFileName.Replace(" - ", "-").Replace(" ", "-").Replace("--", "-");
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}{1}", strFileName, strFileExtension));
Response.ContentType = strContentType;
Response.Charset = "";
Response.BinaryWrite(output.ToArray());
Response.Flush();
Response.End();
}
catch (Exception ex)
{
Settings.exceptionLog.Publish(ex);
}
}
Model:-
public static string abcUserDetails(string abc)
{
string strHtmlBody = (new WebClient()).DownloadString(HttpContext.Current.Server.MapPath("~/Templates/abcUserDetails.html"));
string DomainURL = Settings.GetSettingsKeyValue("DomainURL");
IDictionary<string, string> objData = new Dictionary<string, string>();
dynamic dtUser = User.GetuserDetails(abc);
foreach (dynamic m in dtUser)
{
objData.Add("User.Name", Convert.ToString(m["Name"]));
objData.Add("User.RegNo", Convert.ToString(m["RegNo"]));
objData.Add("User.Gender", Convert.ToString(m["Gender"]));
objData.Add("User.EmailId", Convert.ToString(m["EmailId"]));
objData.Add("User.Address", Convert.ToString(m["VLEAddress"]));
}
string strValue = string.Empty;
foreach (KeyValuePair<string, string> data in objData)
{
strValue = HttpUtility.HtmlEncode(data.Value);
strHtmlBody = strHtmlBody.Replace(string.Format("%{0}%", data.Key), strValue);
}
return strHtmlBody;
}
html template:-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link href="../css/custom/style.css" rel="stylesheet" />
<title></title>
</head>
<body style="font-family:Arial;font-size:7.5px;vertical-align:top!important;">
<h4 style="background-color:#d48d88;text-transform: uppercase;font-weight:bold;width:100%;">
User Details
</h4>
<table border="1" style="color:#000000;border-color:#6f6f6f;border-style: ridge;">
<tr>
<td valign="top" style="font-weight:bold;">
<b>Name</b>
</td>
<td valign="top">
%User.Name%
</td>
<td valign="top" style="font-weight:bold;">
<b>Mobile number</b>
</td>
<td valign="top">
%User.Mobile%
</td>
<td valign="top" style="font-weight:bold;">
<b>Email Id</b>
</td>
<td valign="top">
%User.Email%
</td>
</tr>
</table>
</body>
</html>
Expected Output:-
回答1:
By default the HTML to PDF converter will render the HTML document for 'screen', but this can be changed when another media type is assigned to HtmlToPdf.MediaType property.
For example, when this property is set to 'print' the CSS properties defined by the '@media print
' rule will be used when the HTML is rendered instead of the CSS properties defined by the '@media screen
' rule.
p {
font-family: Verdana;
font-size: 14px;
font-style: italic;
color: Green;
}
@media print {
p {
border-style: dashed;
}
}
回答2:
thead th{
background: #ddd;
}
table {
width: 100% !important;
max-width: 100%;
border-spacing: 0;
border-collapse: collapse;
border-bottom: none;
}
th, td {
border: 1px solid #a9a6a6;
padding: 5px 8px;
text-align: left;
}
h4 {
background-color: #d48d88;
color: white;
padding: 5px;
text-align: center;
font-size: 16px;
}
<h4>User Details</h4>
<table>
<thead>
<tr>
<th>Name </th>
<th> %User.Name%</th>
<th>Mobile number</th>
<th> %User.Mobile%</th>
<th>Email Id</th>
<th>%User.Email%</th>
</tr>
</thead>
<tbody>
<tr>
<td> your name </td>
<td> user name </td>
<td> your mobile </td>
<td> your mobile no </td>
<td> example@gmail.com </td>
<td> User email</td>
</tr>
<tr>
<td> your name </td>
<td> user name </td>
<td> your mobile </td>
<td> your mobile no </td>
<td> example@gmail.com </td>
<td> User email</td>
</tr>
</tbody>
</table>
[ hope it'll help you :::::::::::: let me know if it works.]
来源:https://stackoverflow.com/questions/58457407/how-to-set-background-color-and-table-border-in-pdf-file-using-html-in-c-sharp