question

Alex-Taylor1111 avatar image
0 Votes"
Alex-Taylor1111 asked RichMatheisen-8856 commented

Using "Verb -print" - How to pass additional parameters?

I am new to PS, and am looking to automate printing of files from a directory. I believe I've solved it for a 'real' printer, but in this case I am trying to flatten a .pdf using MS Print to PDF. I haven't been able to find any way to way to pass additional parameters (in this case, the output save location + file name)

Start-Process -file "C:\file.pdf" Verb -Print

As a workaround, I believe this will work to set the default printer (which Print uses) and a custom port (which can be set to a file folder)
Set-Printer -Name "Print to PDF" -Port "C:\OutputPDF\" [Edit: Adding ports can be restricted and does not appear to be a consistent workaround]

This works as long as Adobe is the default .pdf viewer which is an unideal, but acceptable limitation. This will open Adobe with a "Save As" dialog. I am looking to try to automate that final step and pass in a file name to automatically save as. So far I haven't seen an obvious flag that will allow this, and haven't been able to find a way to add input to the print verb or manipulate using the printmanagement PS commands.

This problem seems to be often asked, but no simple solution exists (e.g., not delving deep into multiple .Net classes)
https://docs.microsoft.com/en-us/powershell/module/printmanagement/set-printer?view=windowsserver2019-ps
https://docs.microsoft.com/en-us/answers/questions/503516/printing-using-the-printer-api.html
https://docs.microsoft.com/en-us/answers/questions/224327/print-pdf-to-another-pdf-with-powershell-to-flatte.html

windows-server-powershellwindows-server-print
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered Alex-Taylor1111 commented

Does this work?

automating-printing-to-pdf


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the response. Took me a bit to piece together the code (a learning process), but I believe I did it correctly.

Unfortunately I'm running into the same issue as the other users - I get a blank .pdf file as the output. My theory is that because the original use case was a plain text file, which .net print / drawing classes can handle easily. Everything else seems to either require .net drawing, or somehow leveraging the print/drawing engine of the application (e.g., Outlook, Publisher, Adobe).

From observations, I believe my original solution pushes the print through the default application, which then provides the proper rendering. If I can't provide the file name as an input, I would need to grab the printdocument coming from the Start-Process -file "C:\file.pdf" Verb -Print and set PrintDocument.PrinterSettings.PrintFileName = File path.


0 Votes 0 ·
RichMatheisen-8856 avatar image
1 Vote"
RichMatheisen-8856 answered RichMatheisen-8856 commented

I'm thinking that 3rd-party software is probably the way to go. A product named iText has a decent reputation and works with Java and .Net. I'm pretty sure it's open source, too.

For lots of examples just search for "powershell itext print pdf" (for example).

Their site is itextpdf.com


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I appreciate the research. Unfortunately getting software whitelisted is very onerous and why I've tried hard to find alternatives. Given how many threads I've seen (going back to 2015) this is a common use case, but I haven't seen any solved without significant C#/VB code or 3rd party software.

Thanks again for your thoughts on this.

0 Votes 0 ·

If you have C# code that works you can include it in your PowerShell script. Here's just one of many pages that describe how: executing-c-code-using-powershell-script


0 Votes 0 ·