I have an app deployed on a server that runs a PowerShell command to launch headless Chrome and print a website as a PDF but it fails to generate the file.
I tried running the command directly from PowerShell. It seems that when I try to generate the file in C:/Program Files
or a folder within it, it silently fails. But then to some folders, like Download or where the app is deployed, it generates it fine. And to even other locations, like C:
, it shows that I am missing permissions.
Chrome is installed on the server. I also tried this on my local machine and I'm getting the same results.
This is the failing command I'm using to try and generate a pdf within Program Files folder:
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"
Command succeeds if the target folder is pointed to Downloads.
Why can't I generate a PDF within C:/Program Files
folder (and possibly others) and what can I do to fix this?
Edit: I am actually using the following syntax for the command:
$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
The issue comes from Chrome.exe not understanding PowerShell's way of quoting, where it just wants double quotes for the path:
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"
The need for double double-quotes only comes when you're looking to escape the quotes within a pair of double quotes, whereas single quotes would work too. I know, confusing, so here's an example:
# 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"'
For windows 7/8/9/10/11 users native inbuilt MSEdge can do this very simply (no need to add another chromium or slow it down it is instant from a cmd line or shortcut)
For 32bit users run
"C:\Program Files\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="C:\whichever folder\PDFfromCMD.pdf" https://google.com
For 64bit users run
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="C:\whichever folder\PDFfromCMD.pdf" https://google.com
Note --no-margin or --no-margins does nothing other than make the line longer, and older --export-tagged-pdf is no longer needed as its the current default, however you may wish to add --no-pdf-header-footer
or some other options to slow down premature building as its so fast the html may not have fully loaded so perhaps you may use --run-all-compositor-stages-before-draw
Microsoft docs will refer you to a list of switches for chrome which can be found at https://peter.sh/experiments/chromium-command-line-switches/
so run this way we can print and view this question (and answers) note I did not switch off headline(s) so have both a timestamp and full title, and in the footline(s) a hyperlink to this page and number of pages.
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --headless --print-to-pdf="%userprofile%\desktop\pdfFromcmd.pdf" --enable-logging https://stackoverflow.com/questions/72018079/why-cant-chromes-print-to-pdf-powershell-command-generate-a-pdf-to-some-fold && "%userprofile%\desktop\pdfFromcmd.pdf"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With