Printing multiple format documents into PDF file

Hi,
I need to convert multiple document formats into pdf. I was trying to print them into Microsoft Pdf Printer using PowerShell. I need to do many files, so I don’t want any “save as” prompts.

Found this script:

function ConvertTo-PDF {
	param(
		$TextDocumentPath
	)
	
	Add-Type -AssemblyName System.Drawing
	$doc = New-Object System.Drawing.Printing.PrintDocument
	$doc.DocumentName = $TextDocumentPath
	$doc.PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
	$doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
	$doc.PrinterSettings.PrintToFile = $true
	$file=[io.fileinfo]$TextDocumentPath
	$pdf= [io.path]::Combine($file.DirectoryName, $file.BaseName) + '.pdf'
	$doc.PrinterSettings.PrintFileName = $pdf
	$doc.Print()
	$doc.Dispose()
}

It does not prompt me to save the file, but prints empty page.

Another script:

Function Printto-PDF ($filepath) {
       $VerbosePreference = "Continue"
       add-type -AssemblyName microsoft.VisualBasic
       add-type -AssemblyName System.Windows.Forms
       $focus = 2000
       $FilePath = Get-Item $Filepath
       $newfile = $Filepath.basename + '.pdf'
       Start-Process $Filepath.fullname -verb Print | out-printer -name "Microsoft print to PDF"  
       start-sleep -Milliseconds $focus
       [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
       start-sleep -Milliseconds 250
       [System.Windows.Forms.SendKeys]::SendWait($newfile)
       start-sleep -Milliseconds 250
       [System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
       start-sleep -Milliseconds 1500
}

This one prints fine, but prompts me with “save as” dialog, which I don’t want.

How can I make it so I don’t get empty pages and prompts?

Thanks.

Well that first script is never going to work; it does nothing with the actual document data. That would require a lot more work to do anything useful.

You need a 3rd party tool or print driver to do what you’re trying to do. Once you have that, then a simple foreach loop should be all that’s needed.

FoxIT reader used to bundle a print driver with their PDF viewer and that had command line print support that allowed you to specify the filename as part of the command; for some reason they removed that (the printer) with version 10 but you might be able to find an old version for download. GhostScript should also be able to do this but I’m not sure on the licence status for commercial use.

Ok, in this case why the second script prints fine? How can I automate the “Save As” menu in the second script to save the new pdf file into the same folder where the old file was? I printed multiple formats with the second script, including word documents, html documents and email messages. All printed ok.

Yes, it prints fine but it doesn’t accept accept any command line parameters which is what you need to automate it. You said you didn’t want it to pop up the Save As dialog box and just about every PDF printer will use a GUI to prompt you for a filename.

If you can live with the pop-up (and assuming SendKeys is working for you to automate entering the filename) then just call that function in a loop. How are you deciding which files to print? e.g. do you have a list of files paths, or are you using Get-ChildItem?

As long as I can automate it, pop up is not a big issue, I just want automation, as there are thousands of files. I have them in the database with full paths and extensions. I decide by extension what to print, if it is not pdf, jpg, jpeg or png I need to print it as pdf to feed to amazon recognition and put the recognised text back into the database.

Perform a SELECT query on your database to pull out the list of paths for files that match your criteria and put that in a text file.

$files = Get-Content C:\Temp\listOfFiles.txt
foreach ($file in $files) {
    Printto-PDF $file
}

Enumerating the files is not a problem. I’ve tried this on one file:

$file = 'C:\Folder1\Subfolder1\Subfolder2\MyFile.doc'
Printto-PDF $file

I saw the MS Word windows showing briefly and closing after with no “save as” dialogue, but I can’t find the printed file. Where could it be? I need it in the same folder as the original file. I’ve tried searching the whole C: drive, but could not find the printed file. It could print under a different name. I need to keep the same file name, just different extension.

Actually on the second run I got the “save as” dialogue window. The file printed fine, but this methods lacks automation, I can’t possibly do 15,000 times “save as” typing and clicking.

The function seems to work OK with small files but I think the processing time of more complicated files means the Start-Sleep times are a bit hit and miss. Because of that, the SendKeys commands are being sent at the wrong time, resulting in errors. The Microsoft print to PDF driver also handles images differently to documents, popping up an intermediate window instead of going straight to the Save As diaglog, this also breaks the function.

I go back to my earlier suggestion which is that you need to find a print driver or tool for generating PDFs that accepts command line input so that you can integrate it with PowerShell. The Microsoft print to PDF printer isn’t suitable for this kind of automation.

Although I do agree with Matt, one way to avoid the prompt is to set the output port on the PDF Printer in devices and printer to a filename. Say ‘C:\temp\MyFile.PDF’. I would add a new/second print to pdf to use for this, then set that port using local port and set to a file name. Then, in your script/loop through file names, change the port name/filename for your new PDF to print. I did test this for one file and it worked, but dont have time to work through the port changing in a loop.

This way, you could ditch the function “PrintTo-PDF” altogether and just use:

Start-Process $Filepath.fullname -verb Print | out-printer -name "Microsoft print to PDF"

Might be worth a shot. Since I am curious on this, if I find time, I will put something together to test.

1 Like

OK, this worked for me. You need to set a loop on your 15K files and come up with the proper filename/path for the PrinterPort … I used a simple text document … but this might get you started:

$SomeFileName = 'MyNewPDFFile.PDF'
Add-PrinterPort -Name "C:\temp\$SomeFileName"
Set-Printer -Name 'Microsoft Print to PDF' -PortName "C:\temp\$SomeFileName"
$TextDocument = Get-Content 'C:\Path\to\File\To\Print.TXT'
$TextDocument | Out-Printer -Name "Microsoft print to PDF"
Remove-PrinterPort -Name "C:\temp\$SomeFileName"

Your PDF file will then be in C:\Temp\$SomeFileName. Again, I would create a new Print To PDF to play with, but if you use the existing, you can set the port back to original via:

Set-Printer -Name 'Microsoft Print to PDF' -PortName "PORTPROMPT:"

Lastly, Set-Printer requires admin

2 Likes

@roustam-akhmetov not even a thank you to those that did the work for you?