none
Printing file from PowerShell script not working RRS feed

  • Question

  • Hey Scripting Guys,

    I am trying to use a PowerShell script to print a PDF file to a .prn file. The (relevant) part of the script is:

    $pd = New-Object System.Drawing.Printing.PrintDocument
    $pd.DocumentName = "c:\temp\mydoc.pdf"
    $pd.Printersettings.PrinToFile = $true
    $pd.PrinterSettings.PrinterName = "HP"
    $pd.PrinterSettings.PrintFileName = "C:\temp\x.prn"
    $pd.Print()

    The script runs (with a popup info box saying "Printing page 1 of c:\temp\mydoc.pdf") and terminates. The output file x.prn only contains @PJL commands but no content.

    If I set PrinToFile to $false the printer just outputs a blank page. This happens whether I print a .pdf or a .txt file.

    What am I missing?

    • Moved by Bill_Stewart Thursday, December 20, 2018 8:35 PM This is not third-party support forum
    Saturday, October 13, 2018 4:08 PM

All replies

  • PDF files can only be printed if Adobe or other PDF program is installed and registered as the print provider.

    "PrintDocument" does no work with PDF files.

    This will work if all things are installed and registered correctly.

    start-process -FilePath c:\temp\mydoc.pdf -Verb Print


    \_(ツ)_/

    Saturday, October 13, 2018 5:50 PM
  • I have Adobe Reader installed and registered. 

    start-process -FilePath c:\temp\mydoc.pdf -Verb Print

    does start Reader and print the file. Problem is the "start-process" way doesn't allow me to print to a file.

    The other problem is that PrintDocument doesn't seem to work even for plain text files either. :(

    Sunday, October 14, 2018 10:19 PM
  • PrintDocument can only print PRN files.

    Adobe can print from the command line and select the printer. Post in Adobe forum to find out how.


    \_(ツ)_/

    Sunday, October 14, 2018 10:23 PM
  • You can print text files to an printer like this:

    Get-Content file.txt | Out-Printer -Name <printer name>


    \_(ツ)_/

    Sunday, October 14, 2018 10:41 PM
  • I'm afraid you haven't read my original question very carefully. I know how to actually print a file to a physical printer. I even know how to print to a printer other than the default ( -verb PrintTo("some other printer") ). What I need to do is to capture what would normally get sent to the printer in a file as if I had checked the "Print to file" box and entered a .PRN file name, and I need to do this COMPLETELY UNDER SCRIPT CONTROL - no human interaction required or desired.
    Sunday, October 14, 2018 10:55 PM
  • You can only do that from the Adobe command line calls or from the Adobe automation COM objects.


    \_(ツ)_/

    Sunday, October 14, 2018 11:10 PM
  • I've looked at the Adobe command line options (the "/t" option is what I'm thinking of) but don't see that it allows me to set "print to file". Perhaps the Adobe COM objects. Could you point me to documentation for those?

    I'm thinking, too, maybe I can use a printer whose port is set to FILE: and somehow enter a file name programmatically once the "save file" dialog box pops up. Can you point me to some documentation or examples of that? I am, in case you haven't noticed, a relative newbie to PowerShell. :)

    Monday, October 15, 2018 1:03 AM
  • You can find it on the adobe site in the developers area.  I don't have a link.


    \_(ツ)_/

    Monday, October 15, 2018 1:09 AM