首页 > 解决方案 > Select.PDF未出现在渲染的 PDF 中

问题描述

我正在使用 select.pdf 输出 PDF 文件。我的过程首先使用以下方法在我的 Visual Studio 项目中获取 HTML 文件的文本:

string strHTML = string.Join(" ", File.ReadAllLines("Template.html", Encoding.UTF8)); 

将 strHTML 设置为 HTML 模板中的文本后,我会运行一些替换来替换模板中的变量,如下所示:

strHTML = strHTML.Replace("PARAM_A", "My Replaced Value");

完成所有这些后,我使用以下方法渲染 PDF:

 PdfDocument doc = converter.ConvertHtmlString(strHTML);

我的问题是 - 在 Template.html 文件中,我有一个 HTML img,它的 src 属性设置为公开可用的 Azure BLOB 容器中的图像,如下所示:

<img style="float:left; height:125px; width:125px" src="FULL PATH TO PNG IN AZURE BLOB CONTAINER" />

在我的本地机器上开发和调试时,一切正常,PDF 包含引用的图像。该项目是一个 Azure Web 作业,当我部署它时,PDF 会按预期生成,但 PDF 不包含图像。

我读到 Select.pdf ConvertHtmlString 方法有两个参数,我的 strHTML 文本和一个 baseUrl 字符串属性,但我不确定在我的情况下应该放什么。我正在动态生成所有 HTML,然后尝试输出 PDF。

任何意见,将不胜感激。丹尼尔

标签: pdf-generationazure-webjobsazure-blob-storageselectpdf

解决方案


正如您的描述,我可以看到您将完整的 HTML 文档转换为字符串,然后将其转换为 pdf。

使用命令包含两个参数:

PdfDocument doc = converter.ConvertHtmlString(htmlString, baseUrl);

这是一个完美的示例。

这是反编译代码可以帮助您参考:

public PdfDocument ConvertHtmlString(string htmlString, string baseUrl)
    {
      int num1;
      PdfDocument pdfDocument;
      short num2;
      switch (0)
      {
        case 0:
label_2:
          pdfDocument = (PdfDocument) null;
          num2 = (short) 4;
          num1 = (int) (IntPtr) num2;
          goto default;
        default:
          while (true)
          {
            switch (num1)
            {
              case 0:
                if (!GlobalProperties.RenderWithRestrictedEngine)
                {
                  pdfDocument = PdfDocument.ᜀ(this.ᜁ((string) null, htmlString, baseUrl, baseUrl, false));
                  num2 = (short) 3;
                  num1 = (int) (IntPtr) num2;
                  continue;
                }
                num2 = (short) 5;
                num1 = (int) (IntPtr) num2;
                continue;
              case 1:
              case 3:
                goto label_13;
              case 2:
                num2 = (short) 0;
                num1 = (int) (IntPtr) num2;
                continue;
              case 4:
label_3:
                num2 = (short) 1;
                if (num2 == (short) 0)
                  ;
                if (this.Options.RenderingEngine != RenderingEngine.WebKitRestricted)
                {
                  num2 = (short) 2;
                  num1 = (int) (IntPtr) num2;
                  continue;
                }
                break;
              case 5:
                num2 = (short) 21784;
                int num3 = (int) num2;
                num2 = (short) 21784;
                int num4 = (int) num2;
                switch (num3 == num4 ? 1 : 0)
                {
                  case 0:
                  case 2:
                    goto label_3;
                  default:
                    num2 = (short) 0;
                    if (num2 == (short) 0)
                      break;
                    break;
                }
                break;
              default:
                goto label_2;
            }
            pdfDocument = PdfDocument.ᜀ(this.ᜀ((string) null, htmlString, baseUrl, baseUrl, false));
            num2 = (short) 0;
            num2 = (short) 1;
            num1 = (int) (IntPtr) num2;
          }
label_13:
          return pdfDocument;
      }
    }

推荐阅读