NodeJS PDF:仅在附加调试器时才能正常工作

NodeJS PDF: Only working properly when debugger is attached

我的一个项目(但不是其他项目)出现了一个奇怪的问题,我看不到任何其他人有类似的问题。 (由于这是实际的公司代码,因此我为缺少适当的示例而提前道歉)。

我遇到的问题是在 .NET Core 2 项目中,我正在尝试使用 nodejs 创建 PDF(就像我在第二个项目中遇到的那样)。但是,当附加 Visual Studio (2017) 调试器时,PDF 仅生成 正确

try
{
    var result = await nodeServices.InvokeAsync<byte[]>(pdfSettings, viewWithAbsoluteLinks);

    string invoicePath = _configuration.GetSection("AppSettings")["InvoicePath"];
    string filename = Path.Combine(invoicePath, "invoice.pdf");

    FileUtils.WriteToFile(filename, result);

    return Json(new { success = true, size = result.Count(), viewAsHtml, viewWithAbsoluteLinks, environment, pdfSettings, result });
}
catch
{
    return Json(new { success = false });
}

我将 jsrender 与 phantom-pdf 一起使用,当我在 Visual Studio 中附加调试器时,结果是 69318。但是,当我 运行 没有附加调试器时,结果 = 66167 字节。

为了调试,我返回了变量 viewWithAbsoluteLinks,这样我就可以看到是否有任何区别,并且在这两种情况下,该变量是相同的。

viewWithAbsoluteLinks 是:

<html>

<head>
  <style>
    .pdf-hidden {
      display: none;
    }

    .pdf-visible {
      display: block !important;
    }
  </style>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>Invoice</title>
  <link rel="stylesheet" href="http://localhost:10795/lib/bootstrap/dist/css/bootstrap.css">
  <link rel="stylesheet" href="http://localhost:10795/css/site.css">
  <link rel="stylesheet" href="http://localhost:10795/css/site.theme.css">
  <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt"
    crossorigin="anonymous">
  <link rel="stylesheet" href="http://localhost:10795/lib/bootstrap-datetimepicker/bootstrap-datetimepicker.css">
  <style>
    body {
      font-family: "DIN 2014 Light", 'Open Sans', 'Helvetica Neue', Helvetica, Arial, sans-serif;
      color: #000;
    }
  </style>
</head>

<body>
  <div class="pull-right" style="border: 1px solid #0f0">
    <img src="http://localhost:10795/images/training-logo.png">
  </div>
  <div style="padding-top: 50px;">
    <table style="width: 100%; border: 1px solid #000">
      <tbody>
        <tr>
          <td style="border: 1px solid #f00"> Address </td>
          <td> Invoice </td>
        </tr>
      </tbody>
    </table>
  </div>
  <script type="text/javascript">        window.JSREPORT_READY_TO_START = true;    </script>
</body>

</html>

附加调试器后,标签中的文本会显示在 PDF 中。当调试器未附加时,文本不会出现。 (s 有不同的颜色边框 - 颜色显示正确)

我假设 运行ning 在调试时改变了一些东西,但我不确定是什么。一开始我以为只是 CSS 使文本变白了,但文件大小表明文本确实不存在。

有没有人知道 Visual Studio 在附加调试器时可以做什么以使其正常工作?

经过一段时间的努力,我可能刚刚解决了这个问题。计时。

虽然附加了调试器(没有断点,只是附加),但这可能导致了一个小延迟,刚好足以生成 PDF。

快速查看 SO,我发现了这个:How to set time delay in javascript

我添加了 1 秒的延迟,它在没有附加调试器的情况下正确生成了带有文本的 PDF。