Print PDF File in Python | Print PDF to Printer | Printing PDFs

Printing a PDF document is a common requirement in application development. Whether you are generating reports, invoices, or any other type of document, printing a PDF file directly from code to a printer can be a valuable feature. In this blog post, we will learn how to print a PDF file to a printer in Python.

This article covers the following topics:

Python Library to Print PDF to Printer

For printing a PDF file in Python programmatically, we will use the Aspose.PDF for Python via .NET library. With simple installation and usage, it provides an efficient solution for sending PDF files to the printer using Python code. Aspose.PDF for Python allows developers to generate, manipulate, and convert PDF documents in their Python applications.

To get started with printing a PDF document in Python, we first need to install the necessary libraries. Please download the package or install the API from PyPI using the following pip command in the console:

> pip install aspose-pdf

We can easily print a PDF file programmatically in Python by following the steps below:

  1. Create an instance of the PdfViewer class.
  2. Load the input PDF document using the bind_pdf() method.
  3. After that, call the print_document() method to print the PDF file.
  4. Finally, close the PDF viewer using the close() method.

The following code sample shows how to print a PDF file in Python.

We can also batch print PDF files from a folder in Python by following the steps below:

  1. Load PDF files from the provided folder path.
  2. Loop through all the PDF files.
  3. Create an instance of the PdfViewer class.
  4. Bind each input PDF file one by one using the bind_pdf() method.
  5. After that, call the print_document() method to print the PDF file.
  6. Finally, close the PDF viewer using the close() method.

The following code sample shows how to print multiple PDF files in Python.

In certain cases, we may need to print a specific range of pages from a PDF document. For this purpose, we need to specify the from and to page numbers. We can print the page range by following the steps below:

  1. Create an instance of the PdfViewer class.
  2. Load the input PDF document using the bind_pdf() method.
  3. Optionally, specify various viewer attributes, such as auto_resize, auto_rotate, etc.
  4. Create an instance of the PageSettings class.
  5. Initialize the PrinterSettings class object.
  6. Specify the print_range, from_page, and to_page properties.
  7. After that, call the viewer.print_document_with_settings() method to print the document using the printer and page settings.
  8. Finally, close the PDF viewer using the close() method.

The following code sample shows how to print specific pages of a PDF document in Python.

Specify Page Size for Printing a PDF

Similarly, we can also specify the page size while printing the PDF file by following the steps mentioned earlier. However, we just need to specify the paper size using the PaperSize class and the margins using the Margins class at step 6.

The following code sample shows how to print the PDF document at a specific page size and margins using Python.

We can print a secured or password protected PDF file by following the steps below:

  1. Load the secured PDF with a password using the Document class.
  2. Create an instance of the PdfViewer class.
  3. Bind the PDF document using the bind_pdf() method.
  4. After that, call the print_document() method to print the PDF file.
  5. Finally, close the PDF viewer using the close() method.

The following code sample shows how to print a secured PDF file in Python.

We can print a PDF document in grayscale by following the steps below:

  1. Load the input PDF document using the Document class.
  2. Initialize the RgbToDeviceGrayConversionStrategy class object.
  3. Loop through all the pages and convert to GrayScale using the strategy.convert() method.
  4. Create an instance of the PdfViewer class.
  5. Bind the PDF document using the bind_pdf() method.
  6. After that, call the print_document() method to print the PDF file.
  7. Finally, close the PDF viewer using the close() method.

The following code sample shows how to convert a PDF to grayscale and then print it using Python.

Get a Free License

You can get a temporary license in order to use the library without evaluation limitations.

Print PDF – Free Resources

Besides printing PDFs to printer in Python, learn more about creating, manipulating, and converting PDF documents, and explore various other features of the library using the resources below:

Conclusion

In this article, we have learned how to print a PDF file in Python. By leveraging Aspose.PDF for Python, you can easily send your PDF files to a printer from within your Python code. This powerful feature empowers users to generate reports and manage document workflows seamlessly across different applications. By following the steps outlined in this blog post, you can automate the task of printing PDFs. In case of any ambiguity, please feel free to contact us on our free support forum.

See Also