Print PDFs - PHP 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<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10/**
11 * The following sample is a used to illustrate how to print
12 * PDF document using currently selected default printer. In this sample, PDF::Print class
13 * is used to send data to the printer.
14 *
15 * Following this function is the more complex way of using PDFDraw directly.
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.
20 *
21 * The second example uses PDFDraw send unoptimized rasterized data via the GDI print path.
22 *
23 * If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you
24 * should use PDFRasterizer or PDFNet vector output instead of PDFDraw.
25 */
26
27 PDFNet::Initialize($LicenseKey);
28 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
29
30 // Relative path to the folder containing test files.
31 $input_path = getcwd()."/../../TestFiles/";
32 $doc = new PDFDoc($input_path."tiger.pdf");
33 $doc->InitSecurityHandler();
34
35 // Set our PrinterMode options
36 $printerMode = new PrinterMode();
37 $printerMode->SetCollation(true);
38 $printerMode->SetCopyCount(1);
39 $printerMode->SetDPI(600); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
40 $printerMode->SetDuplexing(PrinterMode::e_Duplex_Auto);
41
42 // If the XPS print path is being used, then the printer spooler file will
43 // ignore the grayscale option and be in full color
44 $printerMode->SetOutputColor(PrinterMode::e_OutputColor_Grayscale);
45 $printerMode->SetOutputQuality(PrinterMode::e_OutputQuality_Medium);
46 // $printerMode->SetNUp(2,1);
47 // $printerMode->SetScaleType(PrinterMode::e_ScaleType_FitToOutputPage);
48
49 // Print the PDF document to the default printer, using "tiger.pdf" as the document
50 // name, send the file to the printer not to an output file, print all pages, set the printerMode
51 // and don't provide a cancel flag.
52 PDFPrint::StartPrintJob($doc, "", $doc->GetFileName(), "", null, $printerMode, null);
53 PDFNet::Terminate();
54?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales