Print PDFs - Ruby 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
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12# The following sample illustrates how to print PDF document using currently selected
13# default printer.
14#
15# The first example uses the new PDF::Print::StartPrintJob function to send a rasterization
16# of the document with optimal compression to the printer. If the OS is Windows 7, then the
17# XPS print path will be used to preserve vector quality. For earlier Windows versions
18# the GDI print path will be used. On other operating systems this will be a no-op
19#
20# The second example uses PDFDraw send unoptimized rasterized data via awt.print API.
21#
22# If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you
23# should use PDFRasterizer or PDFNet vector output instead of PDFDraw.
24
25 if ENV['OS'] == 'Windows_NT'
26 PDFNet.Initialize(PDFTronLicense.Key)
27
28 # Relative path to the folder containing the test files.
29 input_path = "../../TestFiles/"
30
31 doc = PDFDoc.new(input_path + "tiger.pdf")
32 doc.InitSecurityHandler
33
34 # Set our PrinterMode options
35 printerMode = PrinterMode.new
36 printerMode.SetCollation(true)
37 printerMode.SetCopyCount(1)
38 printerMode.SetDPI(100) # regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
39 printerMode.SetDuplexing(PrinterMode::E_Duplex_Auto)
40
41 # If the XPS print path is being used, then the printer spooler file will
42 # ignore the grayscale option and be in full color
43 printerMode.SetOutputColor(PrinterMode::E_OutputColor_Grayscale)
44 printerMode.SetOutputQuality(PrinterMode::E_OutputQuality_Medium)
45 # printerMode.SetNUp(2,1)
46 # printerMode.SetScaleType(PrinterMode.e_ScaleType_FitToOutPage)
47
48 # Print the PDF document to the default printer, using "tiger.pdf" as the document
49 # name, send the file to the printer not to an output file, print all pages, set the printerMode
50 # and don't provide a cancel flag.
51 Print.StartPrintJob(doc, "", doc.GetFileName(), "", nil, printerMode, nil)
52 PDFNet.Terminate
53 puts "Done."
54 else
55 puts "This sample cannot be executed on this platform."
56 end

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales