我在服務器上部署了一個應用程式,該應用程式運行 PowerShell 命令來啟動無頭 Chrome 并將網站列印為 PDF,但它無法生成檔案。
我嘗試直接從 PowerShell 運行命令。似乎當我嘗試在其中生成檔案C:/Program Files或其中的檔案夾時,它會默默地失敗。但是對于某些檔案夾,例如下載或部署應用程式的位置,它會生成它。甚至到其他位置,例如C:,它表明我缺少權限。
Chrome 已安裝在服務器上。我也在我的本地機器上試過這個,我得到了同樣的結果。
這是我用來嘗試在 Program Files 檔案夾中生成 pdf 的失敗命令:
Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\chrome.exe" -ArgumentList "--headless","--print-to-pdf=C:\Program Files\pdfFromPowershell.pdf","--no-margins","--enable-logging","https://google.com"
如果目標檔案夾指向下載,則命令成功。
為什么我不能在C:/Program Files檔案夾(可能還有其他檔案夾)中生成 PDF,我該怎么做才能解決這個問題?
編輯:我實際上對命令使用以下語法:
$chrome='C:\Program Files\Google\Chrome\Application\chrome.exe'; & $chrome --headless --print-to-pdf-no-header --print-to-pdf='C:\Program Files\temp\pdfFromPowershell.pdf' --no-margins https://google.com
uj5u.com熱心網友回復:
問題來自Chrome.exe不理解 PowerShell 的參考方式,它只需要路徑的雙引號:
Start-Process -FilePath "C:\Program Files\Google\Chrome\Application\chrome.exe" `
-ArgumentList "--headless",
"--print-to-pdf=""C:\Program Files\pdfFromPowershell.pdf""",
"--no-margins","--enable-logging","https://google.com"
只有當您希望在一對雙引號中轉義引號時,才需要雙雙引號,而單引號也可以。我知道,令人困惑,所以這里有一個例子:
# escape the double quotes inside double quotes by doubling quotes inside.
"--print-to-pdf=""C:\Program Files\pdfFromPowershell.pdf"""
# using single quotes this should work as well, where only one pair is needed.
'--print-to-pdf="C:\Program Files\pdfFromPowershell.pdf"'
uj5u.com熱心網友回復:
對于 Windows 7/8/9/10/11 用戶,本機內置 MSEdge 可以非常簡單地做到這一點(無需添加另一個鉻或減慢它的速度,它是即時從 cmd 行或快捷方式)
對于 32 位用戶運行
"C:\Program Files\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="C:\whichever folder\PDFfromCMD.pdf" https://google.com
對于 64 位用戶運行
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="C:\whichever folder\PDFfromCMD.pdf" https://google.com
注意 --no-margin 或 --no-margins 只會使行更長,并且不再需要舊的 --export-tagged-pdf 作為其當前默認值,但是您可能希望添加--print-to-pdf-no-header或其他一些選項減慢過早的構建速度,因為它的速度如此之快,html 可能沒有完全加載,所以也許你可以使用--run-all-compositor-stages-before-draw
Microsoft 檔案將向您推薦 chrome 的開關串列,可在
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/465194.html
上一篇:Redis快取相關的幾個問題
