Print PDFs - Python Sample Code

Sample code for using Apryse SDK to print a PDF file using the currently selected default printer; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. It is possible to use this printing functionality in both client and server applications without dependence on any third party components. Learn more about our Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14# The following sample illustrates how to print PDF document using currently selected
15# default printer.
16#
17# The first example uses the new PDF::Print::StartPrintJob function to send a rasterization
18# of the document with optimal compression to the printer. If the OS is Windows 7, then the
19# XPS print path will be used to preserve vector quality. For earlier Windows versions
20# the GDI print path will be used. On other operating systems this will be a no-op
21#
22# The second example uses PDFDraw send unoptimized rasterized data via awt.print API.
23#
24# If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you
25# should use PDFRasterizer or PDFNet vector output instead of PDFDraw.
26
27def main():
28 PDFNet.Initialize(LicenseKey)
29
30 # Relative path to the folder containing the test files.
31 input_path = "../../TestFiles/"
32
33 doc = PDFDoc(input_path + "tiger.pdf")
34 doc.InitSecurityHandler()
35
36 # Set our PrinterMode options
37 printerMode = PrinterMode()
38 printerMode.SetCollation(True)
39 printerMode.SetCopyCount(1)
40 printerMode.SetDPI(100); # regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
41 printerMode.SetDuplexing(PrinterMode.e_Duplex_Auto)
42
43 # If the XPS print path is being used, then the printer spooler file will
44 # ignore the grayscale option and be in full color
45 printerMode.SetOutputColor(PrinterMode.e_OutputColor_Grayscale)
46 printerMode.SetOutputQuality(PrinterMode.e_OutputQuality_Medium)
47 # printerMode.SetNUp(2,1)
48 # printerMode.SetScaleType(PrinterMode.e_ScaleType_FitToOutPage)
49
50 # Print the PDF document to the default printer, using "tiger.pdf" as the document
51 # name, send the file to the printer not to an output file, print all pages, set the printerMode
52 # and don't provide a cancel flag.
53 Print.StartPrintJob(doc, "", doc.GetFileName(), "", None, printerMode, None)
54 PDFNet.Terminate()
55
56if __name__ == '__main__':
57 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales