【问题标题】:wkhtmltopdf loading pages error in powershell scriptPowerShell脚本中的wkhtmltopdf加载页面错误
【发布时间】:2019-12-09 21:50:12
【问题描述】:

我正在 Windows 机器上从 powershell 脚本运行 wkhtmltopdf。 我使用的命令是

& "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"    "C:\Desktop\Test.htm"  "C:\Desktop\Report.pdf"

正在创建 pdf 文件,但它也抛出错误

wkhtmltopdf.exe : Loading pages (1/6)
At line:1 char:2
+ & <<<<  "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"     "C:\Test.htm"  "D:\Report.pdf"
+ CategoryInfo          : NotSpecified: (Loading pages (1/6):String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

[>                                                           ] 0%[======>                                                         ] 10%[==============================>                             ]
50%[============================================================]  100%Counting pages (2/6)                                               
[============================================================] Object 1 of  1Resolving links (4/6)                                                       
[============================================================] Object 1 of  1Loading headers and footers (5/6)                                           
Printing pages (6/6)
[>                                                           ]   Preparing[============================================================] Page 1  of 1Done    

由于脚本返回错误,我无法继续进行。 请为此提供帮助。

【问题讨论】:

  • 看来wkhtmltopdf.exe 正在将输出发送到错误流。你能做这样的事情吗$result = &amp; "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" "C:\Desktop\Test.htm" "C:\Desktop\Report.pdf"。这不是一个错误。 Just 是通过错误流来的,因此 PowerShell 将其视为一个。如果您可以捕获输出,那么您将不会看到这些消息。
  • 嗨@matt,非常感谢您的及时回复。但是在进行建议的更改后,我也面临同样的错误。请帮我解决,

标签: powershell wkhtmltopdf


【解决方案1】:

上面接受的答案说捕获结果对我不起作用,但是查看 WKHTMLTOPDF 文档我发现了抑制信息文本(默认输出)的正确方法。

您需要将 --log-level 设置为错误,以便它只会输出真正的错误文本,而不是 powershell 错误解释为错误的信息文本。

  --log-level <level>             Set log level to: none, error, warn or
                                  info (default info)

【讨论】:

    【解决方案2】:

    您需要处理生成的错误文本。让我们将 std err 放到 std out 并捕获结果(以防您想查看它们)

    $result = & 'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe' 'www.google.ca' "c:\temp\pdf.pdf" 2>&1
    

    如果实际的控制台输出不重要,您可以将其发送至Out-Null

    & 'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe' 'www.google.ca' "c:\temp\pdf.pdf" 2>&1 | Out-Null
    

    【讨论】:

    • 你是个天才。谢谢你救了我。非常感谢。它就像魔法一样工作。
    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 2019-04-12
    • 2016-04-08
    相关资源
    最近更新 更多