Forum Discussion

singhn83's avatar
singhn83
Copper Contributor
Mar 25, 2023

Unable to print to pdf double copies (to a single file) using powershell

Hi,

I am trying to print a single file with 1 page pdf to 2 pages pdf (single file). However, the below code only produces a blank file.

Can someone help me debug or guide me how to fix the issue?

 

 

 

# Load Assembly
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

# Microsoft Print to PDF
$filePath = "C:\Users\Worksheets\Print Font Type\01-A\A\01-A.pdf"

$printerName = "Microsoft Print to PDF"
$printerPort = "PORTPROMPT:"
$numCopies = 2

# Create a PrinterSettings object and set the number of copies
$printerSettings = New-Object System.Drawing.Printing.PrinterSettings
#$printerSettings.Copies = $numCopies

# Create a PrintDocument object and set the file name and printer
$printDoc = New-Object System.Drawing.Printing.PrintDocument
$printDoc.DocumentName = $filePath
$printDoc.PrinterSettings = $printerSettings

# Set the printer to Microsoft Print to PDF and print the document
$printDoc.PrinterSettings.PrinterName = $printerName
$printDoc.PrinterSettings.PrintToFile = $true
$printDoc.PrinterSettings.PrintFileName = "$filePath"
$printDoc.PrinterSettings.Copies = $numCopies

# PrinterPort doesn't work
#$printDoc.PrinterSettings.PrinterPort = $printerPort

$printDoc.Print()

# Wait for the print job to complete
Start-Sleep -Seconds 10

 

 

 

3 Replies

  • singhn83 

    The reason you're getting a blank file is because you're trying to print a PDF file using the Microsoft Print to PDF printer, but you haven't specified the printer driver to use.

    To fix this issue, you can install the Adobe PDF printer driver on your system and then use it to print the PDF file to multiple pages.

     

    Install the Adobe PDF printer driver by following the instructions on the Adobe website.

     

    Change the $printerName variable to "Adobe PDF" to use the Adobe PDF printer driver instead of the Microsoft Print to PDF printer driver.

     

    In the PrintDocument object, set the PrinterSettings.PrintToFile property to $false to print directly to the printer instead of to a file.

     

    Add the PrintPage event handler to the PrintDocument object to print the same page twice.

    # Load Assembly
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    
    # Adobe PDF printer
    $filePath = "C:\Users\Worksheets\Print Font Type\01-A\A\01-A.pdf"
    $printerName = "Adobe PDF"
    $numCopies = 2
    
    # Create a PrinterSettings object and set the number of copies
    $printerSettings = New-Object System.Drawing.Printing.PrinterSettings
    $printerSettings.Copies = $numCopies
    
    # Create a PrintDocument object and set the file name and printer
    $printDoc = New-Object System.Drawing.Printing.PrintDocument
    $printDoc.DocumentName = $filePath
    $printDoc.PrinterSettings = $printerSettings
    
    # Set the printer to Adobe PDF
    $printDoc.PrinterSettings.PrinterName = $printerName
    
    # Disable printing to file
    $printDoc.PrinterSettings.PrintToFile = $false
    
    # Define the PrintPage event handler to print the same page twice
    $printDoc.add_PrintPage({
        $page = $args[1]
        $args.Graphics.DrawImage($page, 0, 0, $page.Width, $page.Height)
        $args.HasMorePages = $true
    })
    
    # Print the document
    $printDoc.Print()
    
    # Wait for the print job to complete
    Start-Sleep -Seconds 10
    • singhn83's avatar
      singhn83
      Copper Contributor

      Varun_Ghildiyal 

       

      I am able to print using Microsoft Print to PDF option when using GUI. Shouldn't that work when use powershell?

       

      I get the following error

      Cannot convert argument "image", with value: "System.Drawing.Printing.PrintPageEventArgs", for "DrawImage" to type "System.Drawing.Image": "Cannot convert the "System.Drawing.Printing.PrintPageEventArgs" value of
      type "System.Drawing.Printing.PrintPageEventArgs" to type "System.Drawing.Image"."
      At line:29 char:5
      + $args.Graphics.DrawImage($page, 0, 0, $page.Width, $page.Height)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (:) [], MethodException
      + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

      The property 'HasMorePages' cannot be found on this object. Verify that the property exists and can be set.
      At line:30 char:5
      + $args.HasMorePages = $true
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (:) [], RuntimeException
      + FullyQualifiedErrorId : PropertyAssignmentException

Resources