Learn how to Print PDF to Printer in Python
Last active
May 19, 2026 01:22
-
-
Save aspose-com-gists/2cf5c716ed8c464739bb70a41faf2933 to your computer and use it in GitHub Desktop.
Print PDF File in Python | Print PDF to Printer | Printing PDFs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This code example demonstrates how to print a PDF file in Python. | |
| import aspose.pdf as ap | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf("Document.pdf"); | |
| # Print a PDF document | |
| viewer.print_document(); | |
| # Close PDF file | |
| viewer.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This code example demonstrates how to print a PDF file as Grayscale in Python. | |
| import aspose.pdf as ap | |
| # Load the input PDF document | |
| document = ap.Document("D:\\Files\\Output.pdf"); | |
| # Initiate RGB to Device Gry conversion strategy | |
| strategy = ap.RgbToDeviceGrayConversionStrategy(); | |
| # Loop through all the pages | |
| for page in document.pages: | |
| # Convert the RGB colorspace image to GrayScale colorspace | |
| strategy.convert(page); | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf(document); | |
| # Print PDF document | |
| viewer.print_document(); | |
| # Close PDF file | |
| viewer.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This code example demonstrates how to print multiple PDF files at once in Python. | |
| import os | |
| import aspose.pdf as ap | |
| # Directory path containing PDF files to print | |
| path = "D:\\Files\\" | |
| # Get PDF files | |
| files = [f for f in os.listdir(path) if f.endswith(".pdf")] | |
| # Read all files and print | |
| for file in files: | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf(path + file); | |
| # Print a PDF document | |
| viewer.print_document(); | |
| # Close PDF file | |
| viewer.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This code example demonstrates how to print a range of pages from a PDF file in Python. | |
| import aspose.pdf as ap | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf("Document.pdf"); | |
| # Set attributes for printing | |
| viewer.auto_resize = True | |
| viewer.auto_rotate = True | |
| viewer.print_page_dialog = False | |
| # Create objects for printer and page settings and PrintDocument | |
| pgs = ap.printing.PageSettings(); | |
| ps = ap.printing.PrinterSettings(); | |
| # Set printer name | |
| ps.printer_name = "Microsoft Print to PDF"; | |
| ps.print_range = ap.printing.PrintRange.SOME_PAGES; | |
| ps.from_page = 1; | |
| ps.to_page = 2; | |
| # Print document using printer and page settings | |
| viewer.print_document_with_settings(pgs, ps); | |
| # Close PDF file | |
| viewer.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Load secure PDF document while specifying User or Owner password | |
| document = ap.Document("Password.pdf" , "userORowner"); | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf(document); | |
| # Print PDF document | |
| viewer.print_document(); | |
| # Close PDF file | |
| viewer.close(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This code example demonstrates how to print a range of pages from a PDF file in Python. | |
| import aspose.pdf as ap | |
| # Create PdfViewer object | |
| viewer = ap.facades.PdfViewer(); | |
| # Open input PDF file | |
| viewer.bind_pdf("Document.pdf"); | |
| # Set attributes for printing | |
| viewer.auto_resize = True | |
| viewer.auto_rotate = True | |
| viewer.print_page_dialog = False | |
| # Create objects for printer and page settings and PrintDocument | |
| pgs = ap.printing.PageSettings(); | |
| ps = ap.printing.PrinterSettings(); | |
| # Set printer name | |
| ps.printer_name = "Microsoft Print to PDF"; | |
| ps.print_range = ap.printing.PrintRange.SOME_PAGES; | |
| ps.from_page = 1; | |
| ps.to_page = 2; | |
| # Set PageSize (if required) | |
| pgs.paper_size = ap.printing.PaperSize("A4", 827, 1169); | |
| # Set PageMargins (if required) | |
| pgs.margins = ap.devices.Margins(0, 0, 0, 0); | |
| # Print document using printer and page settings | |
| viewer.print_document_with_settings(pgs, ps); | |
| # Close PDF file | |
| viewer.close(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment