代码之家  ›  专栏  ›  技术社区  ›  Anyname Donotcare

如何在HTML字符串中嵌入自定义字体的正确URL

  •  0
  • Anyname Donotcare  · 技术社区  · 5 年前

    如何在我的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时:

    url('C:\\Users\\AA\\Desktop\\PrintingStatement\\PrintingStatement\\CSS\\majalla.ttf') 
    

    它工作得很好。如何将我的URL转换为生产环境中的前一个URL。

       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();
            }
    

    我的项目结构:

    enter image description here

    0 回复  |  直到 5 年前
        1
  •  1
  •   Reza Aghaei    5 年前

    要解析相对地址,可以使用以下选项:

    1. 使用HTML中的占位符并将其替换为bin文件夹地址。(适用于除字体以外的所有内容)
    2. 注射 <base> 在头部贴标签。(除字体外,其他都适用)
    3. 实施 IMoniker 设置文档的基URL。(适用于所有内容和字体)我已经为 WebBrowser1.SetHtmlContent(html, url)

    例子

    假设您有这样的html:

    <!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
        <title>Sample</title>
        <style>
            @font-face {
                font-family: "MyFont";
                src: url('./Contents/MyFont.ttf') format('truetype');
            }
    
            body {
                background-image: url('./Contents/Image.jpeg');
                font-family: 'MyFont';
                font-weight: normal;
                font-style: normal;
                font-size: 30px;
                color: red;
            }
        </style>
    </head>
    <body>
        Something
    </body>
    </html>
    

    我想你有 Contents 文件夹包含 Image.jpeg MyFont.ttf 项目输出目录中的文件。(如果项目中有这些文件,则将它们复制到输出目录中的正确文件夹结构中,只需在解决方案资源管理器中这些文件的“属性”窗口中设置 Copy to output directory Copy Always )

    所以不是 ./ 使用 $ROOT$/ ,例如: background-image:url('$ROOT$/Resources/Sample.jpeg'); 然后在设置浏览器的文档文本时,将其替换为应用程序的启动路径:

    this.webBrowser1.DocumentText = yourHtmlContent.Replace("$ROOT$", 
        new Uri(Application.StartupPath).ToString());
    

    解决方案2-注入HTML <基本> 标签

    假设HTML内容中没有任何占位符。那就足够注射 <底座> 标记到 <head> 这样地:

    var html = System.IO.File.ReadAllText("HTMLPage1.html");
    html = html.Replace(@"<head>", $@"<head><base href=""{new Uri(Application.StartupPath)}/""/>");
    webBrowser1.DocumentText = html;
    

    这意味着所有相关地址都将使用 <底座> Href属性。

    解决方案3-实施 伊莫尼克

    你可以实现 伊莫尼克尔 接口和使用 IWebBrowser2 SHDocVw 加载HTML内容并为此设置基本URL。我用这个方法计算 @font-face 以及HTML内容中其他内容的相对地址,它工作得很好。

    您可以在本文中找到我共享的实现: Set URL of custom HTML loaded in webbrowser 而且很容易像这样使用:

    webBrowser1.SetHtmlContent(html, 
        $@"{new Uri(Application.StartupPath)}/");