How To Print PDF Files Using Python

Introduction

Printing PDF files in Python is a valuable capability, enabling developers to work with documents in a platform-independent manner. PDFs are extensively utilized for storing and distributing files, making them essential for Python applications involving document management or workflows.

Python offers several methods for generating and printing PDF files, with one popular approach being the use of libraries that provide classes specifically designed for creating and manipulating PDF documents. This comprehensive How-to Guide focuses on utilizing the IronPDF library to effortlessly generate and print PDF files within Python scripts.


IronPDF: Python PDF Library

IronPDF is a powerful Python library that empowers developers to generate, manipulate, and convert PDF documents. Inspired by the IronPDf C# .NET library, it offers a wide range of features suitable for the Python ecosystem.

With IronPDF, developers can leverage a high-level API that simplifies working with PDF files, eliminating the need to handle low-level intricacies. It provides convenient methods for common PDF operations, including creating new documents, adding content, formatting text, merging and splitting PDF files.

One standout feature of IronPDF is its ability to seamlessly convert HTML, CSS, and JavaScript code into PDF format. This functionality facilitates the effortless generation of PDF files from web pages or HTML templates. Furthermore, IronPDF includes support for printing PDF documents, adding to its versatility and utility.

Install IronPDF via Pip

You can easily add the IronPDF library to your Python project using pip. Use the provided command below to install IronPDF via pip:

 pip install ironpdf

Add IronPDF package by using the following code:

from ironpdf import *
PYTHON

Load a PDF

IronPDF for Python offers a convenient constructor that allows you to load PDF content into the code. This constructor accepts various valid arguments, such as a byte array or a file path. If you're working with password-protected documents, you can also provide a second parameter to specify the password.

The following code snippet demonstrates how to load a PDF file located on the filesystem.

License.LicenseKey = "Enter-Your-License"  
pdf = PdfDocument.FromFile("MyPdf.pdf")
PYTHON

Print a PDF Document With Default Settings

IronPDF provides two ways for printing PDF files.

he first method allows you to print the document instantly using the default printer and page settings. You can achieve this by using the Print method.

pdf.Print()
PYTHON

Customize Print setting

Another option is to provide users with the ability to customize printing options before initiating the print process. By utilizing the GetPrintDocument method, you can access and modify the printing settings. This method will return a PrintDocument object, granting you access to its PrinterSettings attribute for adjusting the desired settings.

printer_setting = pdf.GetPrintDocument()
printer_setting.PrinterSettings.FromPage = 2
printer_setting.PrinterSettings.ToPage = 4
printer_setting.Print()
PYTHON

Full Source Code

Below is the complete source file used in this How-To Guide.

from ironpdf import *

License.LicenseKey = "Enter-Your-License"  
pdf = PdfDocument.FromFile("MyPdf.pdf")

pdf.Print()

printer_setting = pdf.GetPrintDocument()
printer_setting.PrinterSettings.FromPage = 2
printer_setting.PrinterSettings.ToPage = 4
printer_setting.Print()
PYTHON

Summary

In summary, IronPDF is a robust and user-friendly library that simplifies PDF printing in Python applications. With its extensive feature set and comprehensive documentation, IronPDF empowers users to effortlessly generate and customize high-quality PDFs that can be easily printed or shared. Whether you need to create invoices, reports, or any other type of document, IronPDF has all the tools you need.

Make the most of IronPDF's free trial for testing in a production environment. Pricing of IronPDF starts from $749. Experience the benefits of IronPDF by giving it a try with the trial license, and witness how it can streamline your PDF printing workflow.

Download the software product.