我正在尝试使用 DynamicPdf.HmtlConverter 库从 Html 字符串生成 Pdf。为了生成 Html 字符串,我使用 System.Web.UI 中的 HtmlTextWriter 类。我正在尝试添加外部样式表,如下所示:
StringBuilder sb = new StringBuilder();
sb.Append(@"<!DOCTYPE html>" + Environment.NewLine);
StringWriter stringWriter = new StringWriter();
using (HtmlTextWriter writer = new HtmlTextWriter(stringWriter))
{ //adding head and link tag
writer.RenderBeginTag(HtmlTextWriterTag.Html);
writer.RenderBeginTag(HtmlTextWriterTag.Head);
writer.Write("<meta charset=" + "\"UTF-8\">");
//Add Link tag attributes
writer.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
writer.AddAttribute(HtmlTextWriterAttribute.Href,@"~\Stylesheet1.css"); //style sheet reference
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/css");
writer.RenderBeginTag(HtmlTextWriterTag.Link);
writer.RenderEndTag(); // end of Link tag
writer.RenderEndTag(); // end of head tag
// Body tag
writer.RenderBeginTag(HtmlTextWriterTag.Body)
writer.RenderEndTag(); // end of Body tag
writer.RenderEndTag(); // end of Html tag
}
sb.Append(stringWriter);
因此“sb”将具有 Html 字符串,该字符串将传递给 DynamicPdf 库方法以生成 Pdf,如下所示:
ceTe.DynamicPDF.HtmlConverter.Converter.Convert(sb.ToString(),@"~\output3.pdf", null, options);
外部样式表对 Html 控件没有任何影响。
任何建议如何使用带有 HtmlTextWriter 和 DynamicPdf 库的外部样式表来生成 Pdf ..!!!