【问题标题】:NodeJS PDF: Only working properly when debugger is attachedNodeJS PDF:仅在附加调试器时才能正常工作
【发布时间】:2018-12-10 10:15:45
【问题描述】:

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

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

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

我正在使用带有 phantom-pdf 的 jsrender,当我在 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 有不同的颜色边框 - 颜色显示正确)

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

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

【问题讨论】:

  • 我无法编辑我的问题:HTML 示例下方的部分应为:附加调试器时,TD 标记中的文本会显示在 PDF 中。未附加调试器时,文本不会出现。 (TD 有不同的颜色边框 - 并且颜色显示正确)

标签: node.js visual-studio debugging asp.net-core pdf-generation


【解决方案1】:

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

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

快速浏览一下 SO,我发现了这个:How to set time delay in javascript

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

【讨论】:

  • 感谢您在这里分享您的解决方案,您可以接受它作为答案。这可能对阅读此主题的其他社区成员有所帮助。
  • 不幸的是,新任务优先;这似乎对我有用,但我还没有为此工作,所以我不能确定它是否已修复。
  • @Nigel Ellis,如果你能在你身边测试它会更好,所以我们可以知道它是否可以帮助你解决这个问题并帮助其他遇到同样问题的社区成员。等着你。祝你有美好的一天:)
  • 我已经回到了这个任务 - 当有自定义字体时,似乎仍然需要延迟(大约 1 秒)来加载字体。不过,图像不需要延迟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 2016-11-08
  • 2021-12-07
  • 2016-03-08
  • 1970-01-01
  • 2011-05-15
相关资源
最近更新 更多