【发布时间】:2019-12-13 05:05:20
【问题描述】:
如何在我的 HTML 字符串中嵌入自定义字体的正确 URL。
我执行以下操作:
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string path4 = Path.Combine(Regex.Escape(exeDir), "\\..\\\\..\\\\CSS\\\\majalla.ttf");
string arabicFont = @"@font-face {
font-family: 'sakkal_majallaregular';
src: url(" + "'" + path4 + "'" + @"), url('majalla-webfont.woff2') format('woff2'), url('majalla-webfont.woff') format('woff');}";
这不起作用(不生效)。调试后path4 =
C:\\Users\\AA\\Desktop\\PrintingStatement\\PrintingStatement\\bin\\Debug\\..\\..\\CSS\\majalla.ttf
当我尝试这样的常量绝对网址时:
url('C:\\Users\\AA\\Desktop\\PrintingStatement\\PrintingStatement\\CSS\\majalla.ttf')
它工作正常。如何在生产环境中将我的网址转换为以前的网址。
我更新的 HTML 方法
protected string StyleStatementDoc(SingleStatement statementToPrint)
{
string path1 = "CSS/StatementVerifacation.css";
string path2 = "CSS/Statement.css";
string path3 = "CSS/print.min.css";
string path4 = "CSS/majalla.ttf";
string arabicFont = @"@font-face {
font-family: 'sakkal_majallaregular';
src: url(" + "'" + path4 + "'" + @"), url('majalla-webfont.woff2') format('woff2'), url('majalla-webfont.woff') format('woff');
}";
string htmlData = @"<!DOCTYPE html>
<html>
<head>
<title>Statement</title>
<style>" + arabicFont + @"
body {
font-size: 20px;
background-color: White;
color: Black;
font-family:'sakkal_majallaregular', Arial, Helvetica, sans-serif;
text-align:right;
direction:rtl;
}
p {
line-height: 32px; /* within paragraph */
margin-bottom: 30px; /* between paragraphs */
}
</style>
<link href = '" + path1 + "'" + " rel='stylesheet' />" + @"
<link href = '" + path3 + "'" + " rel='stylesheet' />" + @"
</head>
<body>
<div class='my' id='editor1'>" + statementToPrint.StatementBeforePrint + @"
</div>
</body>
</html>
";
// htmlData = htmlData.Replace(@"<head>", $@"<head><base href=""{new Uri(Application.StartupPath)}/""/>");
return htmlData;
}
System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();
protected void Print(string htmlData)
{
webBrowser.SetHtmlContent(htmlData, $@"{new Uri(Application.StartupPath)}/");
webBrowser.Print();
}
我的项目结构:
【问题讨论】:
-
您是否检查过浏览器的开发控制台以查看实际发出了什么请求以及响应是什么?在浏览器中按 F12。
-
@itsme86 运行时的 url
C:\\Users\\AA\\Desktop\\PrintingStatement\\PrintingStatement\\bin\\Debug\\..\\..\\CSS\\majalla.ttf -
如果字体路径与exe所在的目录无关,为什么要使用该文件夹来构建URL?您要捕获该路径的哪一部分?用户的主目录?
-
@itsme86 这是 Windows 窗体应用程序,所以我考虑为用户安装它时路径应该如何(生产环境)。
-
不清楚问题是什么。 html在哪里? CSS在哪里?字体文件在哪里?